This commit is contained in:
fit2-zhao 2020-10-13 16:11:03 +08:00
commit f6af7c9d08
4 changed files with 38 additions and 7 deletions

View File

@ -497,6 +497,15 @@
<outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory> <outputDirectory>src/main/resources/jmeter/lib/ext</outputDirectory>
<destFileName>ApacheJMeter_functions.jar</destFileName> <destFileName>ApacheJMeter_functions.jar</destFileName>
</artifactItem> </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> </artifactItems>
<outputDirectory>${project.build.directory}/wars</outputDirectory> <outputDirectory>${project.build.directory}/wars</outputDirectory>
<overWriteReleases>false</overWriteReleases> <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.util.JMeterUtils;
import org.apache.jmeter.visualizers.backend.BackendListener; import org.apache.jmeter.visualizers.backend.BackendListener;
import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.HashTree;
import org.python.core.Options;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -34,10 +34,6 @@ public class JMeterService {
JMeterUtils.setJMeterHome(JMETER_HOME); JMeterUtils.setJMeterHome(JMETER_HOME);
JMeterUtils.setLocale(LocaleContextHolder.getLocale()); JMeterUtils.setLocale(LocaleContextHolder.getLocale());
//解决无法加载 PyScriptEngineFactory
Options.importSite = false;
try { try {
Object scriptWrapper = SaveService.loadElement(is); Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper); HashTree testPlan = getHashTree(scriptWrapper);
@ -51,7 +47,7 @@ public class JMeterService {
} }
} }
private String getJmeterHome() { public String getJmeterHome() {
String home = getClass().getResource("/").getPath() + "jmeter"; String home = getClass().getResource("/").getPath() + "jmeter";
try { try {
File file = new File(home); File file = new File(home);

View File

@ -1,6 +1,10 @@
package io.metersphere.listener; package io.metersphere.listener;
import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.service.ScheduleService; 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.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
@ -12,12 +16,16 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
@Resource @Resource
private ScheduleService scheduleService; private ScheduleService scheduleService;
@Resource
private JMeterService jMeterService;
@Override @Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) { public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
System.out.println("================= 应用启动 ================="); System.out.println("================= 应用启动 =================");
initPythonEnv();
try { try {
Thread.sleep(3 * 60 * 1000); Thread.sleep(3 * 60 * 1000);
} catch (InterruptedException e) { } catch (InterruptedException e) {
@ -25,6 +33,24 @@ public class AppStartListener implements ApplicationListener<ApplicationReadyEve
} }
scheduleService.startEnableSchedules(); 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);
}
} }
} }

View File

@ -61,7 +61,7 @@ export default {
window.console.error(error.response || error.message); window.console.error(error.response || error.message);
if (error.response && error.response.data) { if (error.response && error.response.data) {
if (error.response.headers["authentication-status"] !== "invalid") { if (error.response.headers["authentication-status"] !== "invalid") {
Message.error({message: error.response.data.message, showClose: true}); Message.error({message: error.response.data.message || error.response.data, showClose: true});
} }
} else { } else {
Message.error({message: error.message, showClose: true}); Message.error({message: error.message, showClose: true});