启动测试
This commit is contained in:
parent
4cc73c172c
commit
d6f285a1d4
|
@ -2,9 +2,10 @@ package io.metersphere.controller.request;
|
|||
|
||||
public class TestRequest {
|
||||
|
||||
int size;
|
||||
String fileString;
|
||||
String testId;
|
||||
private int size;
|
||||
private String fileString;
|
||||
private String testId;
|
||||
private String image;
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
|
@ -29,4 +30,12 @@ public class TestRequest {
|
|||
public void setTestId(String testId) {
|
||||
this.testId = testId;
|
||||
}
|
||||
|
||||
public String getImage() {
|
||||
return image;
|
||||
}
|
||||
|
||||
public void setImage(String image) {
|
||||
this.image = image;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,6 +19,8 @@ import org.apache.commons.lang3.StringUtils;
|
|||
import java.util.List;
|
||||
|
||||
public abstract class AbstractEngine implements Engine {
|
||||
public static final String JMETER_IMAGE = "jmeter-master:0.0.2";
|
||||
|
||||
protected LoadTestWithBLOBs loadTest;
|
||||
protected LoadTestService loadTestService;
|
||||
protected Integer threadNum;
|
||||
|
@ -32,8 +34,7 @@ public abstract class AbstractEngine implements Engine {
|
|||
testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(LoadTestWithBLOBs loadTest) {
|
||||
protected void init(LoadTestWithBLOBs loadTest) {
|
||||
if (loadTest == null) {
|
||||
MSException.throwException("LoadTest is null.");
|
||||
}
|
||||
|
@ -57,7 +58,6 @@ public abstract class AbstractEngine implements Engine {
|
|||
if (CollectionUtils.isEmpty(this.resourceList)) {
|
||||
MSException.throwException("Test Resource is empty");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
protected Integer getRunningThreadNum() {
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
package io.metersphere.engine;
|
||||
|
||||
import io.metersphere.base.domain.LoadTestWithBLOBs;
|
||||
|
||||
public interface Engine {
|
||||
boolean init(LoadTestWithBLOBs loadTest);
|
||||
|
||||
void start();
|
||||
|
||||
|
|
|
@ -47,9 +47,9 @@ public class EngineFactory {
|
|||
|
||||
switch (type) {
|
||||
case NODE:
|
||||
return new DockerTestEngine();
|
||||
return new DockerTestEngine(loadTest);
|
||||
case K8S:
|
||||
return new KubernetesTestEngine();
|
||||
return new KubernetesTestEngine(loadTest);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
|
@ -2,6 +2,7 @@ package io.metersphere.engine.docker;
|
|||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import io.metersphere.base.domain.LoadTestWithBLOBs;
|
||||
import io.metersphere.base.domain.TestResource;
|
||||
import io.metersphere.commons.constants.ResourceStatusEnum;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
|
@ -10,6 +11,7 @@ import io.metersphere.dto.NodeDTO;
|
|||
import io.metersphere.engine.AbstractEngine;
|
||||
import io.metersphere.engine.EngineContext;
|
||||
import io.metersphere.engine.EngineFactory;
|
||||
import io.metersphere.engine.kubernetes.registry.RegistryService;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
|
@ -18,22 +20,27 @@ import java.util.List;
|
|||
import java.util.stream.Collectors;
|
||||
|
||||
public class DockerTestEngine extends AbstractEngine {
|
||||
private static final String BASE_URL = "http://%s:%d";
|
||||
|
||||
private RestTemplate restTemplate;
|
||||
private RegistryService registryService;
|
||||
|
||||
public DockerTestEngine(LoadTestWithBLOBs loadTest) {
|
||||
this.init(loadTest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(LoadTestWithBLOBs loadTest) {
|
||||
protected void init(LoadTestWithBLOBs loadTest) {
|
||||
super.init(loadTest);
|
||||
this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class);
|
||||
this.registryService = CommonBeanFactory.getBean(RegistryService.class);
|
||||
// todo 初始化操作
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void start() {
|
||||
Integer runningSumThreadNum = getRunningThreadNum();
|
||||
Integer totalThreadNum = resourceList.stream()
|
||||
int totalThreadNum = resourceList.stream()
|
||||
.filter(r -> ResourceStatusEnum.VALID.name().equals(r.getStatus()))
|
||||
.map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency())
|
||||
.reduce(Integer::sum)
|
||||
|
@ -46,34 +53,40 @@ public class DockerTestEngine extends AbstractEngine {
|
|||
.map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency())
|
||||
.collect(Collectors.toList());
|
||||
|
||||
resourceRatio.forEach(ratio -> {
|
||||
for (int i = 0, size = resourceList.size(); i < size; i++) {
|
||||
int ratio = resourceRatio.get(i);
|
||||
double realThreadNum = ((double) ratio / totalThreadNum) * threadNum;
|
||||
runTest(Math.round(realThreadNum));
|
||||
});
|
||||
runTest(resourceList.get(i), Math.round(realThreadNum));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void runTest(long realThreadNum) {
|
||||
private void runTest(TestResource resource, long realThreadNum) {
|
||||
// todo 运行测试
|
||||
EngineContext context = null;
|
||||
try {
|
||||
context = EngineFactory.createContext(loadTest, realThreadNum);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
MSException.throwException(e);
|
||||
}
|
||||
|
||||
String configuration = resource.getConfiguration();
|
||||
NodeDTO node = JSON.parseObject(configuration, NodeDTO.class);
|
||||
String nodeIp = node.getIp();
|
||||
Integer port = node.getPort();
|
||||
String testId = context.getTestId();
|
||||
String content = context.getContent();
|
||||
|
||||
String uri = "http://localhost:8082/jmeter/container/start";
|
||||
String uri = String.format(BASE_URL + "/jmeter/container/start", nodeIp, port);
|
||||
|
||||
TestRequest testRequest = new TestRequest();
|
||||
testRequest.setSize(1);
|
||||
testRequest.setTestId(testId);
|
||||
testRequest.setFileString(content);
|
||||
testRequest.setImage(registryService.getRegistry() + JMETER_IMAGE);
|
||||
|
||||
// todo 判断测试状态
|
||||
String taskStatusUri = "http://localhost:8082/jmeter/task/status/" + testId;
|
||||
String taskStatusUri = String.format(BASE_URL + "/jmeter/task/status/" + testId, nodeIp, port);
|
||||
List containerList = restTemplate.getForObject(taskStatusUri, List.class);
|
||||
for (int i = 0; i < containerList.size(); i++) {
|
||||
HashMap h = (HashMap) containerList.get(i);
|
||||
|
@ -88,12 +101,16 @@ public class DockerTestEngine extends AbstractEngine {
|
|||
@Override
|
||||
public void stop() {
|
||||
// TODO 停止运行测试
|
||||
// RestTemplate restTemplate = new RestTemplate();
|
||||
|
||||
String testId = loadTest.getId();
|
||||
this.resourceList.forEach(r -> {
|
||||
NodeDTO node = JSON.parseObject(r.getConfiguration(), NodeDTO.class);
|
||||
String ip = node.getIp();
|
||||
Integer port = node.getPort();
|
||||
|
||||
String uri = String.format(BASE_URL + "/jmeter/container/stop/" + testId, ip, port);
|
||||
restTemplate.postForObject(uri, "", String.class);
|
||||
});
|
||||
|
||||
String uri = "http://localhost:8082/jmeter/container/stop/" + testId;
|
||||
restTemplate.postForObject(uri, "", String.class);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,11 +24,14 @@ public class KubernetesTestEngine extends AbstractEngine {
|
|||
|
||||
private RegistryService registryService;
|
||||
|
||||
public KubernetesTestEngine(LoadTestWithBLOBs loadTest) {
|
||||
this.init(loadTest);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean init(LoadTestWithBLOBs loadTest) {
|
||||
public void init(LoadTestWithBLOBs loadTest) {
|
||||
super.init(loadTest);
|
||||
this.registryService = CommonBeanFactory.getBean(RegistryService.class);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
@ -89,7 +92,7 @@ public class KubernetesTestEngine extends AbstractEngine {
|
|||
}});
|
||||
jmeter.setSpec(new JmeterSpec() {{
|
||||
setReplicas(1);
|
||||
setImage(registryService.getRegistry() + "jmeter-master:0.0.2");
|
||||
setImage(registryService.getRegistry() + JMETER_IMAGE);
|
||||
}});
|
||||
LogUtil.info("Load test started. " + context.getTestId());
|
||||
kubernetesProvider.applyCustomResource(jmeter);
|
||||
|
@ -100,6 +103,28 @@ public class KubernetesTestEngine extends AbstractEngine {
|
|||
|
||||
@Override
|
||||
public void stop() {
|
||||
resourceList.forEach(r -> {
|
||||
try {
|
||||
EngineContext context = EngineFactory.createContext(loadTest, threadNum);
|
||||
String configuration = r.getConfiguration();
|
||||
ClientCredential clientCredential = JSON.parseObject(configuration, ClientCredential.class);
|
||||
KubernetesProvider provider = new KubernetesProvider(JSON.toJSONString(clientCredential));
|
||||
provider.confirmNamespace(context.getNamespace());
|
||||
Jmeter jmeter = new Jmeter();
|
||||
jmeter.setMetadata(new ObjectMeta() {{
|
||||
setName(context.getTestId());
|
||||
setNamespace(context.getNamespace());
|
||||
}});
|
||||
jmeter.setSpec(new JmeterSpec() {{
|
||||
setReplicas(1);
|
||||
setImage(registryService.getRegistry() + JMETER_IMAGE);
|
||||
}});
|
||||
provider.deleteCustomResource(jmeter);
|
||||
} catch (Exception e) {
|
||||
MSException.throwException(e);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -174,16 +174,7 @@ public class LoadTestService {
|
|||
MSException.throwException(String.format("Test cannot be run,test ID:%s", request.getId()));
|
||||
}
|
||||
|
||||
boolean init = true;
|
||||
try {
|
||||
init = engine.init(loadTest);
|
||||
} catch (Exception e) {
|
||||
MSException.throwException(e);
|
||||
}
|
||||
if (!init) {
|
||||
MSException.throwException(Translator.get("run_load_test_file_init_error") + request.getId());
|
||||
}
|
||||
|
||||
// 启动测试
|
||||
engine.start();
|
||||
// 标记running状态
|
||||
loadTest.setStatus(TestStatus.Running.name());
|
||||
|
|
Loading…
Reference in New Issue