This commit is contained in:
chenjianxing 2020-08-18 14:32:38 +08:00
commit ab0790a8a2
2 changed files with 19 additions and 7 deletions

View File

@ -14,6 +14,7 @@ import org.apache.jorphan.collections.HashTree;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.lang.reflect.Field; import java.lang.reflect.Field;
@ -24,14 +25,15 @@ public class JMeterService {
private JmeterProperties jmeterProperties; private JmeterProperties jmeterProperties;
public void run(String testId, String debugReportId, InputStream is) { 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"; String JMETER_PROPERTIES = JMETER_HOME + "/bin/jmeter.properties";
JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES); JMeterUtils.loadJMeterProperties(JMETER_PROPERTIES);
JMeterUtils.setJMeterHome(JMETER_HOME); JMeterUtils.setJMeterHome(JMETER_HOME);
try { try {
Object scriptWrapper = SaveService.loadElement(is); Object scriptWrapper = SaveService.loadElement(is);
HashTree testPlan = getHashTree(scriptWrapper); HashTree testPlan = getHashTree(scriptWrapper);
addBackendListener(testId, debugReportId, testPlan); addBackendListener(testId, debugReportId, testPlan);
LocalRunner runner = new LocalRunner(testPlan); LocalRunner runner = new LocalRunner(testPlan);
runner.run(); 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 { private HashTree getHashTree(Object scriptWrapper) throws Exception {
Field field = scriptWrapper.getClass().getDeclaredField("testPlan"); Field field = scriptWrapper.getClass().getDeclaredField("testPlan");
field.setAccessible(true); field.setAccessible(true);

View File

@ -5,7 +5,6 @@ import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.LoadTestMapper; import io.metersphere.base.mapper.LoadTestMapper;
import io.metersphere.base.mapper.TestResourceMapper; import io.metersphere.base.mapper.TestResourceMapper;
import io.metersphere.base.mapper.TestResourcePoolMapper; import io.metersphere.base.mapper.TestResourcePoolMapper;
import io.metersphere.commons.constants.PerformanceTestStatus;
import io.metersphere.commons.constants.ResourceStatusEnum; import io.metersphere.commons.constants.ResourceStatusEnum;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
@ -25,7 +24,6 @@ import org.springframework.web.client.RestTemplate;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -70,11 +68,9 @@ public class TestResourcePoolService {
} }
public void checkTestStatus(String testResourcePoolId) { public void checkTestStatus(String testResourcePoolId) {
List list = Arrays.asList(PerformanceTestStatus.Running, PerformanceTestStatus.Starting, PerformanceTestStatus.Error);
LoadTestExample example = new LoadTestExample(); LoadTestExample example = new LoadTestExample();
example.createCriteria() example.createCriteria()
.andTestResourcePoolIdEqualTo(testResourcePoolId) .andTestResourcePoolIdEqualTo(testResourcePoolId);
.andStatusIn(list);
if (loadTestMapper.countByExample(example) > 0) { if (loadTestMapper.countByExample(example) > 0) {
MSException.throwException(Translator.get("test_resource_pool_is_use")); MSException.throwException(Translator.get("test_resource_pool_is_use"));
} }