循环、轮询提交

This commit is contained in:
liangdaliang
2025-02-20 15:42:10 +08:00
parent 1eabec1d74
commit 99200c7f63
18 changed files with 1289 additions and 30 deletions

View File

@@ -146,6 +146,11 @@
<artifactId>gson</artifactId>
<version>2.10.1</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>

View File

@@ -0,0 +1,112 @@
package com.test.common.core.domain.model;
/**
* @author liangdaliang
* @DescriptionJmeter调用参数
* @date 2025-02-19 10:55
*/
public class JmeterRequest {
/**
* 用例步骤id
*/
private Long id;
/**
* 请求完整url
*/
private String url;
/**
* 请求端口号
*/
private int port;
/**
* 请求方式
*/
private String method;
/**
* 请求body
*/
private String requestBody;
/**
* 请求参数url?后面的参数
*/
private String requestParams;
/**
* 请求头Json
*/
private String requestHeader;
/**
* jmeter安装home路径
*/
private String jmeterHomePath;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getMethod() {
return method;
}
public void setMethod(String method) {
this.method = method;
}
public String getRequestBody() {
return requestBody;
}
public void setRequestBody(String requestBody) {
this.requestBody = requestBody;
}
public String getRequestParams() {
return requestParams;
}
public void setRequestParams(String requestParams) {
this.requestParams = requestParams;
}
public String getRequestHeader() {
return requestHeader;
}
public void setRequestHeader(String requestHeader) {
this.requestHeader = requestHeader;
}
public String getJmeterHomePath() {
return jmeterHomePath;
}
public void setJmeterHomePath(String jmeterHomePath) {
this.jmeterHomePath = jmeterHomePath;
}
}

View File

