要求:代码生成jar包,浏览器登陆后,运行jar包时,将获取浏览器SESSIONID值,并发数,测试接口地址作为参数传进去。注意:获取sessionID时,需要在开头拼接上"SESSION="字符串,若有就不需要拼接了;
package com.dhc.runmain;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class InitMainEnt {
public static void main(String[] argsq) {
String[] args={"1",
"http://localhost:8083/ccb-webfront/login/login1.do",
"SESSION=MDUwYTEyYTktMzM5NS00YzM0LWIzNWItOWI2MjUyYWVhYzU3",
"40"};
if("1".equals(args[0])){
System.out.println("----------------并发测试开始");
multiRuntime(args[1],args[2],Integer.valueOf(args[3]));
}else{
System.out.println("----------------疲劳测试开始");
strain(args[1],args[2]);
}
}
/**
* 压力测试
*
*/
public static void multiRuntime(String url,String sessionId,int count){
// int count = 10;//设置并发数量
int z =0;
while(true){
z++;
//线程池准备
CyclicBarrier cyclicBarrier = new CyclicBarrier(count);
ExecutorService executorService = Executors.newFixedThreadPool(count);
long now = System.currentTimeMillis();//开始时间
for (int i = 0; i < count; i++){
executorService.execute(new InitMainEnt().new Task(cyclicBarrier,i,url,sessionId));
}
executorService.shutdown();
while (!executorService.isTerminated()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
long end = System.currentTimeMillis();//结束时间
System.out.println("第"+z+"批总共耗时!---------"+(end-now));//总共耗时
}
}
public class Task implements Runnable {
private CyclicBarrier cyclicBarrier;
private int count;
private String url;
private String sessionId;
public Task(CyclicBarrier cyclicBarrier,int count,String url,String sessionId) {
this.cyclicBarrier = cyclicBarrier;
this.count = count;
this.url=url;
this.sessionId=sessionId;
}
@Override
public void run() {
try {
// 等待所有任务准备就绪
cyclicBarrier.await();
System.out.println("线程"+Thread.currentThread().getName()+"开始----------");
//测试路径
String url_ =url;
boolean result = HttpClientUtil.sendPost(url_,sessionId);
if(result){
System.out.println(result);
System.out.println("线程"+Thread.currentThread().getName()+"----------成功");
}else{
System.out.println(result);
System.out.println("线程"+Thread.currentThread().getName()+"----------失败");
}
cyclicBarrier.await();
} catch (Exception e) {
System.out.println("出现超时的线程"+count);
e.printStackTrace();
}
}
}
/**
* 疲劳测试
*
*/
public static void strain(String url,String sessionId){
long start = System.currentTimeMillis();
while(true){
boolean result = HttpClientUtil.sendPost(url,sessionId);
if(!result){
long end = System.currentTimeMillis();//结束时间
System.out.println("距离开始时间"+(end-start)/1000 +"秒后,疲劳测试出错");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
package com.dhc.runmain;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.Map;
public class HttpClientUtil {
public static boolean sendPost(String url,String sessionId) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
try {
if(url==null||url.isEmpty()){
System.out.println("URL为空!");
return false;
}
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性
// conn.setRequestProperty("accept", "*/*");
// conn.setRequestProperty("Cookie", cookie);
// conn.setRequestProperty("connection", "Keep-Alive");
// conn.setRequestProperty("user-agent",
// "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
//
// conn.setRequestProperty("Content-Type", "application/json; charset=utf8");
conn.setDoOutput(true);
// conn.setRequestMethod("GET");
conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
// conn.setRequestProperty("Accept-Charset", "GBK,utf-8;q=0.7,*;q=0.3");
conn.setRequestProperty("Accept-Encoding", "gzip, deflate, br");
conn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.9");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("Cookie", sessionId);
conn.setRequestProperty("Host", "localhost:8083");
// conn.setRequestProperty("Referer", referer);
conn.setRequestProperty("User-Agent",
"Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36");
conn.setRequestProperty("X-Requested-With", "XMLHttpRequest");
// 建立实际的连接
conn.connect();
// 获取所有响应头字段
Map> map = conn.getHeaderFields();
boolean flag = false;
String content = "";
for (String key : map.keySet()) {
// System.out.println(key + "--->" + map.get(key));
content += map.get(key) == null?"":map.get(key).toString().trim();
if(content.indexOf("200")>0){
flag = true;
break;
}
}
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),"utf8"));
String line;
while ((line = in.readLine()) != null) {
result += line;
}
if(result!=null&&result.indexOf("errcode\":\"0000")>0){return true;}
} catch (Exception e) {
e.printStackTrace();
}
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
}
}
return false;
}
}