写点什么

架构师训练营第七周课后作业

用户头像
竹森先生
关注
发布于: 2020 年 07 月 22 日

架构师训练营第七周课后作业

Java实现,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。

public class GetUtils {
private static CloseableHttpClient httpClient = HttpClients.createDefault();
private static final Logger LOG = LoggerFactory.getLogger(TestStress.class);
public static boolean get(String url, Map<String, String> param) {
CloseableHttpResponse response = null;
try {
URIBuilder builder = new URIBuilder(url);
if (param != null) {
for (String key : param.keySet()) {
builder.addParameter(key, param.get(key));
}
}
URI uri = builder.build();
HttpGet httpGet = new HttpGet(uri);
response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() == 200) {
return true;
} else {
LOG.error("Request is unsuccess,responseCode:" + response.getStatusLine().getStatusCode()
+ " ,responseMsg:" + EntityUtils.toString(response.getEntity(), "utf-8"));
}
} catch (Exception e) {
LOG.error("Request error,exceptionInfo:" + ExceptionUtils.getStackTrace(e));
e.printStackTrace();
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
}



public class TestStress {
private static Integer threadCount = null;
private static String requestUrl = null;
private static Integer requestCount = null;
private static ExecutorService ThreadPool = null;
protected static List<Long> timeList = null;
static {
try {
init();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
exec();
Collections.sort(timeList);
System.out.println("结束,执行时间:" + timeList);
Long totalTime = 0l;
Long totalTime95 = 0l;
for (int i = 0; i < timeList.size(); i++) {
totalTime += timeList.get(i);
if (i < 95) {
totalTime95 += timeList.get(i);
}
}
int count = timeList.size();
int count95 = (int) (count*0.95);
System.out.println("响应数量:" + count);
System.out.println("95%响应数量:" + count95);
System.out.println("响应总时间:" + totalTime);
System.out.println("平均响应时间:" + (double)totalTime/count);
System.out.println("95%响应时间:" + (double)totalTime95/count95);
}
private static void init() throws IOException {
threadCount = 10;
requestUrl = "https://www.baidu.com";
requestCount = 100;
timeList = new ArrayList<>();
ThreadPool = Executors.newFixedThreadPool(threadCount);
}
private static void exec() throws InterruptedException {
CountDownLatch requestCountDown = new CountDownLatch(requestCount);
for (int i = 0; i < requestCount; i++) {
RequestThread requestThread = new RequestThread(requestUrl, requestCountDown);
ThreadPool.execute(requestThread);
}
requestCountDown.await();
}
}



public class RequestThread implements Runnable {
private static final Logger LOG = LoggerFactory.getLogger(RequestThread.class);
private CountDownLatch countDownLatch;
private String requestUrl;
public RequestThread(String requestUrl, CountDownLatch countDownLatch) {
this.requestUrl = requestUrl;
this.countDownLatch = countDownLatch;
}
@Override
public void run() {
try {
long startTime = System.currentTimeMillis();
boolean isSuccess = GetUtils.get(requestUrl, null);
long endTime = System.currentTimeMillis();
if (!isSuccess) {
LOG.error("Error,ThreadName:" + Thread.currentThread().getName());
}
TestStress.timeList.add(endTime - startTime);
countDownLatch.countDown();
} catch (Exception e) {
LOG.error("Error,ThreadName:" + Thread.currentThread().getName());
}
}
}

输出

结束,执行时间:[523, 526, 527, 527, 528, 528, 529, 529, 530, 530, 531, 534, 535, 535, 537, 537, 537, 537, 538, 538, 539, 539, 541, 542, 542, 543, 543, 543, 544, 544, 544, 545, 545, 546, 546, 546, 547, 547, 548, 548, 548, 549, 549, 549, 549, 549, 550, 550, 550, 551, 551, 551, 552, 552, 552, 552, 553, 553, 553, 553, 554, 554, 554, 555, 555, 555, 556, 557, 557, 557, 557, 559, 559, 559, 561, 561, 562, 563, 566, 566, 567, 567, 568, 570, 570, 571, 572, 574, 581, 593, 5637, 5639, 5745, 5751, 5859, 5862, 5964, 5983, 6068, 6097]
响应数量:100
95%响应数量:95
响应总时间:108039
平均响应时间:1080.39
95%响应时间:821.7368421052631



发布于: 2020 年 07 月 22 日阅读数: 48
用户头像

竹森先生

关注

还未添加个人签名 2020.03.26 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第七周课后作业