写点什么

自制简单 WEB 压测工具

发布于: 2020 年 07 月 22 日
自制简单WEB压测工具

压测工具

自制了一个简单的WEB性能压力工具,主要是通过线程池来并发的发起请求,然后计算这次请求的平均响应时间和95响应时间。

使用

package cn.qiangjun.client;
/**
*
* @Author: 梅子黄时雨 qiangjun@aliyun.com
* @Date: 2020/7/21 13:59
*/
public class Main {
public static void main(String[] args) {
HttpHandler.httpGetSender("http://www.baidu.com",10,100);
DataAnalysisHandler.showDataAnalysisResult(HttpHandler.REQUEST_SIMPLE_INFO_LIST);
}
}

发送请求

package cn.qiangjun.client;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;
import java.io.IOException;
import java.util.List;
import java.util.concurrent.*;
/**
* HTTP处理器
*
* @Author: 梅子黄时雨 qiangjun@aliyun.com
* @Date: 2020/7/21 13:57
*/
public class HttpHandler {
private static Logger log = Logger.getLogger(HttpHandler.class);
protected static final List<RequestSimpleInfo> REQUEST_SIMPLE_INFO_LIST = new CopyOnWriteArrayList<>();
/**
* 发送请求
* 1. 设置线程池大小(请求总数、并发数)
* 2. 设置发送的请求信息(URL)
* 3. 多线程发送请求
* 4. 收集数据到内存中(每次请求的时间,请求结束时间戳精确到毫秒)
*
* @param url 访问地址
* @param concurrencyLevel 并发数
* @param requests 总请求数
*/
public static void httpGetSender(String url, int concurrencyLevel, int requests) {
int maximumPoolSize = 2 * concurrencyLevel;
long keepAliveTime = 10;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(requests);
ThreadPoolExecutor httpThreadPool = new ThreadPoolExecutor(concurrencyLevel,
maximumPoolSize,
keepAliveTime,
TimeUnit.SECONDS,
workQueue);
for (int i = 0; i < requests; i++) {
httpThreadPool.execute(()->{
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
try {
long start = System.currentTimeMillis();
httpClient.execute(httpGet);
long end = System.currentTimeMillis();
long duration = end - start;
REQUEST_SIMPLE_INFO_LIST.add(new RequestSimpleInfo(start,end,duration));
} catch (IOException e) {
log.error("error http get" + e.getMessage());
}
});
}
httpThreadPool.shutdown();
while (true) {
if (httpThreadPool.isTerminated()) {
return;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
log.error("sleep error",e);
}
}
}
}

计算平均响应时间和95响应时间

package cn.qiangjun.client;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
/**
* 性能数据分析处理器
*
* @Author: 梅子黄时雨 qiangjun@aliyun.com
* @Date: 2020/7/21 13:58
*/
public class DataAnalysisHandler {
/**
* 展示请求的
* 1. 平均响应时间
* 2. 95%响应时间
*
* @param requestSimpleInfos
*/
public static void showDataAnalysisResult(List<RequestSimpleInfo> requestSimpleInfos) {
int requestCount = requestSimpleInfos.size();
Double avgRequestDuration = requestSimpleInfos.stream().
map(RequestSimpleInfo::getRequestDuration).
collect(Collectors.averagingLong(Long::longValue));
List<RequestSimpleInfo> requestSimpleInfosSorted = requestSimpleInfos.stream().
sorted(Comparator.comparing(RequestSimpleInfo::getRequestDuration)).
collect(Collectors.toList());
int index95 = (int) (0.95 * requestCount);
if (0 == index95) {
index95 = 1;
}
long requestDuration95 = requestSimpleInfosSorted.get(index95).getRequestDuration();
System.out.println("请求平均响应时间:" + avgRequestDuration + "ms,95响应时间:" + requestDuration95 + "ms");
}
}



发布于: 2020 年 07 月 22 日阅读数: 50
用户头像

还未添加个人签名 2017.10.30 加入

半壁山房待明月,一盏清茗酬知音。

评论

发布
暂无评论
自制简单WEB压测工具