关于并发数

This commit is contained in:
Captain.B 2020-03-24 20:07:08 +08:00
parent db211cb76b
commit ac369fea63
3 changed files with 112 additions and 77 deletions

View File

@ -0,0 +1,81 @@
package io.metersphere.engine;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.base.domain.TestResource;
import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.commons.constants.ResourcePoolTypeEnum;
import io.metersphere.commons.constants.TestStatus;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.service.LoadTestService;
import io.metersphere.service.TestResourcePoolService;
import io.metersphere.service.TestResourceService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.List;
public abstract class AbstractEngine implements Engine {
protected FileMetadata jmxFile;
protected List<FileMetadata> csvFiles;
protected LoadTestWithBLOBs loadTest;
protected LoadTestService loadTestService;
protected Integer threadNum;
protected List<TestResource> resourceList;
@Override
public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) {
this.loadTest = loadTest;
this.jmxFile = fileMetadata;
this.csvFiles = csvFiles;
TestResourcePoolService testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class);
TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class);
String loadConfiguration = loadTest.getLoadConfiguration();
JSONArray jsonArray = JSON.parseArray(loadConfiguration);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
if (StringUtils.equals(o.getString("key"), "TargetLevel")) {
threadNum = o.getInteger("value");
}
}
String resourcePoolId = loadTest.getTestResourcePoolId();
if (StringUtils.isBlank(resourcePoolId)) {
MSException.throwException("Resource Pool ID is empty");
}
TestResourcePool resourcePool = testResourcePoolService.getResourcePool(resourcePoolId);
if (resourcePool == null) {
MSException.throwException("Resource Pool is empty");
}
if (!ResourcePoolTypeEnum.K8S.name().equals(resourcePool.getType())) {
MSException.throwException("Invalid Resource Pool type.");
}
this.resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId());
if (CollectionUtils.isEmpty(this.resourceList)) {
MSException.throwException("Test Resource is empty");
}
return true;
}
protected Integer getSumThreadNum() {
List<LoadTestWithBLOBs> loadTests = loadTestService.selectByTestResourcePoolId(loadTest.getTestResourcePoolId());
// 使用当前资源池正在运行的测试占用的并发数
return loadTests.stream().filter(t -> TestStatus.Running.name().equals(t.getStatus())).map(t -> {
Integer s = 0;
String loadConfiguration = t.getLoadConfiguration();
JSONArray jsonArray = JSON.parseArray(loadConfiguration);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
if (StringUtils.equals(o.getString("key"), "TargetLevel")) {
s = o.getInteger("value");
}
}
return s;
}).reduce(Integer::sum).orElse(0);
}
}

View File

