写点什么

第七周作业

发布于: 2020 年 12 月 17 日

public class PressureTest {

private static final String USER_AGENT = "HttpClient/3.1/PP";

private static final String CHARSET_UTF = "UTF-8";

private MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();

private HttpClient httpClient = new org.apache.commons.httpclient.HttpClient(httpConnectionManager);

private ExecutorService executor;

/**

* 测试url

*/

private String testUrl;

/**

* 请求总次数

*/

private Integer testLength;

/**

* 并发数

*/

private Integer currentCount;



private SortedMap<Long, Integer> testResult = new TreeMap<>();



public PressureTest(String testUrl, Integer testLength, Integer currentCount) {

this.testUrl = testUrl;

this.currentCount = currentCount;

this.testLength = testLength;

this.executor = Executors.newFixedThreadPool(currentCount);

}



/**

* 发起测试

*/

public void testing(Integer percentTime) {

for (int i = 0; i < testLength; i++) {

executor.execute(() -> {

long startTime = System.currentTimeMillis();

Integer result = null;

try {

result = httpGet(testUrl);

} catch (Exception e) {

System.out.println(e.getMessage());

}

long endTime = System.currentTimeMillis();

long time = endTime - startTime;

synchronized (testResult.getClass()) {

Integer getVal = testResult.get(time);

if (getVal == null) {

getVal = 0;

}

testResult.put(time, ++getVal);

}

});

}

executor.shutdown();

try {

executor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);

} catch (InterruptedException e) {

e.printStackTrace();

}

//计算响应时长

testResultTime(percentTime);

}

/**

* 响应时常

*/

public void testResultTime(Integer percentTime) {

//总时长

Long timeCount = 0L;

// 95总时长

Long timeCount95 = 0L;

// 统计95的数量

Double count95 = 0.0;

//用百分比转化为需要查看的请求数的平均响应时常

Double countTime = testLength percentTime 0.01;

for (HashMap.Entry<Long, Integer> entry : testResult.entrySet()) {

timeCount += entry.getKey() * entry.getValue();

if(count95 < countTime){

if((count95 = count95 + entry.getValue() * 1.0) >= countTime){

timeCount95 = timeCount;

}

}



}

System.out.println("平均响应时间:" + (timeCount / testLength));

System.out.println("95平均响应时间:" + (timeCount95/countTime));

}



public Integer httpGet(String fullUrl) {

GetMethod method = new GetMethod(fullUrl);

method.getParams().setParameter(HttpMethodParams.HTTPCONTENTCHARSET, "utf-8");

method.addRequestHeader("User-Agent", USER_AGENT);

try {

return httpClient.executeMethod(method);

} catch (IOException e) {

e.printStackTrace();

}finally {

method.releaseConnection();

}

return 0;



}



用户头像

还未添加个人签名 2020.09.08 加入

还未添加个人简介

评论

发布
暂无评论
第七周作业