@@ -2,6 +2,9 @@ package com.test.common.utils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import com.test.common.core.domain.model.JmeterRequest;
import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.config.gui.ArgumentsPanel;
import org.apache.jmeter.control.LoopController;
@@ -54,16 +57,18 @@ public class JMeterUtil {
/**
* 生成jmeter测试执行计划并获取执行结果
* @param id
* @param url
* @param port
* @param method
* @param paramJson
* @param requestHeader
* @param jmeterHomePath
* @param jmeterRequest
* @return
*/
public static Map<String, String> getJmeterResult(Long id, String url, int port, String method, String paramJson, String requestHeader, String jmeterHomePath) {
public static Map<String, String> getJmeterResult(JmeterRequest jmeterRequest) {
Long id = jmeterRequest.getId();
String url = jmeterRequest.getUrl();
int port = jmeterRequest.getPort();
String method = jmeterRequest.getMethod().toUpperCase();
String requestBody = jmeterRequest.getRequestBody();
String requestParams = jmeterRequest.getRequestParams();
String requestHeader = jmeterRequest.getRequestHeader();
String jmeterHomePath = jmeterRequest.getJmeterHomePath();
Map<String, String> result = null;
try {
// 1. 初始化 JMeter
@@ -83,13 +88,14 @@ public class JMeterUtil {
httpSampler = createPostHttpSampler(
url, // 请求地址
port,
paramJson // 请求参数
requestBody, // 请求
requestParams // 查询参数
);
} else {
httpSampler = createGetHttpSampler(
url, // 请求地址
port,
paramJson // 请求参数
requestParams // 查询参数
);
}
// 5. 创建请求头管理器
@@ -189,11 +195,11 @@ public class JMeterUtil {
* 创建get请求方式
* @param urlString
* @param port
* @param requestJson
* @param requestParams
* @return
* @throws MalformedURLException
*/
private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, String requestJson) throws MalformedURLException {
private static HTTPSamplerProxy createGetHttpSampler(String urlString, int port, String requestParams) throws MalformedURLException {
URL url = new URL(urlString);
// 提取域名(不包括端口号)
String domain = url.getHost();
@@ -204,6 +210,9 @@ public class JMeterUtil {
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
httpSampler.setDomain(domain); // 提取域名
if (!StringUtils.isEmpty(requestParams)) {
path = path + "?" + requestParams;
}
httpSampler.setPath(path); // 提取路径
httpSampler.setPort(port);
httpSampler.setMethod("GET");
@@ -211,17 +220,17 @@ public class JMeterUtil {
httpSampler.setUseKeepAlive(true);
// 添加请求参数
Map<String, String> parameters = new HashMap<>();
if (!StringUtils.isEmpty(requestJson)) {
Gson gson = new Gson();
// 定义 Map 的类型
Type type = new TypeToken<Map<String, String>>() {}.getType();
// 将 JSON 字符串转换为 Map
parameters = gson.fromJson(requestJson, type);
path += "?";
path += mapToQueryString(parameters);
httpSampler.setPath(path); // 提取路径
}
// Map<String, String> parameters = new HashMap<>();
// if (!StringUtils.isEmpty(requestJson)) {
// Gson gson = new Gson();
// // 定义 Map 的类型
// Type type = new TypeToken<Map<String, String>>() {}.getType();
// // 将 JSON 字符串转换为 Map
// parameters = gson.fromJson(requestJson, type);
// path += "?";
// path += mapToQueryString(parameters);
// httpSampler.setPath(path); // 提取路径
// }
return httpSampler;
}
@@ -249,11 +258,12 @@ public class JMeterUtil {
* 创建post请求方式
* @param urlString
* @param port
* @param requestJson
* @param requestBody
* @param requestParams
* @return
* @throws MalformedURLException
*/
private static HTTPSamplerProxy createPostHttpSampler(String urlString, int port, String requestJson) throws MalformedURLException {
private static HTTPSamplerProxy createPostHttpSampler(String urlString, int port, String requestBody, String requestParams) throws MalformedURLException {
URL url = new URL(urlString);
// 提取域名(不包括端口号)
String domain = url.getHost();
@@ -265,15 +275,18 @@ public class JMeterUtil {
httpSampler.setProperty(TestElement.TEST_CLASS, HTTPSamplerProxy.class.getName());
httpSampler.setProperty(TestElement.GUI_CLASS, HttpTestSampleGui.class.getName());
httpSampler.setDomain(domain);
if (!StringUtils.isEmpty(requestParams)) {
path = path + "?" + requestParams;
}
httpSampler.setPath(path);
httpSampler.setPort(port);
httpSampler.setMethod("POST");
httpSampler.setConnectTimeout("5000");
httpSampler.setUseKeepAlive(true);
// 设置请求头为 JSON
// 设置请求
httpSampler.setPostBodyRaw(true); // 使用原始请求体
httpSampler.addNonEncodedArgument("", requestJson, "");
httpSampler.addNonEncodedArgument("", requestBody, "");
return httpSampler;
}
@@ -398,12 +411,21 @@ public class JMeterUtil {
Map<String, String> resultMap = new HashMap<>();
Element httpSampleElement = (Element) httpSampleNode;
String costMiliseconds = httpSampleElement.getAttribute("t");
String responseCode = httpSampleElement.getAttribute("rc");
resultMap.put("responseCode", responseCode);
resultMap.put("costMiliseconds", costMiliseconds);
// 提取子节点<responseData>的内容
NodeList responseDataList = httpSampleElement.getElementsByTagName("responseData");
if (responseDataList.getLength() > 0) {
Element responseDataElement = (Element) responseDataList.item(0);
resultMap.put("costMiliseconds", responseDataElement.getTextContent());
resultMap.put("responseBody", responseDataElement.getTextContent());
return resultMap;
}
// 提取子节点<responseHeader>的内容
NodeList responseHeaderList = httpSampleElement.getElementsByTagName("responseHeader");
if (responseHeaderList.getLength() > 0) {
Element responseHeaderElement = (Element) responseHeaderList.item(0);
resultMap.put("responseHeader", responseHeaderElement.getTextContent());
return resultMap;
}
}
@@ -413,13 +435,56 @@ public class JMeterUtil {
return null;
}
/**
* 从响应头中读取Map信息结构
* @param responseHeader
* @return
*/
public static Map<String, String> parseHeaders(String responseHeader) {
Map<String, String> headerMap = new HashMap<>();
// 按行拆分
String[] lines = responseHeader.split("\n");
// 遍历每一行
for (String line : lines) {
// 忽略空行和 HTTP 状态行
if (line.trim().isEmpty() || line.startsWith("HTTP/")) {
continue;
}
// 按冒号拆分键值对
int colonIndex = line.indexOf(":");
if (colonIndex > 0) {
String key = line.substring(0, colonIndex).trim();
String value = line.substring(colonIndex + 1).trim();
headerMap.put(key, value);
}
}
return headerMap;
}
/**
* 根据 JSONPath 表达式从 JSON 中提取字段值
*
* @param json JSON 字符串
* @param expression JSONPath 表达式
* @return 字段值,如果字段不存在则返回 null
*/
public static String extractFieldValue(String json, String expression) {
try {
Object result = JsonPath.read(json, expression);
return result != null ? result.toString() : null;
} catch (PathNotFoundException e) {
// 如果字段不存在,返回 null
return null;
}
}
public static void main(String[] args) {
String paramJson = "{\"md5Value\":\"value1\",\"fileName\":\"value2\"}";
paramJson = "{\"name\":\"John\",\"city\":\"New York\",\"age\":30}";
String requestHeader = "{\"Content-Type\":\"application/json; charset=UTF-8\",\"Authorization\":\"Bearer token123\"}";
String jmeterHomePath = "D:/apache-jmeter-5.4.3";
// String result = getJmeterResult("http://127.0.0.1:3000/api/items/add", 3000, "POST", paramJson, requestHeader, jmeterHomePath);
getJmeterResult(123L, "http://127.0.0.1:3000/api/items/user", 3000, "GET", paramJson, requestHeader, jmeterHomePath);
// getJmeterResult(123L, "http://127.0.0.1:3000/api/items/user", 3000, "GET", paramJson, requestHeader, jmeterHomePath);
// System.out.println(result);
}
}