架构师训练营 - 命题作业 第 7 周
发布于: 2020 年 07 月 22 日
1. 性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
响应时间在最佳运行区间略有上升;在负载测试区间逐渐上升;在压力测试区间迅速上升直至崩溃点。
吞吐量在最佳运行区间迅速上升;在负载测试区间缓慢上升;在压力测试区间下降直至崩溃点。
2. 用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
package test;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class RequestThread implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(RequestThread.class);
private CountDownLatch countDownLatch;
private String requestUrl;
private ConcurrentLinkedQueue<Long> queue;
public RequestThread(String requestUrl, CountDownLatch countDownLatch, ConcurrentLinkedQueue<Long> queue) {
this.requestUrl = requestUrl;
this.countDownLatch = countDownLatch;
this.queue = queue;
}
public void run() {
try {
long startTime = System.currentTimeMillis();
boolean isSuccess = HttpClientUtil.get(requestUrl, null);
if (!isSuccess) {
LOG.error("response error.....threadName:" + Thread.currentThread().getName());
}
long endTime = System.currentTimeMillis();
queue.add(endTime-startTime);
countDownLatch.countDown();
} catch (Exception e) {
LOG.error("requset error.....threadName:" + Thread.currentThread().getName());
}
}
}
复制代码
package test;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class StressTest {
private Integer threadCount = 0;
private String requestUrl = null;
private Integer requestCount = 0;
private ExecutorService ThreadPool = null;
private ConcurrentLinkedQueue<Long> queue = null;
public static void main(String[] args) throws Exception {
System.out.println("start execute.....");
StressTest st = new StressTest();
st.init(10,"http://www.baidu.com",100);
st.exec();
st.printResult();
}
public void init(int threadCount, String requestUrl, int requestCount) throws IOException {
this.threadCount = threadCount;
this.requestCount = requestCount;
this.requestUrl = requestUrl;
ThreadPool = Executors.newFixedThreadPool(this.threadCount);
queue = new ConcurrentLinkedQueue<Long> ();
}
public void exec() throws InterruptedException {
CountDownLatch requestCountDown = new CountDownLatch(requestCount);
for (int i = 0; i < requestCount; i++) {
RequestThread requestThread = new RequestThread(requestUrl, requestCountDown, queue);
ThreadPool.execute(requestThread);
}
requestCountDown.await();
}
public void printResult() {
int size = queue.size();
Long[] times = new Long[size];
queue.toArray(times);
Arrays.parallelSort(times);
long time100 = 0;
int size95 = (int)(times.length*0.95);
long time95 = 0;
for (int i = 0; i < times.length; i++) {
if(i<size95)
time95 += times[i];
time100 += times[i];
}
System.out.println("平均响应时间(毫秒):"+ time100/size);
System.out.println("95% 响应时间(毫秒):"+ time95/size95);
}
}
复制代码
start execute.....
平均响应时间:448
95% 响应时间:312
复制代码
划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 35
版权声明: 本文为 InfoQ 作者【铁血杰克】的原创文章。
原文链接:【http://xie.infoq.cn/article/1610e49f790e95fb4b033608a】。未经作者许可,禁止转载。
铁血杰克
关注
还未添加个人签名 2017.12.18 加入
还未添加个人简介
评论 (3 条评论)