Jmeter Json提取

This commit is contained in:
liangdaliang
2025-04-24 09:15:40 +08:00
parent bfea486350
commit d5b6341b21
4 changed files with 72 additions and 74 deletions

View File

@@ -162,6 +162,11 @@
<artifactId>selenium-java</artifactId>
<version>4.30.0</version> <!-- 根据需要选择版本 -->
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>

View File

@@ -1,10 +1,15 @@
package com.test.common.core.domain.model;
import lombok.Data;
import java.util.List;
/**
* @author liangdaliang
* @DescriptionJmeter调用参数
* @date 2025-02-19 10:55
*/
@Data
public class JmeterRequest {
/**
* 用例步骤id
@@ -51,75 +56,8 @@ public class JmeterRequest {
*/
private String jmeterHomePath;
public String getTestCaseName() {
return testCaseName;
}
public void setTestCaseName(String testCaseName) {
this.testCaseName = testCaseName;
}
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;
}
/**
* jmeter提取表达式
*/
private List<String> extractionRuleList;
}

View File

@@ -11,6 +11,8 @@ import org.apache.jmeter.control.LoopController;
import org.apache.jmeter.control.gui.LoopControlPanel;
import org.apache.jmeter.control.gui.TestPlanGui;
import org.apache.jmeter.engine.StandardJMeterEngine;
import org.apache.jmeter.extractor.json.jsonpath.JSONPostProcessor;
import org.apache.jmeter.extractor.json.jsonpath.gui.JSONPostProcessorGui;
import org.apache.jmeter.protocol.http.control.Header;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.apache.jmeter.protocol.http.control.gui.HttpTestSampleGui;
@@ -92,8 +94,8 @@ public class JMeterGroupUtil {
List<ResultCollector> resultCollector = getResultCollector(replayLogPath, summaryReportPath);
// 5. 构建测试树新增每个用例下的http请求节点
HashTree firstTreeTestPlan = new HashTree();
HashTree secondHashTree = new HashTree();
HashTree firstTreeTestPlan = new ListedHashTree();
HashTree secondHashTree = new ListedHashTree();
ListedHashTree thirdHashTree = new ListedHashTree();
dealHttpHashTreeWithRequestList(thirdHashTree, jmeterGroupRequest.getJmeterRequestList());
@@ -194,11 +196,18 @@ public class JMeterGroupUtil {
}
}
HeaderManager headerManager = createHeaderManager(headerMap);
List<String> extractionRuleList = jmeterRequest.getExtractionRuleList();
if (headerManager != null) {
fourHashTree.add(headerManager);
createJsonPostProcessor(extractionRuleList, fourHashTree);
thirdHashTree.add(httpSampler, fourHashTree);
} else {
thirdHashTree.add(httpSampler);
createJsonPostProcessor(extractionRuleList, fourHashTree);
if (fourHashTree.isEmpty()) {
thirdHashTree.add(httpSampler);
} else {
thirdHashTree.add(httpSampler, fourHashTree);
}
}
} catch (Exception e) {
@@ -208,6 +217,34 @@ public class JMeterGroupUtil {
}
}
/**
* 创建Json参数提取
* @param extractionRuleList
* @param fourHashTree
* @return
*/
private static void createJsonPostProcessor(List<String> extractionRuleList, HashTree fourHashTree) {
if (extractionRuleList != null) {
for (String extractionRule : extractionRuleList) {
String[] array = extractionRule.split(",");
JSONPostProcessor jsonExtractor = new JSONPostProcessor();
jsonExtractor.setName("提取" + array[0]);
jsonExtractor.setProperty(TestElement.TEST_CLASS, JSONPostProcessor.class.getName());
jsonExtractor.setProperty(TestElement.GUI_CLASS, JSONPostProcessorGui.class.getName());
jsonExtractor.setEnabled(true);
if (array[1].startsWith("$")) {
jsonExtractor.setJsonPathExpressions(array[1]); // JSONPath 表达式
} else {
jsonExtractor.setJsonPathExpressions("$." + array[1]); // JSONPath 表达式
}
jsonExtractor.setMatchNumbers("1"); // 0表示随机1表示第一个
jsonExtractor.setRefNames(array[0]); // 存储变量名
jsonExtractor.setDefaultValues("");
fourHashTree.add(jsonExtractor);
}
}
}
/**
* 创建测试计划
* @param name

View File

@@ -2,14 +2,17 @@ package com.test.test.service.impl;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.test.common.core.domain.model.*;
import com.test.common.utils.DateUtils;
import com.test.common.utils.JMeterGroupUtil;
import com.test.common.utils.SecurityUtils;
import com.test.common.utils.StringUtils;
import com.test.test.domain.PerformanceTest;
import com.test.test.domain.PerformanceTestCase;
import com.test.test.domain.PerformanceTestCaseReport;
import com.test.test.domain.TestCaseStep;
import com.test.test.domain.qo.ExtractionRule;
import com.test.test.mapper.PerformanceTestCaseMapper;
import com.test.test.mapper.PerformanceTestCaseReportMapper;
import com.test.test.mapper.PerformanceTestMapper;
@@ -21,6 +24,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -164,6 +168,20 @@ public class PerformanceTestCaseReportServiceImpl implements IPerformanceTestCas
jmeterRequest.setRequestBody(testCaseStep.getRequestBody());
jmeterRequest.setRequestParams(testCaseStep.getRequestParams());
jmeterRequest.setRequestHeader(testCaseStep.getRequestHeader());
// Json提取表达式
String assignment = testCaseStep.getAssignment();
if (!StringUtils.isEmpty(assignment) && !"[]".equals(assignment)) {
Gson gson = new Gson();
Type ruleListType = new TypeToken<List<ExtractionRule>>() {}.getType();
List<ExtractionRule> rules = gson.fromJson(assignment, ruleListType);
if (!CollectionUtils.isEmpty(rules)) {
List<String> extractionRuleList = new ArrayList<>();
for (ExtractionRule rule : rules) {
extractionRuleList.add(rule.getName() + "," + rule.getPath());
jmeterRequest.setExtractionRuleList(extractionRuleList);
}
}
}
log.info("getRequestHeader:{}", jmeterRequest.getRequestHeader());
return jmeterRequest;
}