架构师训练营 - 第⑦周命题作业

用户头像
牛牛
关注
发布于: 2020 年 07 月 19 日
架构师训练营 - 第⑦周命题作业



性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?



  • 一开始,响应时间几乎保持不变或缓慢增加,吞吐量快速提升,这时系统的软硬件性能利用率逐渐提高;

  • 随着并发压力继续增大,当系统的某项或多项指标达到安全临界点时,由于资源请求冲突增加,导致系统运行效率下降。响应时间快速增加,导致吞吐量下降;

  • 压力继续增大会导致系统宕机不可用



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

package performance;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
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;
/**
* @description: 性能测试工具
* @author: liu.w
* @create: 2020-07-17 11:19
**/
public class PerformanceTest {
private static final String HTTP_WWW_BAIDU_COM_CN = "http://www.baidu.com.cn";
public static void main(String[] args) {
List<Long> data = runTask(10, 100);
System.out.println("avg: " + data.stream().mapToLong(Long::intValue).average().getAsDouble());
Collections.sort(data);
int index = (int) (data.size() * 0.95) - 1;
System.out.println("95%: " + data.get(index));
}
/**
* @param threadCount 线程数
* @param times 执行次数
* @return
*/
private static List<Long> runTask(int threadCount, int times) {
ExecutorService service = Executors.newFixedThreadPool(15);
final List<Long> data = Collections.synchronizedList(new ArrayList<>());
final CountDownLatch cdOrder = new CountDownLatch(1);
final CountDownLatch cdAnswer = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
service.submit(() -> {
try {
cdOrder.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("线程" + Thread.currentThread().getName() + "开始执行");
for (int j = 0; j < times; j++) {
long beginTime = System.currentTimeMillis();
try {
URL url = new URL(HTTP_WWW_BAIDU_COM_CN);
HttpURLConnection urlcon = (HttpURLConnection) url.openConnection();
urlcon.connect(); //获取连接
if (200 == urlcon.getResponseCode()) {
InputStream is = urlcon.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(is));
StringBuffer bs = new StringBuffer();
String l = null;
while ((l = buffer.readLine()) != null) {
bs.append(l).append("/n");
}
//System.out.println(bs.toString());
long t = System.currentTimeMillis() - beginTime;
data.add(t);
//System.out.println("总共执行时间为:" + t + "毫秒");
} else {
System.out.println("执行失败code=" + urlcon.getResponseCode());
}
} catch (IOException e) {
e.printStackTrace();
}
}
cdAnswer.countDown();
System.out.println("线程" + Thread.currentThread().getName() + "结束");
});
}
cdOrder.countDown();
try {
cdAnswer.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
service.shutdown();
System.out.println("总共执行成功次数: " + data.size());
return data;
}
}



执行结果:

总共执行成功次数: 1000

avg: 38.039

95%: 40



用户头像

牛牛

关注

还未添加个人签名 2018.02.27 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 - 第⑦周命题作业