写点什么

架构师训练营第七周作业

用户头像
听夜雨
关注
发布于: 2020 年 11 月 08 日

作业一

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

对于响应时间

  1. 当并发数上升的时,资源(线程数、数据库连接数、CPU、内存)使用未满时,响应时间保持稳定不变

  2. 当并发数继续上升时,因系统并发导致资源等待、任务调度、CPU 等待等,任务处理时长增加,响应时间会缓慢上升

  3. 当并发继续上升时,因资源不足,无法响应请求,响应时间会快速上升,达到系统崩溃点


对于吞吐量

  1. 当并发数上升的时,吞吐量会达到线性增加

  2. 当并发数继续上升时,吞吐量上升趋势慢慢变缓

  3. 当并发继续上升时,吞吐量下降趋势慢慢增大


作业二

压测工具代码如下:

设置线程池,使用 cando 变量控制是否进行请求。

平均响应为 每次时长之和/次数

95%响应为排序后取第 95%位数

import java.math.BigInteger;

import java.util.Arrays;

import java.util.concurrent.ExecutorService;

import java.util.concurrent.Executors;


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 yace {

private static boolean cando = false;

private static volatile int threadCount = 0;

private static int totalCount = 0;

private static int [] duetime;


public static void main(String[] args) {

int total = 100;

yace yc = new yace();

yace.setTotalCount(total);

ExecutorService fixedThreadPool = Executors.newFixedThreadPool(100);

String url = "http://www.baidu.com";

for (int i = 0; i < total; i++) {

fixedThreadPool.execute(new Runnable() {

public void run() {

try {

yc.doGet(url);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

});

}

yace.setCando(true);

}


public void doGet(String testUrl) throws Exception {

HttpGet httpGet = new HttpGet(testUrl);

CloseableHttpClient httpclient = HttpClients.createDefault();


CloseableHttpResponse response = null;

while (!cando) {

}

long start = System.currentTimeMillis();

// 执行请求

response = httpclient.execute(httpGet);

long end = System.currentTimeMillis();

httpclient.close();

threadCount++;

// 判断返回状态是否为 200

if (200 == response.getStatusLine().getStatusCode()) {

duetime[threadCount - 1] += (end - start);

} else {

duetime[threadCount - 1] += 999999;

System.out.println("request error"+response.getStatusLine().getStatusCode());

}

if(threadCount == totalCount) {

System.out.println(getAvgTime());

System.out.println(get95AvgTime());

}

}

public static long getAvgTime(){

long totalDuetime = 0;

for(int i = 0; i < totalCount; i++) {

totalDuetime += duetime[i];

}

return totalDuetime / totalCount;

}

public static long get95AvgTime(){

Arrays.sort(duetime);

return duetime[duetime.length/100*95];

}


public static boolean isCando() {

return cando;

}


public static void setCando(boolean cando) {

yace.cando = cando;

}


public static int getTotalCount() {

return totalCount;

}


public static void setTotalCount(int totalCount) {

yace.totalCount = totalCount;

duetime = new int[totalCount];

}

}


用户头像

听夜雨

关注

还未添加个人签名 2020.08.19 加入

还未添加个人简介

评论

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