关于并发数

This commit is contained in:
Captain.B 2020-03-24 19:42:38 +08:00
parent 9d10c1e037
commit db211cb76b
12 changed files with 204 additions and 44 deletions

View File

@ -19,6 +19,8 @@ public class LoadTest implements Serializable {
private String status;
private String testResourcePoolId;
private static final long serialVersionUID = 1L;
public String getId() {
@ -84,4 +86,12 @@ public class LoadTest implements Serializable {
public void setStatus(String status) {
this.status = status == null ? null : status.trim();
}
public String getTestResourcePoolId() {
return testResourcePoolId;
}
public void setTestResourcePoolId(String testResourcePoolId) {
this.testResourcePoolId = testResourcePoolId == null ? null : testResourcePoolId.trim();
}
}

View File

@ -643,6 +643,76 @@ public class LoadTestExample {
addCriterion("status not between", value1, value2, "status");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdIsNull() {
addCriterion("test_resource_pool_id is null");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdIsNotNull() {
addCriterion("test_resource_pool_id is not null");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdEqualTo(String value) {
addCriterion("test_resource_pool_id =", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdNotEqualTo(String value) {
addCriterion("test_resource_pool_id <>", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdGreaterThan(String value) {
addCriterion("test_resource_pool_id >", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdGreaterThanOrEqualTo(String value) {
addCriterion("test_resource_pool_id >=", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdLessThan(String value) {
addCriterion("test_resource_pool_id <", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdLessThanOrEqualTo(String value) {
addCriterion("test_resource_pool_id <=", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdLike(String value) {
addCriterion("test_resource_pool_id like", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdNotLike(String value) {
addCriterion("test_resource_pool_id not like", value, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdIn(List<String> values) {
addCriterion("test_resource_pool_id in", values, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdNotIn(List<String> values) {
addCriterion("test_resource_pool_id not in", values, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdBetween(String value1, String value2) {
addCriterion("test_resource_pool_id between", value1, value2, "testResourcePoolId");
return (Criteria) this;
}
public Criteria andTestResourcePoolIdNotBetween(String value1, String value2) {
addCriterion("test_resource_pool_id not between", value1, value2, "testResourcePoolId");
return (Criteria) this;
}
}
public static class Criteria extends GeneratedCriteria {

View File

@ -10,6 +10,7 @@
<result column="create_time" jdbcType="BIGINT" property="createTime" />
<result column="update_time" jdbcType="BIGINT" property="updateTime" />
<result column="status" jdbcType="VARCHAR" property="status" />
<result column="test_resource_pool_id" jdbcType="VARCHAR" property="testResourcePoolId" />
</resultMap>
<resultMap extends="BaseResultMap" id="ResultMapWithBLOBs" type="io.metersphere.base.domain.LoadTestWithBLOBs">
<result column="load_configuration" jdbcType="LONGVARCHAR" property="loadConfiguration" />
@ -76,7 +77,7 @@
</sql>
<sql id="Base_Column_List">
id, project_id, name, description, scenario_definition, create_time, update_time,
status
status, test_resource_pool_id
</sql>
<sql id="Blob_Column_List">
load_configuration, advanced_configuration, schedule
@ -132,14 +133,14 @@
<insert id="insert" parameterType="io.metersphere.base.domain.LoadTestWithBLOBs">
insert into load_test (id, project_id, name,
description, scenario_definition, create_time,
update_time, status, load_configuration,
advanced_configuration, schedule
)
update_time, status, test_resource_pool_id,
load_configuration, advanced_configuration,
schedule)
values (#{id,jdbcType=VARCHAR}, #{projectId,jdbcType=VARCHAR}, #{name,jdbcType=VARCHAR},
#{description,jdbcType=VARCHAR}, #{scenarioDefinition,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT},
#{updateTime,jdbcType=BIGINT}, #{status,jdbcType=VARCHAR}, #{loadConfiguration,jdbcType=LONGVARCHAR},
#{advancedConfiguration,jdbcType=LONGVARCHAR}, #{schedule,jdbcType=LONGVARCHAR}
)
#{updateTime,jdbcType=BIGINT}, #{status,jdbcType=VARCHAR}, #{testResourcePoolId,jdbcType=VARCHAR},
#{loadConfiguration,jdbcType=LONGVARCHAR}, #{advancedConfiguration,jdbcType=LONGVARCHAR},
#{schedule,jdbcType=LONGVARCHAR})
</insert>
<insert id="insertSelective" parameterType="io.metersphere.base.domain.LoadTestWithBLOBs">
insert into load_test
@ -168,6 +169,9 @@
<if test="status != null">
status,
</if>
<if test="testResourcePoolId != null">
test_resource_pool_id,
</if>
<if test="loadConfiguration != null">
load_configuration,
</if>
@ -203,6 +207,9 @@
<if test="status != null">
#{status,jdbcType=VARCHAR},
</if>
<if test="testResourcePoolId != null">
#{testResourcePoolId,jdbcType=VARCHAR},
</if>
<if test="loadConfiguration != null">
#{loadConfiguration,jdbcType=LONGVARCHAR},
</if>
@ -247,6 +254,9 @@
<if test="record.status != null">
status = #{record.status,jdbcType=VARCHAR},
</if>
<if test="record.testResourcePoolId != null">
test_resource_pool_id = #{record.testResourcePoolId,jdbcType=VARCHAR},
</if>
<if test="record.loadConfiguration != null">
load_configuration = #{record.loadConfiguration,jdbcType=LONGVARCHAR},
</if>
@ -271,6 +281,7 @@
create_time = #{record.createTime,jdbcType=BIGINT},
update_time = #{record.updateTime,jdbcType=BIGINT},
status = #{record.status,jdbcType=VARCHAR},
test_resource_pool_id = #{record.testResourcePoolId,jdbcType=VARCHAR},
load_configuration = #{record.loadConfiguration,jdbcType=LONGVARCHAR},
advanced_configuration = #{record.advancedConfiguration,jdbcType=LONGVARCHAR},
schedule = #{record.schedule,jdbcType=LONGVARCHAR}
@ -287,7 +298,8 @@
scenario_definition = #{record.scenarioDefinition,jdbcType=VARCHAR},
create_time = #{record.createTime,jdbcType=BIGINT},
update_time = #{record.updateTime,jdbcType=BIGINT},
status = #{record.status,jdbcType=VARCHAR}
status = #{record.status,jdbcType=VARCHAR},
test_resource_pool_id = #{record.testResourcePoolId,jdbcType=VARCHAR}
<if test="_parameter != null">
<include refid="Update_By_Example_Where_Clause" />
</if>
@ -316,6 +328,9 @@
<if test="status != null">
status = #{status,jdbcType=VARCHAR},
</if>
<if test="testResourcePoolId != null">
test_resource_pool_id = #{testResourcePoolId,jdbcType=VARCHAR},
</if>
<if test="loadConfiguration != null">
load_configuration = #{loadConfiguration,jdbcType=LONGVARCHAR},
</if>
@ -337,6 +352,7 @@
create_time = #{createTime,jdbcType=BIGINT},
update_time = #{updateTime,jdbcType=BIGINT},
status = #{status,jdbcType=VARCHAR},
test_resource_pool_id = #{testResourcePoolId,jdbcType=VARCHAR},
load_configuration = #{loadConfiguration,jdbcType=LONGVARCHAR},
advanced_configuration = #{advancedConfiguration,jdbcType=LONGVARCHAR},
schedule = #{schedule,jdbcType=LONGVARCHAR}
@ -350,7 +366,8 @@
scenario_definition = #{scenarioDefinition,jdbcType=VARCHAR},
create_time = #{createTime,jdbcType=BIGINT},
update_time = #{updateTime,jdbcType=BIGINT},
status = #{status,jdbcType=VARCHAR}
status = #{status,jdbcType=VARCHAR},
test_resource_pool_id = #{testResourcePoolId,jdbcType=VARCHAR}
where id = #{id,jdbcType=VARCHAR}
</update>
</mapper>

View File

@ -1,7 +1,12 @@
package io.metersphere.engine;
import io.metersphere.base.domain.FileMetadata;
import io.metersphere.base.domain.LoadTestWithBLOBs;
import java.util.List;
public interface Engine {
boolean init(EngineContext context);
boolean init(LoadTestWithBLOBs loadTest, FileMetadata fileMetadata, List<FileMetadata> csvFiles);
void start();

View File

@ -27,6 +27,7 @@ import java.util.Map;
@Service
public class EngineFactory {
private static final String RESOURCE_POOL_ID = "resourcePoolId";
private static FileService fileService;
private static TestResourcePoolService testResourcePoolService;
@ -36,7 +37,7 @@ public class EngineFactory {
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"), "resourcePoolId")) {
if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) {
resourcePoolId = jsonObject.getString("value");
break;
}
@ -79,7 +80,7 @@ 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"), "resourcePoolId")) {
if (StringUtils.equals(jsonObject.getString("key"), RESOURCE_POOL_ID)) {
engineContext.setResourcePoolId(jsonObject.getString("value"));
}
}
@ -91,8 +92,10 @@ public class EngineFactory {
MSException.throwException("File type unknown");
}
String content = engineSourceParser.parse(engineContext, new ByteArrayInputStream(fileContent.getFile()));
engineContext.setContent(content);
try (ByteArrayInputStream source = new ByteArrayInputStream(fileContent.getFile())) {
String content = engineSourceParser.parse(engineContext, source);
engineContext.setContent(content);
}
if (CollectionUtils.isNotEmpty(csvFiles)) {
Map<String, String> data = new HashMap<>();

View File

@ -1,69 +1,121 @@
package io.metersphere.engine.kubernetes;
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.ObjectMeta;
import io.fabric8.kubernetes.client.KubernetesClient;
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.commons.utils.LogUtil;
import io.metersphere.engine.Engine;
import io.metersphere.engine.EngineContext;
import io.metersphere.engine.EngineFactory;
import io.metersphere.engine.kubernetes.crds.jmeter.Jmeter;
import io.metersphere.engine.kubernetes.crds.jmeter.JmeterSpec;
import io.metersphere.engine.kubernetes.provider.ClientCredential;
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.lang3.StringUtils;
import java.util.HashMap;
import java.util.List;
public class KubernetesTestEngine implements Engine {
private EngineContext context;
private TestResourcePoolService testResourcePoolService;
private TestResourceService testResourceService;
private FileMetadata jmxFile;
private List<FileMetadata> csvFiles;
private LoadTestWithBLOBs loadTest;
private LoadTestService loadTestService;
private Integer threadNum;
private String resourcePoolId;
private List<TestResource> resourceList;
@Override
public boolean init(EngineContext context) {
this.context = context;
this.testResourcePoolService = CommonBeanFactory.getBean(TestResourcePoolService.class);
this.testResourceService = CommonBeanFactory.getBean(TestResourceService.class);
return true;
}
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);
@Override
public void start() {
if (context == null) {
LogUtil.warn("Please initial the engine.");
return;
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");
}
}
TestResourcePool resourcePool = testResourcePoolService.getResourcePool(context.getResourcePoolId());
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.");
}
List<TestResource> resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId());
if (CollectionUtils.isEmpty(resourceList)) {
this.resourceList = testResourceService.getResourcesByPoolId(resourcePool.getId());
if (CollectionUtils.isEmpty(this.resourceList)) {
MSException.throwException("Test Resource is empty");
}
return true;
}
@Override
public void start() {
// 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 -> {
String configuration = r.getConfiguration();
ClientCredential clientCredential = JSON.parseObject(configuration, ClientCredential.class);
runTest(clientCredential);
// 最大并发数
Integer maxConcurrency = clientCredential.getMaxConcurrency();
// 当前测试需要的并发数大于剩余的并发数报错
if (threadNum > maxConcurrency - sumThreadNum) {
MSException.throwException("资源不足");
}
try {
EngineContext context = EngineFactory.createContext(loadTest, jmxFile, csvFiles);
runTest(context, clientCredential, 1);
} catch (Exception e) {
LogUtil.error(e);
}
});
}
private void runTest(ClientCredential credential) {
private void runTest(EngineContext context, ClientCredential credential, int replicas) {
KubernetesProvider kubernetesProvider = new KubernetesProvider(JSON.toJSONString(credential));
// create namespace
kubernetesProvider.confirmNamespace(context.getNamespace());
@ -94,7 +146,7 @@ public class KubernetesTestEngine implements Engine {
setName(context.getTestId());
}});
jmeter.setSpec(new JmeterSpec() {{
setReplicas(1);
setReplicas(replicas);
setImage("registry.fit2cloud.com/metersphere/jmeter-master:0.0.2");
}});
LogUtil.info("Load test started. " + context.getTestId());

View File

@ -313,7 +313,7 @@ public class JmeterDocumentParser implements DocumentParser {
threadGroup.appendChild(createStringProp(document, "LogFilename", ""));
threadGroup.appendChild(createStringProp(document, "Iterations", "1"));
// todo 单位是S 要修改 成M
threadGroup.appendChild(createStringProp(document, "Unit", "S"));
threadGroup.appendChild(createStringProp(document, "Unit", "M"));
}
private void processCheckoutTimer(Element element) {

View File

@ -186,7 +186,7 @@ public class LoadTestService {
boolean init = true;
try {
init = engine.init(EngineFactory.createContext(loadTest, fileMetadata, csvFiles));
init = engine.init(loadTest, fileMetadata, csvFiles);
} catch (Exception e) {
MSException.throwException(e);
}
@ -226,4 +226,10 @@ public class LoadTestService {
LoadTestWithBLOBs loadTestWithBLOBs = loadTestMapper.selectByPrimaryKey(testId);
return Optional.ofNullable(loadTestWithBLOBs).orElse(new LoadTestWithBLOBs()).getLoadConfiguration();
}
public List<LoadTestWithBLOBs> selectByTestResourcePoolId(String resourcePoolId) {
LoadTestExample example = new LoadTestExample();
example.createCriteria().andTestResourcePoolIdEqualTo(resourcePoolId);
return loadTestMapper.selectByExampleWithBLOBs(example);
}
}

View File

@ -32,6 +32,7 @@ CREATE TABLE IF NOT EXISTS `load_test` (
`create_time` bigint(13) NOT NULL COMMENT 'Create timestamp',
`update_time` bigint(13) NOT NULL COMMENT 'Update timestamp',
`status` varchar(64) DEFAULT NULL COMMENT 'Test Status Running, Completed, Error, etc.',
`test_resource_pool_id` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB

View File

@ -56,9 +56,9 @@
<!-- <table tableName="project"/>-->
<!-- <table tableName="organization"/>-->
<!-- <table tableName="load_test_report"/>-->
<!-- <table tableName="load_test"/>-->
<table tableName="load_test"/>
<!-- <table tableName="file_content"/>-->
<table tableName="file_metadata"/>
<!-- <table tableName="file_metadata"/>-->
<!-- <table tableName="load_test_file"/>-->
</context>
</generatorConfiguration>

View File

@ -160,6 +160,7 @@
this.testPlan.updatedFileList = this.$refs.basicConfig.updatedFileList();
//
this.testPlan.loadConfiguration = JSON.stringify(this.$refs.pressureConfig.convertProperty());
this.testPlan.testResourcePoolId = this.$refs.pressureConfig.resourcePool;
//
this.testPlan.advancedConfiguration = JSON.stringify(this.$refs.advancedConfig.configurations());

View File

@ -104,7 +104,6 @@
const STEPS = "Steps";
const DURATION = "duration";
const RPS_LIMIT = "rpsLimit";
const RESOURCE_POOL = "resourcePoolId";
export default {
name: "PerformancePressureConfig",
@ -174,9 +173,6 @@
case RPS_LIMIT:
this.rpsLimit = d.value;
break;
case RESOURCE_POOL:
this.resourcePool = d.value;
break;
default:
break;
}
@ -299,7 +295,6 @@
{key: STEPS, value: this.step},
{key: DURATION, value: this.duration},
{key: RPS_LIMIT, value: this.rpsLimit},
{key: RESOURCE_POOL, value: this.resourcePool},
];
}
}