refactor: 增加预期结束时间的参数

This commit is contained in:
Captain.B 2021-03-31 19:05:10 +08:00
parent d3efe4bb7f
commit fa08d59725
3 changed files with 53 additions and 4 deletions

View File

@ -11,6 +11,7 @@ public class KafkaProperties {
public static final String KAFKA_PREFIX = "kafka"; public static final String KAFKA_PREFIX = "kafka";
private String acks = "0"; // 不要设置all private String acks = "0"; // 不要设置all
private String expectedDelayEndTime = "30000"; // 30s
private String topic; private String topic;
private String fields; private String fields;
private String timestamp; private String timestamp;

View File

@ -25,8 +25,9 @@ public class JmeterFileController {
@GetMapping("download") @GetMapping("download")
public ResponseEntity<byte[]> downloadJmeterFiles(@RequestParam("testId") String testId, @RequestParam("resourceId") String resourceId, public ResponseEntity<byte[]> downloadJmeterFiles(@RequestParam("testId") String testId, @RequestParam("resourceId") String resourceId,
@RequestParam("ratio") double ratio, @RequestParam("startTime") long startTime, @RequestParam("ratio") double ratio,
@RequestParam("reportId") String reportId, @RequestParam("resourceIndex") int resourceIndex) { @RequestParam("reportId") String reportId, @RequestParam("resourceIndex") int resourceIndex) {
long startTime = System.currentTimeMillis();
byte[] bytes = jmeterFileService.downloadZip(testId, resourceId, ratio, startTime, reportId, resourceIndex); byte[] bytes = jmeterFileService.downloadZip(testId, resourceId, ratio, startTime, reportId, resourceIndex);
return ResponseEntity.ok() return ResponseEntity.ok()
.contentType(MediaType.parseMediaType("application/octet-stream")) .contentType(MediaType.parseMediaType("application/octet-stream"))

View File

@ -29,6 +29,7 @@ public class JmeterDocumentParser implements DocumentParser {
private final static String HASH_TREE_ELEMENT = "hashTree"; private final static String HASH_TREE_ELEMENT = "hashTree";
private final static String TEST_PLAN = "TestPlan"; private final static String TEST_PLAN = "TestPlan";
private final static String STRING_PROP = "stringProp"; private final static String STRING_PROP = "stringProp";
private final static String BOOL_PROP = "boolProp";
private final static String COLLECTION_PROP = "collectionProp"; private final static String COLLECTION_PROP = "collectionProp";
private final static String CONCURRENCY_THREAD_GROUP = "com.blazemeter.jmeter.threads.concurrency.ConcurrencyThreadGroup"; private final static String CONCURRENCY_THREAD_GROUP = "com.blazemeter.jmeter.threads.concurrency.ConcurrencyThreadGroup";
private final static String VARIABLE_THROUGHPUT_TIMER = "kg.apc.jmeter.timers.VariableThroughputTimer"; private final static String VARIABLE_THROUGHPUT_TIMER = "kg.apc.jmeter.timers.VariableThroughputTimer";
@ -94,6 +95,7 @@ public class JmeterDocumentParser implements DocumentParser {
processCheckoutDnsCacheManager(ele); processCheckoutDnsCacheManager(ele);
processCheckoutArguments(ele); processCheckoutArguments(ele);
processCheckoutResponseAssertion(ele); processCheckoutResponseAssertion(ele);
processCheckoutSerializeThreadgroups(ele);
} else if (nodeNameEquals(ele, CONCURRENCY_THREAD_GROUP)) { } else if (nodeNameEquals(ele, CONCURRENCY_THREAD_GROUP)) {
processThreadGroupName(ele); processThreadGroupName(ele);
processCheckoutTimer(ele); processCheckoutTimer(ele);
@ -141,6 +143,21 @@ public class JmeterDocumentParser implements DocumentParser {
} }
} }
private void processCheckoutSerializeThreadgroups(Element element) {
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node item = childNodes.item(i);
if (nodeNameEquals(item, BOOL_PROP)) {
String serializeName = ((Element) item).getAttribute("name");
if (StringUtils.equals(serializeName, "TestPlan.serialize_threadgroups")) {
// 保存线程组是否是顺序执行
context.addProperty("serialize_threadgroups", item.getTextContent());
break;
}
}
}
}
private void processArgumentFiles(Element element) { private void processArgumentFiles(Element element) {
NodeList childNodes = element.getChildNodes(); NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) { for (int i = 0; i < childNodes.getLength(); i++) {
@ -479,7 +496,7 @@ public class JmeterDocumentParser implements DocumentParser {
item.appendChild(elementProp); item.appendChild(elementProp);
} }
} }
if (item instanceof Element && nodeNameEquals(item, "boolProp") if (item instanceof Element && nodeNameEquals(item, BOOL_PROP)
&& org.apache.commons.lang3.StringUtils.equals(((Element) item).getAttribute("name"), "DNSCacheManager.isCustomResolver")) { && org.apache.commons.lang3.StringUtils.equals(((Element) item).getAttribute("name"), "DNSCacheManager.isCustomResolver")) {
item.getFirstChild().setNodeValue("true"); item.getFirstChild().setNodeValue("true");
} }
@ -511,7 +528,7 @@ public class JmeterDocumentParser implements DocumentParser {
} }
private Element createBoolProp(Document document, String name, boolean value) { private Element createBoolProp(Document document, String name, boolean value) {
Element boolProp = document.createElement("boolProp"); Element boolProp = document.createElement(BOOL_PROP);
boolProp.setAttribute("name", name); boolProp.setAttribute("name", name);
boolProp.appendChild(document.createTextNode(String.valueOf(value))); boolProp.appendChild(document.createTextNode(String.valueOf(value)));
return boolProp; return boolProp;
@ -569,6 +586,8 @@ public class JmeterDocumentParser implements DocumentParser {
collectionProp.appendChild(createKafkaProp(document, "test.name", context.getTestName())); collectionProp.appendChild(createKafkaProp(document, "test.name", context.getTestName()));
collectionProp.appendChild(createKafkaProp(document, "test.startTime", context.getStartTime().toString())); collectionProp.appendChild(createKafkaProp(document, "test.startTime", context.getStartTime().toString()));
collectionProp.appendChild(createKafkaProp(document, "test.reportId", context.getReportId())); collectionProp.appendChild(createKafkaProp(document, "test.reportId", context.getReportId()));
collectionProp.appendChild(createKafkaProp(document, "test.expectedEndTime", (String) context.getProperty("expectedEndTime")));
collectionProp.appendChild(createKafkaProp(document, "test.expectedDelayEndTime", kafkaProperties.getExpectedDelayEndTime())); // 30s
elementProp.appendChild(collectionProp); elementProp.appendChild(collectionProp);
// set elementProp // set elementProp
@ -710,6 +729,8 @@ public class JmeterDocumentParser implements DocumentParser {
default: default:
break; break;
} }
// 处理预计结束时间
processExpectedEndTime(duration);
threadGroup.setAttribute("enabled", enabled); threadGroup.setAttribute("enabled", enabled);
if (BooleanUtils.toBoolean(deleted)) { if (BooleanUtils.toBoolean(deleted)) {
@ -815,6 +836,8 @@ public class JmeterDocumentParser implements DocumentParser {
default: default:
break; break;
} }
// 处理预计结束时间
processExpectedEndTime(hold);
threadGroup.setAttribute("enabled", enabled); threadGroup.setAttribute("enabled", enabled);
if (BooleanUtils.toBoolean(deleted)) { if (BooleanUtils.toBoolean(deleted)) {
@ -835,6 +858,27 @@ public class JmeterDocumentParser implements DocumentParser {
threadGroup.appendChild(createStringProp(document, "Unit", "S")); threadGroup.appendChild(createStringProp(document, "Unit", "S"));
} }
private void processExpectedEndTime(String duration) {
long startTime = context.getStartTime();
Long d = Long.parseLong(duration);
Object serialize = context.getProperty("TestPlan.serialize_threadgroups");
String expectedEndTime = (String) context.getProperty("expectedEndTime");
if (StringUtils.isBlank(expectedEndTime)) {
expectedEndTime = startTime + "";
}
long endTime = Long.parseLong(expectedEndTime);
if (BooleanUtils.toBoolean((String) serialize)) {
// 顺序执行线程组
context.addProperty("expectedEndTime", String.valueOf(endTime + d * 1000));
} else {
// 同时执行线程组
if (endTime < startTime + d * 1000) {
context.addProperty("expectedEndTime", String.valueOf(startTime + d * 1000));
}
}
}
private void processIterationThreadGroup(Element threadGroup) { private void processIterationThreadGroup(Element threadGroup) {
// 检查 threadgroup 后面的hashtree是否为空 // 检查 threadgroup 后面的hashtree是否为空
Node hashTree = threadGroup.getNextSibling(); Node hashTree = threadGroup.getNextSibling();
@ -903,8 +947,11 @@ public class JmeterDocumentParser implements DocumentParser {
threadGroup.appendChild(createStringProp(document, "ThreadGroup.duration", "10")); threadGroup.appendChild(createStringProp(document, "ThreadGroup.duration", "10"));
threadGroup.appendChild(createStringProp(document, "ThreadGroup.delay", "")); threadGroup.appendChild(createStringProp(document, "ThreadGroup.delay", ""));
threadGroup.appendChild(createBoolProp(document, "ThreadGroup.same_user_on_next_iteration", true)); threadGroup.appendChild(createBoolProp(document, "ThreadGroup.same_user_on_next_iteration", true));
}
// 处理预计结束时间 (按照迭代次数 * 线程数)s
String duration = String.valueOf(Long.parseLong(loops) * Long.parseLong(threads));
processExpectedEndTime(duration);
}
private void processCheckoutTimer(Element element) { private void processCheckoutTimer(Element element) {
/* /*