From 2bed94efceafe12affc67fd1ccfad0f925fac87e Mon Sep 17 00:00:00 2001 From: q4speed Date: Wed, 6 May 2020 14:21:10 +0800 Subject: [PATCH] NaN --- .../api/jmeter/APIBackendListenerClient.java | 28 +++++++++++++++++-- .../api/service/ApiTestService.java | 24 ++++++++++------ .../components/api/test/ApiTestConfig.vue | 1 + .../business/components/api/test/model/JMX.js | 28 +++++++++++++++++++ .../api/test/model/ScenarioModel.js | 23 +++++++-------- 5 files changed, 82 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index 4ebf5ac930..31f2e7771f 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -1,5 +1,6 @@ package io.metersphere.api.jmeter; +import org.apache.jmeter.assertions.AssertionResult; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.visualizers.backend.AbstractBackendListenerClient; import org.apache.jmeter.visualizers.backend.BackendListenerContext; @@ -14,8 +15,31 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl @Override public void handleSampleResults(List sampleResults, BackendListenerContext context) { - System.out.println(context); - System.out.println(sampleResults.get(0).getAssertionResults()); + System.out.println(context.getParameter("id")); + sampleResults.forEach(result -> { + for (AssertionResult assertionResult : result.getAssertionResults()) { + System.out.println(assertionResult.getName() + ": " + assertionResult.isError()); + System.out.println(assertionResult.getName() + ": " + assertionResult.isFailure()); + System.out.println(assertionResult.getName() + ": " + assertionResult.getFailureMessage()); + } + + println("getSampleLabel", result.getSampleLabel()); + println("getErrorCount", result.getErrorCount()); + println("getRequestHeaders", result.getRequestHeaders()); + println("getResponseHeaders", result.getResponseHeaders()); + println("getSampleLabel", result.getSampleLabel()); + println("getSampleLabel", result.getSampleLabel()); + println("getResponseCode", result.getResponseCode()); + println("getResponseCode size", result.getResponseData().length); + println("getLatency", result.getLatency()); + println("end - start", result.getEndTime() - result.getStartTime()); + println("getTimeStamp", result.getTimeStamp()); + println("getTime", result.getTime()); + }); System.err.println(count.addAndGet(sampleResults.size())); } + + private void println(String name, Object value) { + System.out.println(name + ": " + value); + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestService.java index a6d544daa8..b4d1d0720c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestService.java @@ -14,17 +14,11 @@ import io.metersphere.commons.exception.MSException; import io.metersphere.i18n.Translator; import io.metersphere.service.FileService; import org.apache.commons.lang3.StringUtils; -import org.apache.jmeter.save.SaveService; -import org.apache.jmeter.services.FileServer; -import org.apache.jorphan.collections.HashTree; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.CollectionUtils; -import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; -import java.io.File; -import java.io.FileInputStream; import java.io.IOException; import java.util.List; import java.util.UUID; @@ -89,14 +83,15 @@ public class ApiTestService { apiTestMapper.deleteByPrimaryKey(request.getId()); } - public void run(SaveAPITestRequest request, List files) { - save(request, files); + public String run(SaveAPITestRequest request, List files) { + String id = save(request, files); try { changeStatus(request.getId(), APITestStatus.Running); jMeterService.run(files.get(0).getInputStream()); } catch (IOException e) { MSException.throwException(Translator.get("api_load_script_error")); } + return id; } public void changeStatus(String id, APITestStatus status) { @@ -145,9 +140,20 @@ public class ApiTestService { if (!CollectionUtils.isEmpty(ApiTestFiles)) { final List fileIds = ApiTestFiles.stream().map(ApiTestFile::getFileId).collect(Collectors.toList()); - fileService.deleteFileByIds(fileIds); } } + private ApiTestFile getFileByTestId(String testId) { + ApiTestFileExample ApiTestFileExample = new ApiTestFileExample(); + ApiTestFileExample.createCriteria().andTestIdEqualTo(testId); + final List ApiTestFiles = apiTestFileMapper.selectByExample(ApiTestFileExample); + apiTestFileMapper.selectByExample(ApiTestFileExample); + if (!CollectionUtils.isEmpty(ApiTestFiles)) { + return ApiTestFiles.get(0); + } else { + return null; + } + } + } diff --git a/frontend/src/business/components/api/test/ApiTestConfig.vue b/frontend/src/business/components/api/test/ApiTestConfig.vue index 8d5c494025..808b121781 100644 --- a/frontend/src/business/components/api/test/ApiTestConfig.vue +++ b/frontend/src/business/components/api/test/ApiTestConfig.vue @@ -123,6 +123,7 @@ let jmx = this.test.toJMX(); let blob = new Blob([jmx.xml], {type: "application/octet-stream"}); formData.append("files", new File([blob], jmx.name)); + console.log(jmx.xml) return { method: 'POST', diff --git a/frontend/src/business/components/api/test/model/JMX.js b/frontend/src/business/components/api/test/model/JMX.js index c85cad19e3..9888ff102c 100644 --- a/frontend/src/business/components/api/test/model/JMX.js +++ b/frontend/src/business/components/api/test/model/JMX.js @@ -369,3 +369,31 @@ export class Arguments extends DefaultTestElement { } } +export class BackendListener extends DefaultTestElement { + constructor(testName, className, args) { + super('BackendListener', 'BackendListenerGui', 'BackendListener', testName || 'Backend Listener'); + this.stringProp('classname', className); + this.add(new ElementArguments(args)); + } +} + +export class ElementArguments extends Element { + constructor(args) { + super('elementProp', { + name: "arguments", + elementType: "Arguments", + guiclass: "ArgumentsPanel", + testclass: "Arguments", + enabled: "true" + }); + + let collectionProp = this.collectionProp('Arguments.arguments'); + args.forEach(arg => { + let elementProp = collectionProp.elementProp(arg.name, 'Argument'); + elementProp.stringProp('Argument.name', arg.name); + elementProp.stringProp('Argument.value', arg.value); + elementProp.stringProp('Argument.metadata', "="); + }); + } +} + diff --git a/frontend/src/business/components/api/test/model/ScenarioModel.js b/frontend/src/business/components/api/test/model/ScenarioModel.js index 5a263ec775..fd1803e47a 100644 --- a/frontend/src/business/components/api/test/model/ScenarioModel.js +++ b/frontend/src/business/components/api/test/model/ScenarioModel.js @@ -9,7 +9,8 @@ import { HTTPSamplerArguments, ResponseCodeAssertion, ResponseDataAssertion, - ResponseHeadersAssertion, DefaultTestElement + ResponseHeadersAssertion, + BackendListener } from "./JMX"; export const generateId = function () { @@ -259,7 +260,7 @@ export class ResponseTime extends AssertionType { } } -/** ------------------------------------ **/ +/** ------------------------------------------------------------------------ **/ const JMX_ASSERTION_CONDITION = { MATCH: 1, CONTAINS: 1 << 1, @@ -301,16 +302,9 @@ class JMeterTestPlan extends Element { } } -class APIBackendListener extends DefaultTestElement { - constructor() { - super('BackendListener', 'BackendListenerGui', 'BackendListener', 'API Backend Listener'); - this.stringProp('classname', 'io.metersphere.api.jmeter.APIBackendListenerClient'); - } -} - class JMXGenerator { constructor(test) { - if (!test || !(test instanceof Test)) return; + if (!test || !test.id || !(test instanceof Test)) return; let testPlan = new TestPlan(test.name); test.scenarioDefinition.forEach(scenario => { @@ -332,7 +326,7 @@ class JMXGenerator { threadGroup.put(httpSamplerProxy); }) - threadGroup.put(new APIBackendListener()); + this.addBackendListener(threadGroup, test.id); testPlan.put(threadGroup); }) @@ -393,6 +387,13 @@ class JMXGenerator { } } + addBackendListener(threadGroup, testId) { + let testName = 'API Backend Listener'; + let className = 'io.metersphere.api.jmeter.APIBackendListenerClient'; + let args = [{name: 'id', value: testId}]; + threadGroup.put(new BackendListener(testName, className, args)); + } + filter(config) { return config.isValid(); }