性能测试工具

发布于: 11 小时前

下面是自己实现的一个简单的性能测试工具,可以支持按请求数或者按持续时长来进行压测。代码实现如下:

public class TestCondition {
private String url;
private int concurrencyNum;
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getConcurrencyNum() {
return concurrencyNum;
}
public void setConcurrencyNum(int concurrencyNum) {
this.concurrencyNum = concurrencyNum;
}
}
public class RequestNumberTestCondition extends TestCondition {
private int requestNum;
public int getRequestNum() {
return requestNum;
}
public void setRequestNum(int requestNum) {
this.requestNum = requestNum;
}
}
public class RequestDurationTestCondition extends TestCondition {
private long duration;
public long getDuration() {
return duration;
}
public void setDuration(long duration) {
this.duration = duration;
}
}
public abstract class BaseTestFrame {
private volatile boolean isStop = false;
public Metric execute(TestCondition testCondition) {
int concurrencyNum = testCondition.getConcurrencyNum();
String url = testCondition.getUrl();
ExecutorService service = Executors.newFixedThreadPool(concurrencyNum);
List<Long> list = Collections.synchronizedList(new ArrayList<>());
AtomicLong successCount = new AtomicLong();
AtomicLong failCount = new AtomicLong();
boolean flag = true;
long taskBeginTime = System.currentTimeMillis();
long taskNumber = 0;
while (flag) {
taskNumber++;
System.out.println(isStop);
if (isStop) {
break;
}
service.submit(new Runnable() {
public void run() {
long begin = System.currentTimeMillis();
//TODO 优化http request
int responseCode = HttpClient.sendRequest(url, "GET", null);
System.out.println("responseCode: " + responseCode);
long elapsedTime = System.currentTimeMillis() - begin;
if (responseCode == 200) {
successCount.incrementAndGet();
} else {
failCount.incrementAndGet();
}
list.add(elapsedTime);
}
});
flag = judgeTerminal(testCondition, taskBeginTime, taskNumber);
}
service.shutdown();
try {
service.awaitTermination(5, TimeUnit.MINUTES);
} catch (InterruptedException e) {
}
Metric metric = new Metric(successCount.get(), failCount.get(), list);
return metric;
}
public void stop() {
isStop = true;
}
public abstract boolean judgeTerminal(TestCondition testCondition, long taskBeginTime, long taskNumber);
}
public class RequestNumberTestFrame extends BaseTestFrame {
@Override
public boolean judgeTerminal(TestCondition testCondition, long taskBeginTime, long taskNumber) {
RequestNumberTestCondition requestNumberTestCondition = (RequestNumberTestCondition) testCondition;
if (taskNumber < requestNumberTestCondition.getRequestNum()) {
return true;
}
return false;
}
}
public class RequestDurationTestFrame extends BaseTestFrame {
@Override
public boolean judgeTerminal(TestCondition testCondition, long taskBeginTime, long taskNumber) {
RequestDurationTestCondition requestDurationTestCondition = (RequestDurationTestCondition) testCondition;
if ((System.currentTimeMillis() - taskBeginTime) < requestDurationTestCondition.getDuration()) {
return true;
}
return false;
}
}
public class Metric {
private long successCount;
private long failCount;
private List<Long> elapsedTimeList;
public Metric(long successCount, long failCount, List<Long> elapsedTimeList) {
this.successCount = successCount;
this.failCount = failCount;
Collections.sort(elapsedTimeList);
this.elapsedTimeList = elapsedTimeList;
}
public long getTotalCount() {
return this.successCount + this.failCount;
}
public long getSuccessCount() {
return successCount;
}
public long getFailCount() {
return failCount;
}
public double getAverageTime() {
return elapsedTimeList.stream().mapToLong(Long::longValue).average().getAsDouble();
}
public double get95thTime() {
long totalCount = getTotalCount();
int percentOf95 = (int)(totalCount * 0.95);
return elapsedTimeList.get(percentOf95);
}
public double get99thTime() {
long totalCount = getTotalCount();
int percentOf99 = (int)(totalCount * 0.99);
return elapsedTimeList.get(percentOf99);
}
}
public class Client {
public static void main(String[] args) {
String outputFormat = "总请求数: %d, 成功数: %d, 失败数: %d, 平均时间: %f, 95th: %f";
// BaseTestFrame testFrame = new RequestNumberTestFrame();
// RequestNumberTestCondition requestNumberTestCondition = new RequestNumberTestCondition();
// requestNumberTestCondition.setUrl("http://39.104.167.105:8080/hello");
// requestNumberTestCondition.setConcurrencyNum(10);
// requestNumberTestCondition.setRequestNum(10000);
// Metric metric = testFrame.execute(requestNumberTestCondition);
// System.out.println(String.format(outputFormat, metric.getTotalCount(), metric.getSuccessCount(),
// metric.getFailCount(), metric.getAverageTime(), metric.get95thTime()));
BaseTestFrame requestDurationTestFrame = new RequestDurationTestFrame();
RequestDurationTestCondition requestDurationTestCondition = new RequestDurationTestCondition();
requestDurationTestCondition.setUrl("http://39.104.167.105:8080/hello");
requestDurationTestCondition.setConcurrencyNum(100);
requestDurationTestCondition.setDuration(60000);
new Thread(new Runnable() {
@Override
public void run() {
Metric resultMetric = requestDurationTestFrame.execute(requestDurationTestCondition);
System.out.println(String.format(outputFormat, resultMetric.getTotalCount(), resultMetric.getSuccessCount(),
resultMetric.getFailCount(), resultMetric.getAverageTime(), resultMetric.get95thTime()));
}
}).start();
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
requestDurationTestFrame.stop();
}
}

发布于: 11 小时前 阅读数: 4
用户头像

olderwei

关注

还未添加个人签名 2018.04.26 加入

还未添加个人简介

评论

发布
暂无评论
性能测试工具