性能测试需求
我们经常需要对代码的性能进行压测,如果全部自己写一个工具,还是很消耗时间的。
junit
如果你使用 junit,可以直接结合 junitperf 进行编写。
简单的 main()
如果你直接想测试验证下,那也不是不行。
此处直接给出一个测试验证的代码模板。
[java]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
* 执行步骤:
*
* http://127.0.0.1:8081/xxx/perform/start?max=1&increase=1&interval=10000
*/
@RestController
@RequestMapping("/perform")
public class PerformanceController {
private static Logger logger = LoggerFactory.getLogger(PerformanceController.class);
static int successCount = 0;
static int failedCount = 0;
static int numberOfThread = 0;
static MonitorThread monitorThread;
static QueryThread[] workThreads;
static int MAX_THREAD = 10;
static int INCREASEMENT = 1;
static int INCREASE_INTERVAL_MILLIS = 10000;
ArrayList<HashMap<String, String>> arrlt = new ArrayList<HashMap<String, String>>();
@RequestMapping("/start")
public String start(int max, int increase, int interval) {
// 基础信息初始化
MAX_THREAD = max;
INCREASEMENT = increase;
INCREASE_INTERVAL_MILLIS = interval;
numberOfThread = 0;
failedCount = 0;
successCount = 0;
// 启动 monitor 线程
monitorThread = new MonitorThread();
monitorThread.start();
workThreads = new QueryThread[MAX_THREAD];
for (int i = 0; i < workThreads.length; i++) {
workThreads[i] = new QueryThread();
workThreads[i].pause();
workThreads[i].start();
}
while (numberOfThread < MAX_THREAD) {
int maxIndex = numberOfThread + INCREASEMENT;
for (int i = numberOfThread; i < maxIndex && i < workThreads.length; i++) {
workThreads[i].resumeRun();
numberOfThread++;
}
try {
TimeUnit.MILLISECONDS.sleep(INCREASE_INTERVAL_MILLIS);
} catch (InterruptedException e) {
}
}
return "finished";
}
@RequestMapping("/stop")
public String stop() {
for (int i = 0; i < workThreads.length; i++) {
workThreads[i].interrupt();
}
for (int i = 0; i < workThreads.length; i++) {
try {
workThreads[i].join();
} catch (InterruptedException e) {
}
}
monitorThread.interrupt();
try {
monitorThread.join();
} catch (InterruptedException e) {
}
return "stopped";
}
class MonitorThread extends Thread {
int lastSuccessCount = 0;
int lastFailedCount = 0;
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
logger.info("Threads: " + numberOfThread + ", Success: " + successCount + ", Failed: " + failedCount + ", NewSuccess: " + (successCount - lastSuccessCount) + ", NewFailed: " + (failedCount - lastFailedCount));
DecimalFormat rt=new DecimalFormat("0.00");
if ((successCount - lastSuccessCount) > 0) {
logger.info("(每秒处理事务数)TPS:" + (successCount - lastSuccessCount) + ", 平均响应时间(RT):" + rt.format((float)1000/(successCount - lastSuccessCount)) +" ms");
}
lastSuccessCount = successCount;
lastFailedCount = failedCount;
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
}
}
class QueryThread extends Thread {
private boolean runnable = true;
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
if (runnable) {
try {
//TODO: 执行测试验证代码
if (success()) { //校验应答
successCount++;
} else {
failedCount++;
}
TimeUnit.SECONDS.sleep(1);
} catch (Exception e) {
failedCount++;
logger.error(e.getMessage());
}
} else {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
break;
}
}
}
}
public void pause() {
this.runnable = false;
}
public void resumeRun() {
this.runnable = true;
}
}
}