自己写的压力测试工具

用户头像
stars
关注
发布于: 2020 年 07 月 22 日
自己写的压力测试工具

1、基本需求

用你熟悉的编程语言写一个 web 性能压测工具,输入参数:URL,请求总次数,并发数。输出参数:平均响应时间,95% 响应时间。用这个测试工具以 10 并发、100 次请求压测 www.baidu.com。

2、主要代码

功能相对简单,这里使用Java语言,要使用线程的相关知识。同时,使用HttpClient发送web请求。

2.1、引入依赖

<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.9</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>

2.2、httpClient发送请求

public class HttpUtil {
/**
* GET请求数据
*
* @param targetUrl url地址
* @param params key=value形式
* @return 返回结果
* @throws Exception
*/
public static String doGet(String targetUrl, JSONObject params) throws Exception {
String result = "";
URL getUrl = null;
BufferedReader reader = null;
HttpURLConnection connection = null;
try {
getUrl = getFullLink(targetUrl, params);
connection = (HttpURLConnection) getUrl.openConnection();
connection.connect();
reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String lines = "";
while ((lines = reader.readLine()) != null) {
result = result + lines;
}
return result;
} catch (Exception e) {
throw e;
} finally {
if (reader != null) {
reader.close();
}
connection.disconnect();
}
}
private static URL getFullLink(String targetUrl, JSONObject params) throws MalformedURLException {
StringBuilder sb = new StringBuilder();
URL doGetUrl = new URL(targetUrl);
if (params != null) {
for (String key : params.keySet()) {
sb.append(key).append("=").append(params.get(key)).append("&");
}
sb.deleteCharAt(sb.lastIndexOf("&"));
doGetUrl = new URL(targetUrl + "?" + sb);
}
return doGetUrl;
}
/**
* GET请求数据, 不需要传参的情况
*
* @param targetUrl url地址
* @return 返回结果
* @throws Exception
*/
public static String doGet(String targetUrl) {
try {
return doGet(targetUrl, null);
} catch (Exception e) {
throw new RuntimeException("httpUtil error occoured!");
}
}
/**
* GET请求数据, 不需要传参的情况
*
* @param res url地址
* @return 返回结果
* @throws Exception
*/
public static String getResData(String res) {
if (StringUtils.isNotBlank(res)) {
JSONObject jsonObject = JSONObject.parseObject(res, JSONObject.class);
String code = jsonObject.get("code").toString();
if (code.equals("0")) {
String data = jsonObject.get("data") == null ? "" : jsonObject.get("data").toString();
if (StringUtils.isNotBlank(data)) {
return data;
}
}
}
return null;
}
}

2.3、压力主工具类

public class PressureTestTools {
public static void main(String[] args) {
try {
press("http://www.baidu.com", 10000, 10);
} catch (Exception e) {
e.printStackTrace();
}
}
private static void press(final String url, int requestTotalCount, int threadCount) throws Exception {
if (StringUtils.isEmpty(url) || threadCount <= 0 || requestTotalCount <= 0) {
throw new IllegalArgumentException("参数错误!");
}
long totalExpandTime = 0L;
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(threadCount);
for (int i=requestTotalCount; i>0; i--) {
Future expandTime = fixedThreadPool.submit(() -> {
System.out.println(">>> " + Thread.currentThread().getName() + " start ...");
long startTime = System.currentTimeMillis();
String result = HttpUtil.doGet(url);
// System.out.println("result = " + result);
long endTime = System.currentTimeMillis();
System.out.println(">>> " + Thread.currentThread().getName() + ", expend = " + (endTime - startTime));
return (endTime - startTime);
});
Object sigleTime = expandTime.get();
totalExpandTime = totalExpandTime + Long.parseLong(sigleTime.toString());
}
fixedThreadPool.shutdown();
System.out.println("共" + requestTotalCount + "个请求," + threadCount + "个线程, 共耗时:" + totalExpandTime + "毫秒");
}
}
//:)~~

2.4、结果

通过对http://www.baidu.com测试,结果如下:



用户头像

stars

关注

架构师的肚是杂货铺 2018.02.19 加入

还未添加个人简介

评论

发布
暂无评论
自己写的压力测试工具