架构师训练营 - 第七周 - 作业
发布于: 2020 年 07 月 22 日
一.性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
1.性能测试阶段:系统并发访问数在系统的指定目标之内,系统硬件资源充足,此时系统的响应时间短,系统吞吐量未到达系统上线,所以吞吐量处于线性增长趋势,响应时间比较短,增长缓慢。
2.负载测试阶段:并发用户数递增,系统硬件资源利用率激增,到硬件资源到达一定临界点时,等待资源分配,并发请求不能及时处理,如果继续增加并发数,系统吞吐量增加缓慢,达到系统临界点后系统吞吐量开始降低,响应时间快速增长。
3.压力测试阶段:系统资源使用率到达饱和状态之后,继续增加并发量,此时系统吞吐量快速降低,直到系统压跨,响应时间延长,系统不再处理任何请求。
二.用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
package com.zztest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* @author
* @Title:null.java
* @Package PACKAGE_NAME
* @Description:
* @date 2020/07/22
*/
public class test {
public static final String url = "https://www.baidu.com";
public static final int requestCount = 100;
public static final int concurrentThreadCount = 10;
public static void main(String[] args) throws InterruptedException {
List<Long> responseTimeList = Collections.synchronizedList(new ArrayList<>());
ExecutorService executorService = Executors.newCachedThreadPool();
CountDownLatch countDownLatch = new CountDownLatch(requestCount);
Semaphore semaphore = new Semaphore(concurrentThreadCount);
for (int i = 0; i < requestCount; i++) {
executorService.execute(() -> {
try {
semaphore.acquire();
long startTime = System.currentTimeMillis();
CommonUtil.get(url);
long endTime = System.currentTimeMillis();
responseTimeList.add(endTime - startTime);
semaphore.release();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
countDownLatch.countDown();
});
}
countDownLatch.await();
executorService.shutdown();
BenchReport(responseTimeList);
}
private static void CountPercent95ReponseTime(List<Long> responseTimeList,double percent){
long percent95ResponseTime = 0;
Collections.sort(responseTimeList);
double px = percent * (responseTimeList.size() - 1);
int i = (int) Math.floor(px);
double g = px - i;
if (g == 0) {
percent95ResponseTime = responseTimeList.get(0);
} else {
percent95ResponseTime = (long) ((1 - g) * responseTimeList.get(i) + g * responseTimeList.get(i + 1));
}
System.out.println("95%响应时间(毫秒):" + percent95ResponseTime);
}
private static void BenchReport(List<Long> timeList) {
long totalResponseTime = 0;
long averageResponseTime;
for (Long time : timeList) {
totalResponseTime += time;
}
averageResponseTime = Math.round(totalResponseTime / timeList.size());
System.out.println(url + "压测结束:");
System.out.println("总请求数:" + requestCount);
System.out.println("并发数:" + concurrentThreadCount);
System.out.println("平均响应时间(毫秒):" + averageResponseTime);
CountPercent95ReponseTime(timeList,0.95);
}
}
复制代码
package com.zztest;
import okhttp3.*;
import java.io.IOException;
/**
* @author
* @Title:null.java
* @Package PACKAGE_NAME
* @Description:
* @date 2020/07/22
*/
public class CommonUtil {
/**
* 给定特定的url,参数封装成一个map,get请求
* @param url
* @param params
* @return 响应字符串
*/
private static OkHttpClient client = new OkHttpClient();
public static String get(String url){
Request request = new Request.Builder()
.url(url)
.build();
try {
String res = client.newCall(request).execute().body().string();
return res;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
复制代码

划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 56
版权声明: 本文为 InfoQ 作者【狂奔嘀兔纸】的原创文章。
原文链接:【http://xie.infoq.cn/article/757057835dd834b5dfb009ffa】。文章转载请联系作者。

狂奔嘀兔纸
关注
还未添加个人签名 2018.04.28 加入
还未添加个人简介
评论