第七周·命题作业·写 web 性能压测工具

发布于: 12 小时前
第七周·命题作业·写 web 性能压测工具

要求:用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。



@Slf4j
public class PerformanceTest1 implements Runnable {
private List<ConcurrentResponse> responseList;
private int cycleIndex, concurrentIndex;
private PerformanceTest1(List<ConcurrentResponse> responseList, int cycleIndex, int concurrentIndex) {
this.responseList = responseList;
this.cycleIndex = cycleIndex;
this.concurrentIndex = concurrentIndex;
}
public static void main(String[] args) throws Exception {
int cycleTimes = 10;//循环次数
int concurrentTimes = 10;//同时并发次数
List<ConcurrentResponse> responseList = new ArrayList<>();
List<Thread> threadList = new ArrayList<>();
//循环次数
for (int i = 0; i < cycleTimes; i++) {
//并发执行线程个数
for (int j = 0; j < concurrentTimes; j++) {
try {
// 多线程同时执行
Thread thread = new Thread(new PerformanceTest1(responseList, i, j));
threadList.add(thread);
thread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
for (Thread thread : threadList) {
thread.join();
}
responseList = responseList.stream()
.sorted(Comparator.comparing(ConcurrentResponse::getResponseTime))
.collect(Collectors.toList());
double responseTimeSum = responseList.stream().mapToDouble(ConcurrentResponse::getResponseTime).sum();
log.info("平均响应时间:{} | 95% 响应时间:{}", responseTimeSum / 100.0, responseList.get(94).getResponseTime());
}
@Override
public void run() {
String url = "http://www.baidu.com";
try {
Date start = new Date();
HttpUtil.doGet(url);
Date end = new Date();
ConcurrentResponse obj = ConcurrentResponse.builder().responseTime((end.getTime() - start.getTime()) / 1000.0)
.cycleIndex(this.cycleIndex).concurentIndex(this.concurrentIndex).build();
responseList.add(obj);
} catch (Exception e) {
e.printStackTrace();
}
}
}



用户头像

刘璐

关注

还未添加个人签名 2018.03.29 加入

还未添加个人简介

评论

发布
暂无评论
第七周·命题作业·写 web 性能压测工具