一个简单的压力测试程序,可设置请求地址,并发请求的线程数和总请求数
代码结构如下:
下面直接贴代码
1.pom依赖
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.5</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.7</version>
</dependency>
</dependencies>
2.RequestThread.java源码
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;
}
public void run() {
try {
boolean isSuccess = HttpClientUtil.get(requestUrl, null);
if (!isSuccess) {
LOG.error("response error.....threadName:" + Thread.currentThread().getName());
}
countDownLatch.countDown();
} catch (Exception e) {
LOG.error("requset error.....threadName:" + Thread.currentThread().getName());
}
}
}
3.StressTest.java源码
public class StressTest {
private static Integer threadCount = 0;
private