按比率启动容器
This commit is contained in:
parent
ac369fea63
commit
c4cfae5bae
|
@ -36,14 +36,7 @@ public abstract class AbstractEngine implements Engine {
|
||||||
TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
|
TestResourceService testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
|
||||||
this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class);
|
this.loadTestService = CommonBeanFactory.getBean(LoadTestService.class);
|
||||||
|
|
||||||
String loadConfiguration = loadTest.getLoadConfiguration();
|
threadNum = getThreadNum(loadTest);
|
||||||
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();
|
String resourcePoolId = loadTest.getTestResourcePoolId();
|
||||||
if (StringUtils.isBlank(resourcePoolId)) {
|
if (StringUtils.isBlank(resourcePoolId)) {
|
||||||
MSException.throwException("Resource Pool ID is empty");
|
MSException.throwException("Resource Pool ID is empty");
|
||||||
|
@ -62,20 +55,26 @@ public abstract class AbstractEngine implements Engine {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Integer getSumThreadNum() {
|
protected Integer getRunningThreadNum() {
|
||||||
List<LoadTestWithBLOBs> loadTests = loadTestService.selectByTestResourcePoolId(loadTest.getTestResourcePoolId());
|
List<LoadTestWithBLOBs> loadTests = loadTestService.selectByTestResourcePoolId(loadTest.getTestResourcePoolId());
|
||||||
// 使用当前资源池正在运行的测试占用的并发数
|
// 使用当前资源池正在运行的测试占用的并发数
|
||||||
return loadTests.stream().filter(t -> TestStatus.Running.name().equals(t.getStatus())).map(t -> {
|
return loadTests.stream()
|
||||||
Integer s = 0;
|
.filter(t -> TestStatus.Running.name().equals(t.getStatus()))
|
||||||
String loadConfiguration = t.getLoadConfiguration();
|
.map(this::getThreadNum)
|
||||||
JSONArray jsonArray = JSON.parseArray(loadConfiguration);
|
.reduce(Integer::sum)
|
||||||
for (int i = 0; i < jsonArray.size(); i++) {
|
.orElse(0);
|
||||||
JSONObject o = jsonArray.getJSONObject(i);
|
}
|
||||||
if (StringUtils.equals(o.getString("key"), "TargetLevel")) {
|
|
||||||
s = o.getInteger("value");
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ public class EngineContext {
|
||||||
private String fileType;
|
private String fileType;
|
||||||
private String content;
|
private String content;
|
||||||
private String resourcePoolId;
|
private String resourcePoolId;
|
||||||
|
private Long threadNum;
|
||||||
private Map<String, Object> properties = new HashMap<>();
|
private Map<String, Object> properties = new HashMap<>();
|
||||||
private Map<String, String> testData = new HashMap<>();
|
private Map<String, String> testData = new HashMap<>();
|
||||||
|
|
||||||
|
@ -76,4 +77,12 @@ public class EngineContext {
|
||||||
public void setResourcePoolId(String resourcePoolId) {
|
public void setResourcePoolId(String resourcePoolId) {
|
||||||
this.resourcePoolId = resourcePoolId;
|
this.resourcePoolId = resourcePoolId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Long getThreadNum() {
|
||||||
|
return threadNum;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setThreadNum(Long threadNum) {
|
||||||
|
this.threadNum = threadNum;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,22 +27,11 @@ import java.util.Map;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class EngineFactory {
|
public class EngineFactory {
|
||||||
private static final String RESOURCE_POOL_ID = "resourcePoolId";
|
|
||||||
private static FileService fileService;
|
private static FileService fileService;
|
||||||
private static TestResourcePoolService testResourcePoolService;
|
private static TestResourcePoolService testResourcePoolService;
|
||||||
|
|
||||||
public static Engine createEngine(LoadTestWithBLOBs loadTest) {
|
public static Engine createEngine(LoadTestWithBLOBs loadTest) {
|
||||||
String resourcePoolId = null;
|
String resourcePoolId = loadTest.getTestResourcePoolId();
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (StringUtils.isBlank(resourcePoolId)) {
|
if (StringUtils.isBlank(resourcePoolId)) {
|
||||||
MSException.throwException("Resource Pool ID is empty.");
|
MSException.throwException("Resource Pool ID is empty.");
|
||||||
}
|
}
|
||||||
|
@ -63,7 +52,7 @@ public class EngineFactory {
|
||||||
return null;
|
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());
|
final FileContent fileContent = fileService.getFileContent(fileMetadata.getId());
|
||||||
if (fileContent == null) {
|
if (fileContent == null) {
|
||||||
MSException.throwException(Translator.get("run_load_test_file_content_not_found") + loadTest.getId());
|
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.setTestName(loadTest.getName());
|
||||||
engineContext.setNamespace(loadTest.getProjectId());
|
engineContext.setNamespace(loadTest.getProjectId());
|
||||||
engineContext.setFileType(fileMetadata.getType());
|
engineContext.setFileType(fileMetadata.getType());
|
||||||
|
engineContext.setThreadNum(threadNum);
|
||||||
|
engineContext.setResourcePoolId(loadTest.getTestResourcePoolId());
|
||||||
|
|
||||||
if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) {
|
if (!StringUtils.isEmpty(loadTest.getLoadConfiguration())) {
|
||||||
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());
|
final JSONArray jsonArray = JSONObject.parseArray(loadTest.getLoadConfiguration());
|
||||||
|
@ -80,9 +71,6 @@ public class EngineFactory {
|
||||||
for (int i = 0; i < jsonArray.size(); i++) {
|
for (int i = 0; i < jsonArray.size(); i++) {
|
||||||
final JSONObject jsonObject = jsonArray.getJSONObject(i);
|
final JSONObject jsonObject = jsonArray.getJSONObject(i);
|
||||||
engineContext.addProperty(jsonObject.getString("key"), jsonObject.get("value"));
|
engineContext.addProperty(jsonObject.getString("key"), jsonObject.get("value"));
|
||||||
if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) {
|
|
||||||
engineContext.setResourcePoolId(jsonObject.getString("value"));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -3,6 +3,7 @@ package io.metersphere.engine.docker;
|
||||||
import com.alibaba.fastjson.JSON;
|
import com.alibaba.fastjson.JSON;
|
||||||
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.commons.constants.ResourceStatusEnum;
|
||||||
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;
|
||||||
|
@ -15,6 +16,7 @@ import org.springframework.web.client.RestTemplate;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
public class DockerTestEngine extends AbstractEngine {
|
public class DockerTestEngine extends AbstractEngine {
|
||||||
|
|
||||||
|
@ -31,16 +33,32 @@ public class DockerTestEngine extends AbstractEngine {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
Integer runningSumThreadNum = getSumThreadNum();
|
Integer runningSumThreadNum = getRunningThreadNum();
|
||||||
Integer integer = resourceList.stream().map(r -> {
|
Integer totalThreadNum = resourceList.stream()
|
||||||
NodeDTO nodeDTO = JSON.parseObject(r.getConfiguration(), NodeDTO.class);
|
.filter(r -> ResourceStatusEnum.VALID.name().equals(r.getStatus()))
|
||||||
return nodeDTO.getMaxConcurrency();
|
.map(r -> JSON.parseObject(r.getConfiguration(), NodeDTO.class).getMaxConcurrency())
|
||||||
}).reduce(Integer::sum).orElse(0);
|
.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 运行测试
|
// todo 运行测试
|
||||||
EngineContext context = null;
|
EngineContext context = null;
|
||||||
try {
|
try {
|
||||||
context = EngineFactory.createContext(loadTest, jmxFile, csvFiles);
|
context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, realThreadNum);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,7 +31,7 @@ public class KubernetesTestEngine extends AbstractEngine {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
Integer sumThreadNum = getSumThreadNum();
|
Integer sumThreadNum = getRunningThreadNum();
|
||||||
// resourceList size 1
|
// resourceList size 1
|
||||||
resourceList.forEach(r -> {
|
resourceList.forEach(r -> {
|
||||||
String configuration = r.getConfiguration();
|
String configuration = r.getConfiguration();
|
||||||
|
@ -43,7 +43,7 @@ public class KubernetesTestEngine extends AbstractEngine {
|
||||||
MSException.throwException("资源不足");
|
MSException.throwException("资源不足");
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles);
|
EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles, threadNum);
|
||||||
runTest(context, clientCredential, 1);
|
runTest(context, clientCredential, 1);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
LogUtil.error(e);
|
LogUtil.error(e);
|
||||||
|
|
|
@ -386,6 +386,11 @@ public class JmeterDocumentParser implements DocumentParser {
|
||||||
if (nodeNameEquals(ele, STRING_PROP)) {
|
if (nodeNameEquals(ele, STRING_PROP)) {
|
||||||
parseStringProp(ele);
|
parseStringProp(ele);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 设置具体的线程数
|
||||||
|
if (nodeNameEquals(ele, STRING_PROP) && "TargetLevel".equals(ele.getAttribute("name"))) {
|
||||||
|
ele.getFirstChild().setNodeValue(context.getThreadNum().toString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue