This commit is contained in:
q4speed 2020-05-06 14:21:10 +08:00
parent 69586479ba
commit 2bed94efce
5 changed files with 82 additions and 22 deletions

View File

@ -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<SampleResult> 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);
}
}

View File

@ -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<MultipartFile> files) {
save(request, files);
public String run(SaveAPITestRequest request, List<MultipartFile> 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<String> 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<ApiTestFile> ApiTestFiles = apiTestFileMapper.selectByExample(ApiTestFileExample);
apiTestFileMapper.selectByExample(ApiTestFileExample);
if (!CollectionUtils.isEmpty(ApiTestFiles)) {
return ApiTestFiles.get(0);
} else {
return null;
}
}
}

View File

@ -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',

View File

@ -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', "=");
});
}
}

View File

@ -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();
}