写点什么

架构师训练营 第七周【作业】

用户头像
小K
关注
发布于: 2020 年 07 月 22 日

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

随着并发增加,tps 如下图 1:

  • b 点之前基本是线性增长,是性能测试阶段,目的是为了测试在一定资源消耗的情况下,并发数和 tps 能不能满足既定要求。

  • b-->c 点之间 tps 增长趋势减弱,曲线逐渐平缓,这是负载测试阶段,目的是为了测试出系统的最大 tps 值是 c 点多少,已确认系统的最大处理能力。

  • c-->d 点 tps 急剧减小,因为并发压力超过了系统的最大负载,tps 不增反降,这个测试的目的是,确认系统的崩溃点 d 是多少,已确认系统的故障点。

随着并发数增加,系统响应时间如下图 2:

  • b 点之前,对应图 1 的性能测试阶段,系统有足够的性能冗余,所以响应时间一直很小。

  • b-->c 之间,随着并发数的增加,系统资源占用逐渐到达最高点,这个过程,虽然系统依然可以正常响应,但随着系统资源的占满,响应时间会缓慢边长。

  • c-->d 之间,系统满载并持续增加并发压力,任务堆积抢夺资源,响应时间会急剧增加,直到 d 点崩溃,系统不再响应请求。


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

import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.ThreadPoolExecutor;
import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;
public class PerformanceTest {
public static void main(String[] args) throws ClientProtocolException, IOException, InterruptedException { int requestCount=100;//请求总次数 int concurrency =10;//并发数 String url="https://www.baidu.com/"; ExecutorService executorService = Executors.newFixedThreadPool(concurrency); //创建线程池 List resultList = Collections.synchronizedList(new ArrayList<Long>());//创建线程安全的list
//创建局部内部类 class RunRequest implements Runnable{ @Override public void run() { CloseableHttpClient cClient=null; CloseableHttpResponse cResponse=null; try { Thread.sleep((long)(2000*Math.random()));//请求随机间隔,以防请求过快 long startTime=System.currentTimeMillis();//记录开始时间 cClient=HttpClients.createDefault();//创建HttpClient实例 cResponse = cClient.execute(new HttpGet(url));//发起请求 if(200==cResponse.getStatusLine().getStatusCode()) { long endTime=System.currentTimeMillis();//记录结束时间 resultList.add(endTime-startTime);//计算本地请求的时间间隔,并存入集合 } } catch (Exception e) { e.printStackTrace(); }finally { try { cResponse.close(); cClient.close(); } catch (IOException e) { e.printStackTrace(); } } } } RunRequest runR=new RunRequest(); for(int i=0;i<requestCount;i++) { executorService.execute(runR);//利用线程池中的线程运行Runnable方法 } //重复检查是否所有的线程都运行完毕 while(true) { if(((ThreadPoolExecutor)executorService).getActiveCount()==0) { System.out.println("所有的子线程都结束了!"); executorService.shutdown(); break; } Thread.sleep(1000); } System.out.println("成功的请求数量:"+resultList.size()); resultList.sort(Comparator.comparingLong(Long::longValue));//集合排序 long sum=0;//计算响应时间总和 for(int i=0;i<resultList.size();i++) { sum+=(long)(resultList.get(i)); } System.out.println("平均响应时间:"+sum/resultList.size()+"ms"); System.out.println("95%响应时间:"+resultList.get(50)+"ms"); }}
复制代码

输出结果如下:

所有的子线程都结束了!成功的请求数量:100平均响应时间:269ms95%响应时间:177ms
复制代码


用户头像

小K

关注

还未添加个人签名 2019.11.08 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营 第七周【作业】