架构师训练营 week7 作业 1
发布于: 2020 年 07 月 22 日
Java模拟并发操作进行服务器压力测试
用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com
package me.lichun.week7;import cn.hutool.http.HttpUtil;import java.text.NumberFormat;import java.util.concurrent.ConcurrentHashMap;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class MultiThreadProxyHubApiTest { static int count = 0; //并发量 int thread_num = 10; //总访问量 int client_num = 100; //平均响应时间 float avg_exec_time = 0; //总执行时间 float sum_exec_time = 0; long first_exec_time = Long.MAX_VALUE; long last_done_time = Long.MIN_VALUE; float total_exec_time = 0; String url = ""; public MultiThreadProxyHubApiTest(int thread_num, int client_num, String url){ this.thread_num = thread_num; this.client_num = client_num; this.url = url; } public void run() { final MultiThreadProxyHubApiTest currentObj = this; final ConcurrentHashMap<Integer, ThreadRecord> records = new ConcurrentHashMap<Integer, ThreadRecord>(); // 建立ExecutorService线程池 ExecutorService exec = Executors.newFixedThreadPool(thread_num); final CountDownLatch doneSignal = new CountDownLatch(client_num); for (int i = 0; i < client_num; i++) { Runnable run = new Runnable() { @Override public void run() { int index = getIndex(); long st = System.currentTimeMillis(); try { HttpUtil.get(url); } catch (Exception e) { e.printStackTrace(); } records.put(index, new ThreadRecord(st, System.currentTimeMillis())); doneSignal.countDown();//每调用一次countDown()方法,计数器减1 } }; exec.execute(run); } try { //计数器大于0 时,await()方法会阻塞程序继续执行 doneSignal.await(); } catch (InterruptedException e) { e.printStackTrace(); } /** * 获取每个线程的开始时间和结束时间 */ for(int i : records.keySet()){ ThreadRecord r = records.get(i); sum_exec_time += ((double)(r.et - r.st))/1000; if(r.st < first_exec_time){ first_exec_time = r.st; } if(r.et > last_done_time){ this.last_done_time = r.et; } } this.avg_exec_time = this.sum_exec_time / records.size(); this.total_exec_time = ((float)(this.last_done_time - this.first_exec_time)) / 1000; NumberFormat nf=NumberFormat.getNumberInstance(); nf.setMaximumFractionDigits(4); System.out.println("======================================================"); System.out.println("Thread Num: " + thread_num + ", Client Count: "+ client_num +"."); System.out.println("Avg Exec Time: " + nf.format(this.avg_exec_time) + " s"); System.out.println("Total Exec Time: " + nf.format(this.total_exec_time) + " s"); System.out.println("Throughput: " + nf.format(this.client_num /this.total_exec_time)+ " /s"); } public static int getIndex(){ return ++count; } public static void main(String[] args) { //总访问量和并发量两重循环,依次增大访问 for(int j=100; j<200; j+= 100){ for(int i=10; i<100; i+=10){ //要测试的URL String url="http://www.baidu.com/"; new MultiThreadProxyHubApiTest(i, j, url).run(); } } System.out.println("finished!"); }}class ThreadRecord { long st; long et; public ThreadRecord(long st, long et){ this.st = st; this.et = et; }}
折腾了一晚上没有执行成功,先交作业了,不过那个"95% 响应时间"不知道该怎么算
划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 45
平淡人生
关注
还未添加个人签名 2018.11.05 加入
还未添加个人简介
评论