第七周 性能优化(一) 作业 「架构师训练营 3 期」
发布于: 2021 年 01 月 10 日
用你熟悉的编程语言写一个 Web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测
题 2 压测代码如下:
package com.hyf.test;
import cn.hutool.http.HttpUtil;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* @author hyf
* @date 2021/1/10
*/
public class RequestTest {
public static List<Long> dataList = new ArrayList<>();
private ExecutorService executor = Executors.newFixedThreadPool(20);
/**
* 输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测
*
* @param url 请求地址
* @param userNum 并发用户数
* @param times 请求次数
* @return
*/
public Map test(String url, int userNum, int times) {
HashMap<String, Object> map = new HashMap<>();
CountDownLatch countDownLatch = new CountDownLatch(userNum);
for (int i = 0; i < userNum; i++) {
executor.execute(new TestThread(times, url, countDownLatch));
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("异常了!");
}
// while (true) {
// if (dataList.size() == userNum * times) {
// break;
// }
// System.out.println(dataList.size());
// }
Collections.sort(dataList);
int errorNum = 0;
int rightNum = 0;
int percent95Num = (int) (userNum * times * 0.95);
Long p95Time = 0L;
Long allTime = 0L;
for (int i = 0; i < dataList.size(); i++) {
Long v = dataList.get(i);
if (v < 0) {
errorNum++;
continue;
}
rightNum++;
if (rightNum == percent95Num) {
p95Time = v;
}
allTime += v;
}
long avgTime = allTime / rightNum;
map.put("avgTime", avgTime);
map.put("p95Time", p95Time);
map.put("errorNum", errorNum);
map.put("rightNum", rightNum);
return map;
}
public static void main(String[] args) {
RequestTest requestTest = new RequestTest();
Map test = requestTest.test("https://www.baidu.com", 10, 100);
System.out.println(RequestTest.dataList.toString());
System.out.println(test.toString());
}
}
class TestThread implements Runnable {
private int times;
private String url;
private CountDownLatch countDownLatch;
public TestThread(int times, String url, CountDownLatch countDownLatch) {
this.times = times;
this.url = url;
this.countDownLatch = countDownLatch;
}
public int getTimes() {
return times;
}
public void setTimes(int times) {
this.times = times;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public CountDownLatch getCountDownLatch() {
return countDownLatch;
}
public void setCountDownLatch(CountDownLatch countDownLatch) {
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
for (int i = 0; i < times; i++) {
testHttp(url);
}
countDownLatch.countDown();
}
private void testHttp(String url) {
try {
long startTime = System.currentTimeMillis();
HttpUtil.get(url, 3000);
long endTime = System.currentTimeMillis();
long useTime = endTime - startTime;
RequestTest.dataList.add(useTime);
} catch (Exception e) {
RequestTest.dataList.add(-1L);
e.printStackTrace();
}
}
}
复制代码
题 2 压测结果如下图:
划线
评论
复制
发布于: 2021 年 01 月 10 日阅读数: 15
feiyun123
关注
还未添加个人签名 2019.09.28 加入
还未添加个人简介
评论