@ -1,32 +1,50 @@
package io.metersphere.engine.docker; package io.metersphere.engine.docker;
import com.alibaba.fastjson.JSON;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.controller.request.TestRequest; import io.metersphere.controller.request.TestRequest;
import io.metersphere.engine.Engine; import io.metersphere.dto.NodeDTO;
import io.metersphere.engine.AbstractEngine;
import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineContext;
import io.metersphere.engine.EngineFactory;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
public class DockerTestEngine implements Engine { public class DockerTestEngine extends AbstractEngine {
private EngineContext context;
private RestTemplate restTemplate;
RestTemplate restTemplate;
@Override @Override
public boolean init(EngineContext context) { public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) {
super.init(loadTest, fileMetadata, csvFiles);
this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class); this.restTemplate = CommonBeanFactory.getBean(RestTemplate.class);
// todo 初始化操作 // todo 初始化操作
this.context = context;
return true; return true;
} }
@Override @Override
public void start() { public void start() {
Integer runningSumThreadNum = getSumThreadNum();
Integer integer = resourceList.stream().map(r -> {
NodeDTO nodeDTO = JSON.parseObject(r.getConfiguration(), NodeDTO.class);
return nodeDTO.getMaxConcurrency();
}).reduce(Integer::sum).orElse(0);
// todo 运行测试 // todo 运行测试
// RestTemplate restTemplate = new RestTemplate(); EngineContext context = null;
try {
context = EngineFactory.createContext(loadTest, jmxFile, csvFiles);
} catch (Exception e) {
e.printStackTrace();
}
String testId = context.getTestId(); String testId = context.getTestId();
String content = context.getContent(); String content = context.getContent();
@ -42,7 +60,7 @@ public class DockerTestEngine implements Engine {
List containerList = restTemplate.getForObject(taskStatusUri, List.class); List containerList = restTemplate.getForObject(taskStatusUri, List.class);
for (int i = 0; i < containerList.size(); i++) { for (int i = 0; i < containerList.size(); i++) {
HashMap h = (HashMap) containerList.get(i); HashMap h = (HashMap) containerList.get(i);
if (StringUtils.equals((String)h.get("State"), "running")) { if (StringUtils.equals((String) h.get("State"), "running")) {
MSException.throwException("the test is running!"); MSException.throwException("the test is running!");
} }
} }
@ -55,7 +73,7 @@ public class DockerTestEngine implements Engine {
// TODO 停止运行测试 // TODO 停止运行测试
// RestTemplate restTemplate = new RestTemplate(); // RestTemplate restTemplate = new RestTemplate();
String testId = context.getTestId(); String testId = loadTest.getId();
String uri = "http://localhost:8082/jmeter/container/stop/" + testId; String uri = "http://localhost:8082/jmeter/container/stop/" + testId;
restTemplate.postForObject(uri, "", String.class); restTemplate.postForObject(uri, "", String.class);

View File

@ -1,102 +1,38 @@
package io.metersphere.engine.kubernetes; package io.metersphere.engine.kubernetes;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.api.model.ConfigMap;
import io.fabric8.kubernetes.api.model.ObjectMeta; import io.fabric8.kubernetes.api.model.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClient;
import io.metersphere.base.domain.FileMetadata; import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs; import io.metersphere.base.domain.LoadTestWithBLOBs;
import io.metersphere.base.domain.TestResource;
import io.metersphere.base.domain.TestResourcePool;
import io.metersphere.commons.constants.ResourcePoolTypeEnum;
import io.metersphere.commons.constants.TestStatus;
import io.metersphere.commons.exception.MSException; import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.LogUtil;
import io.metersphere.engine.Engine; import io.metersphere.engine.AbstractEngine;
import io.metersphere.engine.EngineContext; import io.metersphere.engine.EngineContext;
import io.metersphere.engine.EngineFactory; import io.metersphere.engine.EngineFactory;
import io.metersphere.engine.kubernetes.crds.jmeter.Jmeter; import io.metersphere.engine.kubernetes.crds.jmeter.Jmeter;
import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec; import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec;
import io.metersphere.engine.kubernetes.provider.ClientCredential; import io.metersphere.engine.kubernetes.provider.ClientCredential;
import io.metersphere.engine.kubernetes.provider.KubernetesProvider; import io.metersphere.engine.kubernetes.provider.KubernetesProvider;
import io.metersphere.service.LoadTestService;
import io.metersphere.service.TestResourcePoolService;
import io.metersphere.service.TestResourceService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils; import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
public class KubernetesTestEngine implements Engine { public class KubernetesTestEngine extends AbstractEngine {
private FileMetadata jmxFile;
private List<FileMetadata> csvFiles;
private LoadTestWithBLOBs loadTest;
private LoadTestService loadTestService;
private Integer threadNum;
private String resourcePoolId;
private List<TestResource> resourceList;
@Override @Override
public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) { public boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) {
this.loadTest = loadTest; super.init(loadTest, fileMetadata, csvFiles);
this.jmxFile = fileMetadata;
this.csvFiles = csvFiles;
TestResourcePoolService testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class);
TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class);
String loadConfiguration = loadTest.getLoadConfiguration();
JSONArray jsonArray = JSON.parseArray(loadConfiguration);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
if (StringUtils.equals("resourcePoolId", o.getString("key"))) {
resourcePoolId = o.getString("value");
}
if (StringUtils.equals(o.getString("key"), "TargetLevel")) {
threadNum = o.getInteger("value");
}
}
if (StringUtils.isBlank(resourcePoolId)) {
MSException.throwException("Resource Pool ID is empty");
}
TestResourcePool resourcePool = testResourcePoolService.getResourcePool(resourcePoolId);
if (resourcePool == null) {
MSException.throwException("Resource Pool is empty");
}
if (!ResourcePoolTypeEnum.K8S.name().equals(resourcePool.getType())) {
MSException.throwException("Invalid Resource Pool type.");
}
this.resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId());
if (CollectionUtils.isEmpty(this.resourceList)) {
MSException.throwException("Test Resource is empty");
}
return true; return true;
} }
@Override @Override
public void start() { public void start() {
Integer sumThreadNum = getSumThreadNum();
// resourceList size 1 // resourceList size 1
List<LoadTestWithBLOBs> loadTests = loadTestService.selectByTestResourcePoolId(resourcePoolId);
// 使用当前资源池正在运行的测试占用的并发数
Integer sumThreadNum = loadTests.stream().filter(t -> TestStatus.Running.name().equals(t.getStatus())).map(t -> {
Integer s = 0;
String loadConfiguration = t.getLoadConfiguration();
JSONArray jsonArray = JSON.parseArray(loadConfiguration);
for (int i = 0; i < jsonArray.size(); i++) {
JSONObject o = jsonArray.getJSONObject(i);
if (StringUtils.equals(o.getString("key"), "TargetLevel")) {
s = o.getInteger("value");
}
}
return s;
}).reduce(Integer::sum).orElse(0);
resourceList.forEach(r -> { resourceList.forEach(r -> {
String configuration = r.getConfiguration(); String configuration = r.getConfiguration();
ClientCredential clientCredential = JSON.parseObject(configuration, ClientCredential.class); ClientCredential clientCredential = JSON.parseObject(configuration, ClientCredential.class);