写点什么

第七章作业

用户头像
小胖子
关注
发布于: 2020 年 07 月 19 日



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

压测开始后,响应时间缓慢增长,吞吐量线性增长,当达到各项系统指标达到安全临界值时,资源已经呈饱和状态,继续施压性能不升反降,响应时间大幅增长,吞吐量逐渐下降,直至系统崩溃。



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



import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
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;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.stream.Collectors;

public class StressTest {
public static void main(String[] args) {
//并发10,请求1000次
pressTest("http://www.baidu.com", 10, 1000);
//并发100,请求1000次
pressTest("http://www.baidu.com", 100, 1000);
}

private static void pressTest(String uri, int concurrentCount, int reqCount) {
ExecutorService executor = Executors.newFixedThreadPool(concurrentCount);
List<Long> responseTimeStats = Collections.synchronizedList(Lists.newLinkedList());
int i = 0;
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(concurrentCount);
connManager.setDefaultMaxPerRoute(concurrentCount);
CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(connManager).build();
CountDownLatch latch = new CountDownLatch(reqCount);
while (i++ < reqCount) {
executor.submit(() -> {
HttpGet method = new HttpGet(uri);
CloseableHttpResponse httpResponse = null;
try {
long start = System.currentTimeMillis();
httpResponse = httpClient.execute(method);
long responseTime = System.currentTimeMillis() - start;
responseTimeStats.add(responseTime);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (httpResponse != null) {
try {
EntityUtils.consume(httpResponse.getEntity());
} catch (IOException e) {
e.printStackTrace();
}
}
}
latch.countDown();
});
}
try {
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
int responseCount = responseTimeStats.size();
long average = responseTimeStats.stream().mapToLong(Long::longValue).sum() / responseCount;
List<Long> reversedSortedResponseTimeStats = responseTimeStats.stream().sorted(Comparator.comparing(Long::longValue).reversed()).collect(Collectors.toList());
List<Long> responseTimeStats95 = reversedSortedResponseTimeStats.subList((int) (responseCount * 0.05), responseCount - 1);
System.out.println("并发数:" + concurrentCount + ",请求次数:" + reqCount + ",平均响应时间:" + average + ",95%响应时间:" + responseTimeStats95.get(0));
// System.out.println("响应时间明细:" + JSON.toJSONString(reversedSortedResponseTimeStats));
}
}





用户头像

小胖子

关注

还未添加个人签名 2018.02.04 加入

还未添加个人简介

评论

发布
暂无评论
第七章作业