写点什么

http 请求压测工具

用户头像
潜默闻雨
关注
发布于: 2020 年 07 月 21 日



public class StressTesting {
long costTimes[] = null;
int nums = -1;
volatile boolean isEmpty = false;
// 发送http请求
public void sendRequest(String url) {
URL destUrl = null;
HttpURLConnection connection = null;
try {
destUrl = new URL(url);
connection = (HttpURLConnection) destUrl.openConnection();
connection.setDoOutput(true); // 设置该连接是可以输出的
connection.setRequestMethod("GET"); // 设置请求方式
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String line = null;
StringBuilder result = new StringBuilder();
while ((line = br.readLine()) != null) { // 读取数据
result.append(line + "\n");
}
connection.disconnect();
// System.out.println("请求返回结果:"+result.toString());
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != connection) {
connection.disconnect();
}
}
}
public void stressTest(String url,int times,int threadNum) {
nums = times-1;
costTimes = new long[times];
Lock lock = new ReentrantLock();
for(int i = 0; i < threadNum; i++) {
new Thread() {
@Override
public void run() {
while(true) {
lock.lock();
if(nums < 0) {
lock.unlock();
isEmpty = true;
break;
}
int tmp = nums;
nums--;
lock.unlock();
long start = System.currentTimeMillis();
sendRequest(url);
long cost = System.currentTimeMillis() - start;
costTimes[tmp]=cost;
}
}
}.start();
}
}
public void stressTestWIthForkJoinPool(String url,int times,int threadNum) {
nums = times-1;
costTimes = new long[times];
Lock lock = new ReentrantLock();
ForkJoinPool forkJoinPool = new ForkJoinPool(threadNum);
forkJoinPool.execute(() -> IntStream.rangeClosed(1, threadNum).parallel().forEach(i -> {
while(true) {
lock.lock();
if(nums < 0 && isEmpty == false) {
lock.unlock();
isEmpty = true;
break;
}
int tmp = nums;
nums--;
lock.unlock();
long start = System.currentTimeMillis();
sendRequest(url);
long cost = System.currentTimeMillis() - start;
costTimes[tmp]=cost;
}
}));
forkJoinPool.shutdown();
}
public void stressTestWithThreadPool(String url,int times,int threadNum) {
ScheduledExecutorService scheduledThreadPool = Executors.newScheduledThreadPool(10);
nums = times-1;
costTimes = new long[times];
Lock lock = new ReentrantLock();
for(int i = 0; i < threadNum; i++) {
scheduledThreadPool.schedule(new Runnable(){
@Override
public void run() {
while(true) {
lock.lock();
if(nums < 0) {
lock.unlock();
isEmpty = true;
break;
}
int tmp = nums;
nums--;
lock.unlock();
long start = System.currentTimeMillis();
sendRequest(url);
long cost = System.currentTimeMillis() - start;
costTimes[tmp]=cost;
}
}
}, 0, TimeUnit.SECONDS);
}
scheduledThreadPool.shutdown();
}
public void timeConsumingCaculate() {
if(costTimes == null) {
return;
}
quickSortAsc(costTimes,0,costTimes.length - 1);
// 平均耗时
long avg = 0L;
for(int i = 0; i < costTimes.length; i++) {
avg += costTimes[i];
}
int t = (costTimes.length/100)*95;
System.out.println("请求平均耗时:"+avg/costTimes.length+"ms");
System.out.println("95%响应时间:"+costTimes[t-1]+"ms");
}
public void printTimeConsuming() {
while(true) {
if(this.isEmpty) {
this.timeConsumingCaculate();
break;
}
}
}
public void quickSortAsc(long array[],int low, int high) {
int p_pos, i;
long pivot, t;
if(low < high) {
p_pos = low;
pivot = array[p_pos];
for(i = low + 1; i <= high; i++) {
if(array[i] < pivot) {
p_pos++;
t = array[p_pos];
array[p_pos] = array[i];
array[i] = t;
}
}
t = array[low];
array[low] = array[p_pos];
array[p_pos] = t;
quickSortAsc(array, low, p_pos - 1);
quickSortAsc(array, p_pos + 1, high);
}
}
public static void main(String[] args) {
StressTesting stress = new StressTesting();
String url = "https://www.baidu.com";
int times = 100;
int threadNum = 10;
stress.stressTest(url, times, threadNum);
// stress.stressTestWIthForkJoinPool(url, times, threadNum);
// stress.stressTestWithThreadPool(url, times, threadNum);
stress.printTimeConsuming();
}
}

。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。

用户头像

潜默闻雨

关注

还未添加个人签名 2018.11.23 加入

还未添加个人简介

评论 (1 条评论)

发布
用户头像
请用代码块编辑
2020 年 07 月 21 日 12:02
回复
没有更多了
http请求压测工具