第七周作业 - 命题作业
发布于: 2020 年 07 月 21 日
1.用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
StressTest.java
package com.imolly.stresstest;import java.util.*;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.util.concurrent.Semaphore;/** * 压力测试工具类 */public class StressTest { private int requestTotal; //请求总数 private int threadTotal; //并发数 private String requestUrl; //请求Url private List<Long> responseTimes = new ArrayList<Long>(); public StressTest(int requestTotal, int threadTotal, String requestUrl) { this.requestTotal = requestTotal; this.threadTotal = threadTotal; this.requestUrl = requestUrl; } public StressTestResult run() throws Exception { ExecutorService executorService = Executors.newCachedThreadPool(); final Semaphore semaphore = new Semaphore(threadTotal); final CountDownLatch countDownLatch = new CountDownLatch(requestTotal); for (int i = 0; i < requestTotal; i++) { executorService.execute(() -> { try { semaphore.acquire(); runTask(); semaphore.release(); } catch (Exception e) { e.printStackTrace(); } countDownLatch.countDown(); }); } countDownLatch.await(); executorService.shutdown(); return getStressTestResult(); } private void runTask() { long startTime = System.currentTimeMillis(); HttpClientUtil.doGet(requestUrl); long endTime = System.currentTimeMillis(); responseTimes.add((endTime - startTime)); } private StressTestResult getStressTestResult(){ Collections.sort(responseTimes); long totalResponseTime = responseTimes.stream().mapToLong(Long::longValue).sum(); OptionalDouble optionalDouble = responseTimes.stream().mapToLong(Long::longValue).average(); StressTestResult result = new StressTestResult(); result.setRequestTotal(requestTotal); result.setThreadTotal(threadTotal); result.setTotalRequestTime(totalResponseTime); result.setAvgResponseTime((long)optionalDouble.getAsDouble()); result.setResponseTime95(responseTimes.get(94)); return result; }}
StressTestResult.java
package com.imolly.stresstest;/** * 压测结果信息 */public class StressTestResult { private int requestTotal; //请求总数 private int threadTotal; //并发数 private long totalRequestTime; //总响应时间,ms private long avgResponseTime; //平均响应时间,ms private long responseTime95; //95% 响应时间,ms public int getRequestTotal() { return requestTotal; } public void setRequestTotal(int requestTotal) { this.requestTotal = requestTotal; } public int getThreadTotal() { return threadTotal; } public void setThreadTotal(int threadTotal) { this.threadTotal = threadTotal; } public long getTotalRequestTime() { return totalRequestTime; } public void setTotalRequestTime(long totalRequestTime) { this.totalRequestTime = totalRequestTime; } public long getAvgResponseTime() { return avgResponseTime; } public void setAvgResponseTime(long avgResponseTime) { this.avgResponseTime = avgResponseTime; } public long getResponseTime95() { return responseTime95; } public void setResponseTime95(long responseTime95) { this.responseTime95 = responseTime95; } public void print(){ System.out.println("请求总次数:" + requestTotal + " 次"); System.out.println("并发数:" + threadTotal + " 个"); System.out.println("总响应时间:" + totalRequestTime + " ms"); System.out.println("平均响应时间:" + avgResponseTime + " ms"); System.out.println("95%响应时间:" + responseTime95 + " ms"); }}
HttpClientUtil.java
package com.imolly.stresstest;import java.io.*;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;/** * HttpClient工具类 */public class HttpClientUtil { public static String doGet(String httpurl) { HttpURLConnection connection = null; InputStream is = null; BufferedReader br = null; String result = null; try { URL url = new URL(httpurl); connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(15000); connection.setReadTimeout(60000); connection.connect(); if (connection.getResponseCode() == 200) { is = connection.getInputStream(); br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer sbf = new StringBuffer(); String temp = null; while ((temp = br.readLine()) != null) { sbf.append(temp); sbf.append("\r\n"); } result = sbf.toString(); } } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (null != br) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } connection.disconnect(); } return result; }}
StressTestTest.java
package com.imolly.stresstest;import org.junit.Test;/** * 压力测试工具的测试类 */public class StressTestTest { @Test public void testRun(){ try { StressTest stressTest = new StressTest(100,10,"https://www.baidu.com"); StressTestResult result = stressTest.run(); if(result != null){ result.print(); } } catch (Exception e) { e.printStackTrace(); } }}
运行结果:
请求总次数:100 次并发数:10 个总响应时间:4872 ms平均响应时间:48 ms95%响应时间:339 ms
划线
评论
复制
发布于: 2020 年 07 月 21 日阅读数: 46
molly
关注
还未添加个人签名 2017.12.14 加入
还未添加个人简介
评论