fix(接口测试): 无法导入内置python包

This commit is contained in:
chenjianxing 2020-10-13 14:08:16 +08:00
parent d4c762bfe4
commit 971bc350bd
3 changed files with 37 additions and 6 deletions

View File

@ -490,6 +490,15 @@
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>ApacheJMeter_functions.jar</destFileName>
</artifactItem>
<artifactItem>
<groupId>org.python</groupId>
<artifactId>jython-standalone</artifactId>
<version>2.7.0</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>jython-standalone.jar</destFileName>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases>

View File

@ -11,7 +11,7 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.util.JMeterUtils;
import org.apache.jmeter.visualizers.backend.BackendListener;
import org.apache.jorphan.collections.HashTree;
import org.python.core.Options;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;
@ -34,10 +34,6 @@ public class JMeterService {
JMeterUtils.setJMeterHome(JMETER_HOME);
JMeterUtils.setLocale(LocaleContextHolder.getLocale());
//解决无法加载 PyScriptEngineFactory
Options.importSite = false;
try {
Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper);
@ -51,7 +47,7 @@ public class JMeterService {
}
}
private String getJmeterHome() {
public String getJmeterHome() {
String home = getClass().getResource("/").getPath() + "jmeter";
try {
File file = new File(home);

View File

@ -1,6 +1,10 @@
package io.metersphere.listener;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.service.ScheduleService;
import org.python.core.Options;
import org.python.util.PythonInterpreter;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
@ -12,12 +16,16 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
@Resource
private ScheduleService scheduleService;
@Resource
private JMeterService jMeterService;
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
System.out.println("================= 应用启动 =================");
initPythonEnv();
try {
Thread.sleep(3 * 60 * 1000);
} catch (InterruptedException e) {
@ -25,6 +33,24 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
}
scheduleService.startEnableSchedules();
}
/**
* 解决接口测试-无法导入内置python包
*/
private void initPythonEnv() {
//解决无法加载 PyScriptEngineFactory
Options.importSite = false;
try {
PythonInterpreter interp = new PythonInterpreter();
String path = jMeterService.getJmeterHome();
System.out.println("sys.path: " + path);
path += "/lib/ext/jython-standalone.jar/Lib";
interp.exec("import sys");
interp.exec("sys.path.append(\"" + path + "\")");
} catch (Exception e) {
e.printStackTrace();
LogUtil.error(e.getMessage(), e);
}
}
}