java编写一个实用的压力测试程序

一个简单的压力测试程序,可设置请求地址,并发请求的线程数和总请求数

代码结构如下:

在这里插入图片描述
下面直接贴代码

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 
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值