按比率启动容器

This commit is contained in:
Captain.B 2020-03-24 22:18:05 +08:00
parent ac369fea63
commit c4cfae5bae
6 changed files with 63 additions and 44 deletions

View File

@ -36,14 +36,7 @@ public abstract class AbstractEngine implements Engine {
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");
}
}
threadNum = getThreadNum(loadTest);
String resourcePoolId = loadTest.getTestResourcePoolId();
if (StringUtils.isBlank(resourcePoolId)) {
MSException.throwException("Resource Pool ID is empty");
@ -62,20 +55,26 @@ public abstract class AbstractEngine implements Engine {
return true;
}
protected Integer getSumThreadNum() {
protected Integer getRunningThreadNum() {
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 loadTests.stream()
.filter(t -> TestStatus.Running.name().equals(t.getStatus()))
.map(this::getThreadNum)
.reduce(Integer::sum)
.orElse(0);
}
private Integer getThreadNum(LoadTestWithBLOBs 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);
}
return s;
}
}

View File

@ -10,6 +10,7 @@ public class EngineContext {
private String fileType;
private String content;
private String resourcePoolId;
private Long threadNum;
private Map<String, Object> properties = new HashMap<>();
private Map<String, String> testData = new HashMap<>();
@ -76,4 +77,12 @@ public class EngineContext {
public void setResourcePoolId(String resourcePoolId) {
this.resourcePoolId = resourcePoolId;
}
public Long getThreadNum() {
return threadNum;
}
public void setThreadNum(Long threadNum) {
this.threadNum = threadNum;
}
}

View File

@ -27,22 +27,11 @@ import java.util.Map;
@Service
public class EngineFactory {
private static final String RESOURCE_POOL_ID = "resourcePoolId";
private static FileService fileService;
private static TestResourcePoolService testResourcePoolService;
public static Engine createEngine(LoadTestWithBLOBs loadTest) {
String resourcePoolId = null;
if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) {
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());
for (int i = 0; i < jsonArray.size(); i++) {
final JSONObject jsonObject = jsonArray.getJSONObject(i);
if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) {
resourcePoolId = jsonObject.getString("value");
break;
}
}
}
String resourcePoolId = loadTest.getTestResourcePoolId();
if (StringUtils.isBlank(resourcePoolId)) {
MSException.throwException("Resource Pool ID is empty.");
}
@ -63,7 +52,7 @@ public class EngineFactory {
return null;
}
public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles) throws Exception {
public static EngineContext createContext(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles, long threadNum) throws Exception {
final FileContent fileContent = fileService.getFileContent(fileMetadata.getId());
if (fileContent == null) {
MSException.throwException(Translator.get("run_load_test_file_content_not_found") + loadTest.getId());
@ -73,6 +62,8 @@ public class EngineFactory {
engineContext.setTestName(loadTest.getName());
engineContext.setNamespace(loadTest.getProjectId());
engineContext.setFileType(fileMetadata.getType());
engineContext.setThreadNum(threadNum);
engineContext.setResourcePoolId(loadTest.getTestResourcePoolId());
if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) {
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());
@ -80,9 +71,6 @@ public class EngineFactory {
for (int i = 0; i < jsonArray.size(); i++) {
final JSONObject jsonObject = jsonArray.getJSONObject(i);
engineContext.addProperty(jsonObject.getString("key"), jsonObject.get("value"));
if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) {
engineContext.setResourcePoolId(jsonObject.getString("value"));
}
}
}

View File

@ -3,6 +3,7 @@ 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.constants.ResourceStatusEnum;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.controller.request.TestRequest;
@ -15,6 +16,7 @@ import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;
public class DockerTestEngine extends AbstractEngine {
@ -31,16 +33,32 @@ public class DockerTestEngine extends AbstractEngine {
@Override
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);
Integer runningSumThreadNum = getRunningThreadNum();
Integer totalThreadNum = resourceList.stream()
.filter(r -> ResourceStatusEnum.VALID.name().equals(r.getStatus()))
.map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency())
.reduce(Integer::sum)
.orElse(0);
if (threadNum > totalThreadNum - runningSumThreadNum) {
MSException.throwException("资源不足");
}
List<Integer> resourceRatio = resourceList.stream()
.filter(r -> ResourceStatusEnum.VALID.name().equals(r.getStatus()))
.map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency())
.collect(Collectors.toList());
resourceRatio.forEach(ratio -> {
double realThreadNum = ((double) ratio / totalThreadNum) * threadNum;
runTest(Math.round(realThreadNum));
});
}
private void runTest(long realThreadNum) {
// todo 运行测试
EngineContext context = null;
try {
context = EngineFactory.createContext(loadTest, jmxFile, csvFiles);
context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, realThreadNum);
} catch (Exception e) {
e.printStackTrace();
}

View File

@ -31,7 +31,7 @@ public class KubernetesTestEngine extends AbstractEngine {
@Override
public void start() {
Integer sumThreadNum = getSumThreadNum();
Integer sumThreadNum = getRunningThreadNum();
// resourceList size 1
resourceList.forEach(r -> {
String configuration = r.getConfiguration();
@ -43,7 +43,7 @@ public class KubernetesTestEngine extends AbstractEngine {
MSException.throwException("资源不足");
}
try {
EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles);
EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, threadNum);
runTest(context, clientCredential, 1);
} catch (Exception e) {
LogUtil.error(e);

View File

@ -386,6 +386,11 @@ public class JmeterDocumentParser implements DocumentParser {
if (nodeNameEquals(ele, STRING_PROP)) {
parseStringProp(ele);
}
// 设置具体的线程数
if (nodeNameEquals(ele, STRING_PROP) && "TargetLevel".equals(ele.getAttribute("name"))) {
ele.getFirstChild().setNodeValue(context.getThreadNum().toString());
}
}
}
}