写点什么

第七周

用户头像
Geek_fabd84
关注
发布于: 2020 年 11 月 08 日
package com.demo;
/****************************** * 用途说明: 压测参数 * 作者姓名: chenshijie * 创建时间: 2020/11/08 01:29 ******************************/public class TestConfig {
private String url;
private int total;
private int semaphore;
public String getUrl() { return url; }
public void setUrl(String url) { this.url = url; }
public int getTotal() { return total; }
public void setTotal(int total) { this.total = total; }
public int getSemaphore() { return semaphore; }
public void setSemaphore(int semaphore) { this.semaphore = semaphore; }}
复制代码


package com.demo;
/****************************** * 用途说明: 压测引擎 * 作者姓名: chenshijie * 创建时间: 2020/11/08 01:06 ******************************/public class TestEngine {
public Task createTask(TestConfig testConfig) { Task t = new Task(testConfig); return t; }
public void runTest(TestConfig testConfig){ Task task = createTask(testConfig); try { task.run(); } catch (InterruptedException e) { e.printStackTrace(); } }
}
复制代码


package com.demo;
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.URL;import java.net.URLConnection;
/****************************** * 用途说明: 请求工具类 * 作者姓名: chenshijie * 创建时间: 2020/11/08 13:44 ******************************/public class HttpClient {
public static long request(String url) { String result = "";// 访问返回结果 BufferedReader read = null;// 读取访问结果
long start = System.currentTimeMillis(); long end = 0; try { // 创建url URL realurl = new URL(url); // 打开连接 URLConnection connection = realurl.openConnection(); // 设置通用的请求属性 connection.setRequestProperty("accept", "*/*"); connection.setRequestProperty("connection", "Keep-Alive"); connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)"); // 建立连接 connection.connect();
// 定义 BufferedReader输入流来读取URL的响应 read = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8")); String line;// 循环读取 while ((line = read.readLine()) != null) { result += line; } } catch (IOException e) { e.printStackTrace(); } finally { if (read != null) {// 关闭流 try { read.close(); } catch (IOException e) { e.printStackTrace(); } } end = System.currentTimeMillis(); } return end - start; }}
复制代码


package com.demo;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import java.util.*;import java.util.concurrent.CountDownLatch;import java.util.concurrent.atomic.AtomicLong;
/****************************** * 用途说明: 压测任务 * 作者姓名: chenshijie * 创建时间: 2020/11/08 01:50 ******************************/public class Task {
private static final Logger LOGGER = LoggerFactory.getLogger(Task.class);
// private AtomicLong acount = new AtomicLong(0L);
private TestConfig testConfig;
private List<Long> result = new ArrayList<Long>();
public Task(TestConfig testConfig) { this.testConfig = testConfig; }
public void run() throws InterruptedException { final CountDownLatch latch = new CountDownLatch(testConfig.getTotal()); for (int i = 0; i < testConfig.getSemaphore(); i++) { new Thread(new Runnable() { public void run() { while (latch.getCount() > 0) { LOGGER.info("执行:{}",latch.getCount()); long request = HttpClient.request(testConfig.getUrl());// LOGGER.info("请求时间:{} ms", request); result.add(request); latch.countDown(); } } }).start(); } latch.await(); LOGGER.info("请求完成:{}",latch.getCount());
Collections.sort(result, new Comparator<Long>() { public int compare(Long o1, Long o2) { Long l = o1 - o2; return l.intValue(); } });

int i = 1; long all = 0; long all95 = 0; for(Iterator<Long> it = result.iterator();it.hasNext();) { Long next = it.next(); LOGGER.info("请求{},时间:{}",i,next); all = all + next; if(i == 95) { all95 = next; }
i++; } LOGGER.info("总共请求:{},平均请求时间:{},95%请求时长:{}",result.size(),all/result.size(),all95); }
}
复制代码


package com.demo;
import org.junit.Test;
public class TaskTest {
@Test public void run() { TestConfig testConfig = new TestConfig(); testConfig.setUrl("https://baidu.com"); testConfig.setSemaphore(10); testConfig.setTotal(100);
TestEngine testEngine = new TestEngine(); testEngine.runTest(testConfig);
}
}
复制代码


2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 1,时间:39

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 2,时间:39

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 3,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 4,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 5,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 6,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 7,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 8,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 9,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 10,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 11,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 12,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 13,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 14,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 15,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 16,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 17,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 18,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 19,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 20,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 21,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 22,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 23,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 24,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 25,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 26,时间:40

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 27,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 28,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 29,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 30,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 31,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 32,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 33,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 34,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 35,时间:41

2020-11-08 15:49:18.649 INFO com.demo.Task - 请求 36,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 37,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 38,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 39,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 40,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 41,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 42,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 43,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 44,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 45,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 46,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 47,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 48,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 49,时间:41

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 50,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 51,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 52,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 53,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 54,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 55,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 56,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 57,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 58,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 59,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 60,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 61,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 62,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 63,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 64,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 65,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 66,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 67,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 68,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 69,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 70,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 71,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 72,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 73,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 74,时间:42

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 75,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 76,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 77,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 78,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 79,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 80,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 81,时间:43

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 82,时间:170

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 83,时间:170

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 84,时间:173

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 85,时间:174

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 86,时间:176

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 87,时间:177

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 88,时间:179

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 89,时间:179

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 90,时间:187

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 91,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 92,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 93,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 94,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 95,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 96,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 97,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 98,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 99,时间:1888

2020-11-08 15:49:18.650 INFO com.demo.Task - 请求 100,时间:1888

2020-11-08 15:49:18.651 INFO com.demo.Task - 总共请求:100,平均请求时间:237,95%请求时长:1888


用户头像

Geek_fabd84

关注

还未添加个人签名 2019.06.30 加入

还未添加个人简介

评论

发布
暂无评论
第七周