第七周作业
发布于: 2020 年 12 月 13 日
1:性能压测的时候,随着并发压力的增加,系统响应时间和吞吐量如何变化,为什么?
因为刚开始的时候,资源没有利用完全,还有很多空闲的 cpu,内存等,所以刚开始增加并发的时候,响应时间是线性增加的,当 cpu 或者内存等资源在每秒使用率都快沾满的时候,再增加并发,此时 cpu 需要增加线程切换以及用户态与内核态的转化,加上大量的线程等待占有时间较长,故线程等待时间变长,系统响应时间也会变长,吞吐量则下降
2:用你熟悉的编程语言写一个 Web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。
代码如下:
package com.zhong.cli.service;
import com.zhong.cli.enums.RequestType;
import com.zhong.cli.util.HttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicReference;
/**
* @Author Administrator
* @Date 2020/11/8 8:43
* @Version 1.0
*/
@Component
public class PressureTestService {
@Autowired
HttpClient httpClient;
List<String> execResult = new CopyOnWriteArrayList<>();
CountDownLatch latch = null;
List<Long> innerTimes = new ArrayList<>();
public String request(String urlAddr, Integer concurrentNum, Map<String, String> params) throws URISyntaxException, InterruptedException, ExecutionException {
String result = "主线程执行结束";
latch = new CountDownLatch(concurrentNum );
BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(concurrentNum);
ThreadPoolExecutor executor = new ThreadPoolExecutor(concurrentNum, concurrentNum, 1000, TimeUnit.MILLISECONDS, workQueue);
for (int i = 0; i < concurrentNum; i++) {
executor.submit(new Task(urlAddr, new HashMap<>()));
}
latch.await();
for (String str : execResult) {
result = result.concat(str);
}
Long avgTime ;
Long totalTime = 0L;
//计算平均时间
for(Long time : innerTimes){
totalTime =totalTime+ time;
}
avgTime = totalTime/innerTimes.size();
return "平均时间:"+ avgTime +"毫秒";
}
private class Task implements Callable<String> {
private String urlAddr;
private Map<String, String> hashmap;
public Task(String urlAddr, Map<String, String> hashmap) {
this.urlAddr = urlAddr;
this.hashmap = hashmap;
}
@Override
public String call() throws Exception {
Long beginTime = System.currentTimeMillis();
String tempresult = httpClient.requestURI(urlAddr, hashmap, RequestType.GET);
execResult.add(tempresult);
if(null != latch){
latch.countDown();
}
Long endTime = System.currentTimeMillis();
innerTimes.add(endTime-beginTime);
return tempresult;
}
}
}
复制代码
package com.zhong.cli.util;
import com.zhong.cli.enums.RequestType;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.net.URI;
import java.util.Map;
/**
* @Author Administrator
* @Date 2020/11/8 8:55
* @Version 1.0
*/
@Component
public class HttpClient {
public String requestURI(String urlAddr, Map<String, String> params, RequestType requestType) {
System.out.println("execute requestURI");
String result = "";
if (requestType.name().equals(RequestType.GET.name())) {
result = getResult(result, urlAddr);
}
return result;
}
private String getResult(String result, String uri) {
CloseableHttpClient client = HttpClients.createDefault();
HttpGet httpGet = new HttpGet(uri);
try {
CloseableHttpResponse response = client.execute(httpGet);
int iGetResultCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
if (iGetResultCode >= 200 && iGetResultCode < 303) {
result = EntityUtils.toString(entity);
} else if (iGetResultCode >= 400 && iGetResultCode < 500) {
result = "请求的目标地址不存在:" + iGetResultCode;
} else {
result = "请求错误:" + iGetResultCode;
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
public URI getURL(String urlAddr, Map<String, String> params) {
try {
URIBuilder builder = new URIBuilder()
.setScheme("http")
.setHost(urlAddr);
params.forEach((x, y) -> {
builder.setParameter(x, y);
});
return builder.build();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
复制代码
package com.zhong.cli.shell;
import com.zhong.cli.service.PressureTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.shell.standard.ShellComponent;
import org.springframework.shell.standard.ShellMethod;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.concurrent.ExecutionException;
//
@ShellComponent
public class CommandShell {
@Autowired
PressureTestService pressureTestService;
@ShellMethod(value = "please input url,request num,Concurrent number,param.", key = "sum")
public String add(String url,Integer requestNum,Integer concurrentNum) throws URISyntaxException, InterruptedException, ExecutionException {
String result = "";
for(int i =0 ;i<requestNum;i++){
result = result.concat(pressureTestService.request(url,concurrentNum,new HashMap<>()));
}
return result;
}
}
复制代码
划线
评论
复制
发布于: 2020 年 12 月 13 日阅读数: 21
Geek_9527
关注
还未添加个人签名 2019.10.23 加入
还未添加个人简介
评论