Merge remote-tracking branch 'origin/master'

This commit is contained in:
2025-02-18 13:58:40 +08:00
5 changed files with 18 additions and 13 deletions

View File

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

View File

@@ -72,7 +72,7 @@ public class TestCaseStep extends BaseEntity {
/** http 请求端口号 */ /** http 请求端口号 */
@Excel(name = "http 请求端口号") @Excel(name = "http 请求端口号")
private String apiPort; private Integer apiPort;
/** http 请求协议(http, https) */ /** http 请求协议(http, https) */
@Excel(name = "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 com.test.test.domain.TestCaseStep;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 用例步骤Service接口 * 用例步骤Service接口
@@ -67,5 +68,5 @@ public interface ITestCaseStepService
* @param jmeterHomePath jmeter安装路径 * @param jmeterHomePath jmeter安装路径
* @return 结果 * @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 org.springframework.stereotype.Service;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* 用例步骤Service业务层处理 * 用例步骤Service业务层处理
@@ -106,13 +107,12 @@ public class TestCaseStepServiceImpl implements ITestCaseStepService
* @return 结果 * @return 结果
*/ */
@Override @Override
public String executeJmeterTestCaseStepById(Long id, String jmeterHomePath) { public Map<String, String> executeJmeterTestCaseStepById(Long id, String jmeterHomePath) {
TestCaseStep testCaseStep = testCaseStepMapper.selectTestCaseStepById(id); TestCaseStep testCaseStep = testCaseStepMapper.selectTestCaseStepById(id);
if (testCaseStep == null) { if (testCaseStep == null) {
log.error("根据主键id{}未查询到用例步骤计划", id); log.error("根据主键id{}未查询到用例步骤计划", id);
return null; return null;
} }
String result = null;
String url = testCaseStep.getRequestUrl(); String url = testCaseStep.getRequestUrl();
String method = testCaseStep.getRequestMethod().toUpperCase(); String method = testCaseStep.getRequestMethod().toUpperCase();
if (!method.equals("GET") && !method.equals("POST")) { if (!method.equals("GET") && !method.equals("POST")) {
@@ -120,9 +120,9 @@ public class TestCaseStepServiceImpl implements ITestCaseStepService
return null; return null;
} }
String requestHeader = testCaseStep.getRequestHeader(); 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="requestParams != null and requestParams != ''"> and request_params = #{requestParams}</if>
<if test="apiHttpId != null "> and api_http_id = #{apiHttpId}</if> <if test="apiHttpId != null "> and api_http_id = #{apiHttpId}</if>
<if test="apiHost != null and apiHost != ''"> and api_host = #{apiHost}</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="apiProtocol != null and apiProtocol != ''"> and api_protocol = #{apiProtocol}</if>
<if test="sqlCommand != null and sqlCommand != ''"> and sql_command = #{sqlCommand}</if> <if test="sqlCommand != null and sqlCommand != ''"> and sql_command = #{sqlCommand}</if>
<if test="count != null "> and count = #{count}</if> <if test="count != null "> and count = #{count}</if>