用例步骤表触发JMeter调用接口调整

This commit is contained in:
liangdaliang
2025-02-18 11:46:49 +08:00
parent c552e6abbd
commit 4f60c8b647
5 changed files with 18 additions and 13 deletions

View File

@@ -63,8 +63,8 @@ public class JMeterUtil {
* @param jmeterHomePath
* @return
*/
public static String getJmeterResult(Long id, String url, int port, String method, String paramJson, String requestHeader, String jmeterHomePath) {
String result = "";
public static Map<String, String> getJmeterResult(Long id, String url, int port, String method, String paramJson, String requestHeader, String jmeterHomePath) {
Map<String, String> result = null;
try {
// 1. 初始化 JMeter
JMeterUtils.loadJMeterProperties(jmeterHomePath + "/bin/jmeter.properties");
@@ -379,7 +379,7 @@ public class JMeterUtil {
* @param filePath
* @return
*/
private static String getResultMessageFromFile(String filePath) {
private static Map<String, String> getResultMessageFromFile(String filePath) {
File file = new File(filePath);
if (!file.exists()) {
return null;
@@ -395,12 +395,16 @@ public class JMeterUtil {
NodeList httpSampleList = document.getElementsByTagName("httpSample");
Node httpSampleNode = httpSampleList.item(0);
if (httpSampleNode.getNodeType() == Node.ELEMENT_NODE) {
Map<String, String> resultMap = new HashMap<>();
Element httpSampleElement = (Element) httpSampleNode;
String costMiliseconds = httpSampleElement.getAttribute("t");
resultMap.put("costMiliseconds", costMiliseconds);
// 提取子节点<responseData>的内容
NodeList responseDataList = httpSampleElement.getElementsByTagName("responseData");
if (responseDataList.getLength() > 0) {
Element responseDataElement = (Element) responseDataList.item(0);
return responseDataElement.getTextContent();
resultMap.put("costMiliseconds", responseDataElement.getTextContent());
return resultMap;
}
}
} catch (Exception e) {
@@ -415,7 +419,7 @@ public class JMeterUtil {
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);
String result = getJmeterResult(123L, "http://127.0.0.1:3000/api/items/user", 3000, "GET", paramJson, requestHeader, jmeterHomePath);
System.out.println(result);
getJmeterResult(123L, "http://127.0.0.1:3000/api/items/user", 3000, "GET", paramJson, requestHeader, jmeterHomePath);
// System.out.println(result);
}
}

View File

@@ -72,7 +72,7 @@ public class TestCaseStep extends BaseEntity {
/** http 请求端口号 */
@Excel(name = "http 请求端口号")
private String apiPort;
private Integer apiPort;
/** http 请求协议(http, https) */
@Excel(name = "http 请求协议(http, https)")

View File

@@ -3,6 +3,7 @@ package com.test.test.service;
import com.test.test.domain.TestCaseStep;
import java.util.List;
import java.util.Map;
/**
* 用例步骤Service接口
@@ -67,5 +68,5 @@ public interface ITestCaseStepService
* @param jmeterHomePath jmeter安装路径
* @return 结果
*/
public String executeJmeterTestCaseStepById(Long id, String jmeterHomePath);
public Map<String, String> executeJmeterTestCaseStepById(Long id, String jmeterHomePath);
}

View File

@@ -10,6 +10,7 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 用例步骤Service业务层处理
@@ -106,13 +107,12 @@ public class TestCaseStepServiceImpl implements ITestCaseStepService
* @return 结果
*/
@Override
public String executeJmeterTestCaseStepById(Long id, String jmeterHomePath) {
public Map<String, String> executeJmeterTestCaseStepById(Long id, String jmeterHomePath) {
TestCaseStep testCaseStep = testCaseStepMapper.selectTestCaseStepById(id);
if (testCaseStep == null) {
log.error("根据主键id{}未查询到用例步骤计划", id);
return null;
}
String result = null;
String url = testCaseStep.getRequestUrl();
String method = testCaseStep.getRequestMethod().toUpperCase();
if (!method.equals("GET") && !method.equals("POST")) {
@@ -120,9 +120,9 @@ public class TestCaseStepServiceImpl implements ITestCaseStepService
return null;
}
String requestHeader = testCaseStep.getRequestHeader();
result = JMeterUtil.getJmeterResult(id, url, Integer.parseInt(testCaseStep.getApiPort()), testCaseStep.getRequestMethod(), testCaseStep.getRequestParams(), requestHeader, jmeterHomePath);
Map<String, String> resultMap = JMeterUtil.getJmeterResult(id, url, testCaseStep.getApiPort(), testCaseStep.getRequestMethod(), testCaseStep.getRequestParams(), requestHeader, jmeterHomePath);
return result;
return resultMap;
}
}

View File

@@ -55,7 +55,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
<if test="requestParams != null and requestParams != ''"> and request_params = #{requestParams}</if>
<if test="apiHttpId != null "> and api_http_id = #{apiHttpId}</if>
<if test="apiHost != null and apiHost != ''"> and api_host = #{apiHost}</if>
<if test="apiPort != null and apiPort != ''"> and api_port = #{apiPort}</if>
<if test="apiPort != null "> and api_port = #{apiPort}</if>
<if test="apiProtocol != null and apiProtocol != ''"> and api_protocol = #{apiProtocol}</if>
<if test="sqlCommand != null and sqlCommand != ''"> and sql_command = #{sqlCommand}</if>
<if test="count != null "> and count = #{count}</if>