1、性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
吞吐量变化如下图:
随着并发数据量增加,吞吐量不断上升;当系统资源达到高峰后,并发数量还在增加的话,服务器处理不过来了,吞吐量会逐渐下降,系统崩溃。
响应时间变化图:
服务器资源空闲时,响应时间较短,随着并发用户数不断增加,系统资源逐渐消耗。请求多了,服务器处理不过来,响应时间会慢慢边长。此时,并发量还在不断增加,达到系统最大负载点,也就是服务器资源消耗尽了,彻底失去响应。
2、用你熟悉的编程语言写一个 Web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
public class PerformanceTest {
public static void main(String[] args) throws InterruptedException {
PerformanceTest performanceTest = new PerformanceTest();
performanceTest.testWebsite("https://www.baidu.com", 10, 100);
}
public void testWebsite(String site, Integer concurrency, Integer requestNum) throws InterruptedException {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNamePrefix("performance-test").build();
ExecutorService executorService = new ThreadPoolExecutor(concurrency, 200,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(1024), namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
List<Long> responseTime = new ArrayList<>();
CountDownLatch countDownLatch = new CountDownLatch(concurrency);
int turn = requestNum / concurrency;
for (int x = 0; x < concurrency; x++) {
executorService.execute(() -> {
for (int i = 0; i < turn; i++) {
responseTime.add(request(site));
}
countDownLatch.countDown();
});
}
countDownLatch.await();
if (countDownLatch.getCount() == 0) {
System.out.println("网站:" + site + "并发数:" + concurrency + ",请求数:" + requestNum+",性能测试开始");
printResult(responseTime);
System.out.println("网站:" + site + "并发数:" + concurrency + ",请求数:" + requestNum+",性能测试结束");
}
executorService.shutdown();
}
public void printResult(List<Long> responseTime) {
responseTime = responseTime.stream().filter(item -> item > -1).sorted().collect(Collectors.toList());
System.out.println(responseTime);
long maxTime = responseTime.stream().mapToLong(Long::intValue).max().orElse(-1);
long minTime = responseTime.stream().mapToLong(Long::intValue).min().orElse(-1);
double avg = responseTime.stream().mapToLong(Long::intValue).average().orElse(-1);
System.out.println("最大响应时间:" + maxTime);
System.out.println("最小响应时间:" + minTime);
System.out.println("平均响应时间:" + avg);
Double index = responseTime.size() * 0.95;
System.out.println("95%响应时间:" + responseTime.get(index.intValue()));
}
public Long request(String url) {
long current = System.currentTimeMillis();
HttpResponse response = HttpRequest.get(url).execute();
if (response.getStatus() == HttpStatus.HTTP_OK) {
return System.currentTimeMillis() - current;
}
return -1L;
}
}
测试结果:
网站:https://www.baidu.com并发数:10,请求数:100,性能测试开始
[209, 209, 210, 210, 211, 212, 213, 213, 214, 215, 215, 215, 216, 217, 218, 218, 218, 219, 219, 220, 222, 222, 222, 223, 224, 225, 228, 234, 245, 245, 245, 246, 246, 247, 249, 249, 250, 250, 250, 250, 251, 251, 251, 251, 252, 252, 252, 253, 253, 254, 254, 254, 255, 255, 256, 256, 256, 256, 257, 257, 258, 259, 260, 260, 261, 261, 262, 262, 263, 264, 265, 265, 266, 267, 267, 268, 270, 271, 271, 275, 279, 279, 282, 288, 296, 298, 298, 299, 304, 335, 689, 691, 691, 692, 692, 694, 698, 699, 990, 998]
最大响应时间:998
最小响应时间:209
平均响应时间:299.26
95%响应时间:694
网站:https://www.baidu.com并发数:10,请求数:100,性能测试结束
平均响应时间: 所有请求的平均响应时间,取的平均值
95%响应时间: 统计学术语,如果将一组数据从小到大排序,并计算相应的累计百分位,则某一百分位所对应数据的值就称为这一百分位的百分位数。可表示为:一组n个观测值按数值大小排列。如,处于p%位置的值称第p百分位数
评论