第 7 周作业:web 性能测压工具
发布于: 2020 年 07 月 18 日
用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
系统学习了HTTP网络请求的知识点
HttpURLConnection与HttpClient的比较:
https://www.cnblogs.com/shamo89/p/10204479.html
HTTP与HTTPs的请求区别:
https://www.cnblogs.com/mengen/p/9138214.html
多线程知识点
使用线程池。
下面的解决方法使用CountDownLatch来保证线程在所有请求完成钱都处于阻塞状态。
还可以使用executor.shutdown()以及executor.awaittermination(long timeout, TimeUnit unit)来保证。
import java.io.IOException;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Queue;import java.util.concurrent.ConcurrentLinkedQueue;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;import java.net.HttpURLConnection;import java.net.URL;import java.io.InputStream;import java.util.concurrent.CountDownLatch;public class CapacityTestingTool { private final String url; private final int totalRequest; private final ExecutorService pool; public CapacityTestingTool(String url, int totalRequest, ExecutorService pool) { this.url = url; this.totalRequest = totalRequest; this.pool = pool; } public double[] test() { CountDownLatch doneSignal = new CountDownLatch(totalRequest); Queue<Long> timeQueue = new ConcurrentLinkedQueue<>(); for (int i = 0; i < totalRequest; i++) { pool.execute(new Runnable() { @Override public void run() { HttpURLConnection con = null; long startTime = System.nanoTime(); // Make HTTP request to the specified URL try { URL link = new URL(url); con = (HttpURLConnection) link.openConnection(); con.setUseCaches(false); con.setConnectTimeout(3000); con.setRequestMethod("GET"); InputStream in = con.getInputStream(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); StringBuilder result = new StringBuilder(); String line; while((line = reader.readLine()) != null) { result.append(line); } //System.out.println(result.toString()); in.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (con != null) { con.disconnect(); } } long endTime = System.nanoTime(); timeQueue.add(endTime - startTime); System.out.println(endTime - startTime); doneSignal.countDown(); } }); } try { doneSignal.await(); } catch (InterruptedException e) { e.printStackTrace(); } List<Long> times = new ArrayList<>(timeQueue); double avgResponseTime = getAvg(times) / 1000000000.0; double ninetyFivePercentileTime = get95Percentile(times) / 1000000000.0; return new double[]{avgResponseTime, ninetyFivePercentileTime}; } private static double getAvg(List<Long> times) { long total = 0; for (long time : times) { total += time; } return total / (float)times.size(); } private static long get95Percentile(List<Long> times) { Collections.sort(times); int index = (int) Math.ceil(95 / 100.0 * times.size()); return times.get(index-1); } public static void main(String[] args) { ExecutorService pool = Executors.newFixedThreadPool(10); CapacityTestingTool tool = new CapacityTestingTool("https://www.baidu.com/", 100, pool); double[] metrics = tool.test(); System.out.println("The average response time is " + metrics[0] + " seconds and the 95 " + "percentile response time is " + metrics[1] + " seconds"); pool.shutdown(); }}
Output
The average response time is 0.109775896 seconds and the 95 percentile response time is 0.520162642 seconds
划线
评论
复制
发布于: 2020 年 07 月 18 日阅读数: 136
Melo
关注
还未添加个人签名 2019.09.17 加入
还未添加个人简介
评论