作业一:性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
随着并发压力的增加,系统的响应时间从平稳到逐步上升,当超过负载点后会激增,并最终导致系统崩溃;吞吐量起初会随着并发数增大,然后趋于平稳,当超过负载点后,吞吐量会下降,并最终导致系统崩溃。
这是由于起初在系统没有到达满负载的阶段,系统可以处于一个比较好和稳定的状态,当负责过大后,系统任意指标满负荷运作后,会导致大量请求无法及时处理,大量的堆积会耗尽资源,最终导致系统崩溃。
作业二:用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
代码如下:
PageViewRequestTask 负责执行 url 请求发送,并记录每个请求的响应时间。count 表示需要发送多少次请求。
public class PageViewRequestTask implements Runnable {
private String url;
private int count;
private CountDownLatch latch;
List<Long> responseTimeList = new ArrayList<>();
@Override
public void run() {
// send request one by one in one task
for (int i = 0; i < count; i++) {
try {
System.out.println("task [" + Thread.currentThread().getId() + "] is sending request[" + i + "]");
long responseTime = sendRequest(url);
// add response time to result
responseTimeList.add(responseTime);
} catch (IOException e) {
e.printStackTrace();
}
}
// count down when this task is finished
latch.countDown();
System.out.println("task [" + Thread.currentThread().getId() + "] is finished");
}
private long sendRequest(String url) throws IOException {
HttpClient httpClient = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(url);
long startTime = new Date().getTime();
// ignore error for this demo
HttpResponse response = httpClient.execute(httpGet);
//System.out.println("response status code: " + response.getStatusLine().getStatusCode());
return new Date().getTime() - startTime;
}
public PageViewRequestTask(String url, int count, CountDownLatch latch) {
this.url = url;
this.count = count;
this.latch = latch;
}
public List<Long> getResponseTimeList() {
return responseTimeList;
}
}
复制代码
StressTestLauncher 负责运行测试,它根据并发数创建线程,每个线程平均运行总请求数。最后打印 95%响应时间。
public class StressTestLauncher {
private static final String TEST_REQUEST_URL = "https://www.baidu.com";
private static final int TEST_REQUEST_COUNT = 100;
private static final int TEST_REQUEST_CONCURRENCY_TASK_COUNT = 10;
private static final int TEST_REQUEST_COUNT_PER_TASK = TEST_REQUEST_COUNT / TEST_REQUEST_CONCURRENCY_TASK_COUNT;
public static void main(String[] args) throws InterruptedException {
CountDownLatch latch = new CountDownLatch(TEST_REQUEST_CONCURRENCY_TASK_COUNT);
List<PageViewRequestTask> tasks = new ArrayList<>();
List<Thread> taskThreads = new ArrayList<>();
// create threads according to concurrency count
for (int i = 0; i < TEST_REQUEST_CONCURRENCY_TASK_COUNT; i++) {
PageViewRequestTask task = new PageViewRequestTask(TEST_REQUEST_URL, TEST_REQUEST_COUNT_PER_TASK, latch);
tasks.add(task);
taskThreads.add(new Thread(task));
}
// start running stress tests
for (Thread taskThread : taskThreads) {
taskThread.start();
}
// wait until all threads finish
latch.await();
System.out.println("Stress test is finished");
// merge all response results
List<Long> responseTimes = new ArrayList<>();
for (PageViewRequestTask task : tasks) {
responseTimes.addAll(task.getResponseTimeList());
}
// sort response time
responseTimes.sort(Long::compareTo);
// calculate index of 95% response time, and print out the result
int resultIndex = (int) Math.round(TEST_REQUEST_COUNT * 0.95) - 1;
System.out.println("95% response time is: " + responseTimes.get(resultIndex) + " ms");
}
}
复制代码
对以下网站进行了测试,每个网站发出 100 个请求,10 个并发:
95% response time is: 528 ms
95% response time is: 675 ms
95% response time is: 268 ms
评论