refactor: 代码调整,不强制电脑上有 /opt/jmeter/ 目录

This commit is contained in:
Captain.B 2020-08-18 11:39:00 +08:00
parent 240ac7ab26
commit be18d0b9ab
1 changed files with 18 additions and 2 deletions

View File

@ -14,6 +14,7 @@ import org.apache.jorphan.collections.HashTree;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Field;
@ -24,14 +25,15 @@ public class JMeterService {
private JmeterProperties jmeterProperties;
public void run(String testId, String debugReportId, InputStream is) {
String JMETER_HOME = jmeterProperties.getHome();
String JMETER_HOME = getJmeterHome();
String JMETER_PROPERTIES = JMETER_HOME + "/bin/jmeter.properties";
JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES);
JMeterUtils.setJMeterHome(JMETER_HOME);
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper);
addBackendListener(testId, debugReportId, testPlan);
addBackendListener(testId, debugReportId, testPlan);
LocalRunner runner = new LocalRunner(testPlan);
runner.run();
@ -41,6 +43,20 @@ public class JMeterService {
}
}
private String getJmeterHome() {
String home = getClass().getResource("/").getPath() + "/jmeter";
try {
File file = new File(home);
if (file.exists()) {
return home;
} else {
return jmeterProperties.getHome();
}
} catch (Exception e) {
return jmeterProperties.getHome();
}
}
private HashTree getHashTree(Object scriptWrapper) throws Exception {
Field field = scriptWrapper.getClass().getDeclaredField("testPlan");
field.setAccessible(true);