第七周作业
发布于: 2020 年 07 月 22 日
作业一:
以下两题,至少选做一题
性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
package com.example;import java.io.IOException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.concurrent.ArrayBlockingQueue;import java.util.concurrent.CountDownLatch;import java.util.concurrent.ThreadPoolExecutor;import java.util.concurrent.TimeUnit;import net.sf.json.JSONObject;import org.apache.http.HttpStatus;import org.apache.http.client.config.RequestConfig;import org.apache.http.client.methods.CloseableHttpResponse;import org.apache.http.client.methods.HttpPost;import org.apache.http.entity.StringEntity;import org.apache.http.impl.client.CloseableHttpClient;import org.apache.http.impl.client.HttpClients;import org.springframework.util.CollectionUtils;public class TestUtils { final static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); public static void main(String[] args) throws Exception { test("http://www.baidu.com",10,100); } public static void test(String url,Integer crNum,Integer execNum){ Long startTime = System.currentTimeMillis(); ThreadPoolExecutor executor = new ThreadPoolExecutor(2000, 5000, 2, TimeUnit.MINUTES, new ArrayBlockingQueue<Runnable>(5000)); List<Long> list = new ArrayList<>(); for(int j=0;j<execNum;j++){ //模拟crNum人并发请求 CountDownLatch latch = new CountDownLatch(1); //模拟crNum个用户 for (int i = 0; i < crNum; i++) { AnalogUser analogUser = new AnalogUser(latch,url,list); executor.submit(analogUser); } //计数器減一 所有线程释放 并发访问。 latch.countDown(); System.out.println("第"+j+"次"); } executor.shutdown(); //等待所有任务都执行结束 while (true) { if (executor.isTerminated()) { //所有的子线程都结束了 System.out.println("共耗时:"+(System.currentTimeMillis()-startTime)/1000.0+"s"); break; } } if(!CollectionUtils.isEmpty(list)){ Long count = 0L; for(Long l:list){ count +=l; } System.out.println("平均响应时间:"+count/execNum/crNum+"毫秒"); Collections.sort(list); System.out.println("95% 时间"+list.get(94)+"毫秒"); } } static class AnalogUser implements Runnable { CountDownLatch latch; String url; List<Long> list; public AnalogUser(CountDownLatch latch,String url,List<Long> list) { this.latch = latch; this.url = url; this.list = list; } @Override public void run() { long starTime = 0; try { starTime = System.currentTimeMillis(); latch.await(); System.out.println("请求开始了"); httpPost(url, ""); } catch (InterruptedException e) { e.printStackTrace(); } long endTime = System.currentTimeMillis(); Long t = endTime - starTime; System.out.println(t + "毫秒"); list.add(t); } } static boolean httpPost(String url, String strParam) { // post请求返回结果 CloseableHttpClient httpClient = HttpClients.createDefault(); JSONObject jsonResult = null; HttpPost httpPost = new HttpPost(url); // 设置请求和传输超时时间 RequestConfig requestConfig = RequestConfig.custom() .setSocketTimeout(2000).setConnectTimeout(2000).build(); httpPost.setConfig(requestConfig); try { if (null != strParam) { // 解决中文乱码问题 StringEntity entity = new StringEntity(strParam, "utf-8"); entity.setContentEncoding("UTF-8"); entity.setContentType("application/x-www-form-urlencoded"); httpPost.setEntity(entity); } CloseableHttpResponse result = httpClient.execute(httpPost); //请求发送成功,并得到响应 if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { return true; } } catch (IOException e) { return false; } finally { httpPost.releaseConnection(); } return false; }}
共耗时:4.52s
平均响应时间:3948毫秒
95% 时间3399毫秒
划线
评论
复制
发布于: 2020 年 07 月 22 日阅读数: 42

魔曦
关注
我思故我在! 2018.01.15 加入
凡事有交代,件件有着落,事事有回音。
评论