架构师训练营第七周作业
发布于: 2020 年 07 月 22 日
一、性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
随着并发压力的增加,
在最佳运行区间内,系统响应时间缓慢增加,吞吐量接近线性增长;
在最佳运行区间之后,系统最大负载点之前,系统响应时间增长较快,吞吐量增长变慢;
在系统最大负载点之后,系统响应时间显著增长,吞吐量逐步下降,一直到系统奔溃。
二、用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
import java.util.*;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.atomic.AtomicInteger;import java.util.concurrent.atomic.AtomicReference;public class BaiduTest { public static Map<String,Long> hashMap = new ConcurrentHashMap<>(); public static void main(String[] args) { long start = System.currentTimeMillis(); ExecutorService fixedThreadPool = Executors.newFixedThreadPool(10); for (int i = 0; i < 100; i++) { fixedThreadPool.execute(new Runnable() { @Override public void run() { long startTIme = System.currentTimeMillis(); String str = HttpClientUtil.httpGet("http://www.baidu.com"); long endTIme = System.currentTimeMillis(); long useTime = endTIme-startTIme; System.out.println("useTime"+useTime); hashMap.put(UUID.randomUUID().toString(),useTime); } }); } fixedThreadPool.shutdown(); while(true){ if(fixedThreadPool.isTerminated()){ System.out.println((System.currentTimeMillis() - start) + "ms"); System.out.println("所有的子线程都结束了!"); AtomicReference<Long> allTIme = new AtomicReference<>(0l); List<Long> nineFiveTIme = new ArrayList<>(); AtomicInteger i = new AtomicInteger(); hashMap.forEach((k,v)->{ allTIme.updateAndGet(v1 -> v1 + v); nineFiveTIme.add(v); }); nineFiveTIme.sort(Comparator.comparingLong(Long::longValue)); System.out.println("平均响应时间:"+allTIme.get()/100+"ms"); System.out.println("95%响应时间:"+nineFiveTIme.get(95)+"ms"); break; } } }}
划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 61
版权声明: 本文为 InfoQ 作者【James-Pang】的原创文章。
原文链接:【http://xie.infoq.cn/article/d261d687e267aef9c73dcfd15】。未经作者许可,禁止转载。
James-Pang
关注
不忘初心 2018.11.08 加入
还未添加个人简介
评论