Skip to content

Commit

Permalink
feat: 初始化提交
Browse files Browse the repository at this point in the history
  • Loading branch information
wan92hen committed Nov 4, 2021
0 parents commit bd499f6
Show file tree
Hide file tree
Showing 8 changed files with 301 additions and 0 deletions.
68 changes: 68 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>io.metersphere</groupId>
<artifactId>metersphere-plugin-ParallelController</artifactId>
<version>1.0.0</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<custom.lib-path>${pom.basedir}/src/main/resources</custom.lib-path>
</properties>

<dependencies>
<dependency>
<groupId>io.metersphere</groupId>
<artifactId>metersphere-plugin-core</artifactId>
<version>1.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.blazemeter</groupId>
<artifactId>jmeter-parallel</artifactId>
<version>0.11</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.5</version>
<configuration>
<descriptors>
<descriptor>${custom.lib-path}/xml/assembly.xml</descriptor><!-- 指定assembly配置文件路径 -->
</descriptors>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
54 changes: 54 additions & 0 deletions src/main/java/io/metersphere/plugin/parallel/UiScriptApiImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package io.metersphere.plugin.parallel;

import io.metersphere.plugin.core.api.UiScriptApi;
import io.metersphere.plugin.core.ui.PluginResource;
import io.metersphere.plugin.core.ui.UiScript;
import io.metersphere.plugin.core.utils.LogUtil;

import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

public class UiScriptApiImpl extends UiScriptApi {
/**
* 企业版插件增加 这个方法
*
* @return 是否是企业版插件
*/
public boolean xpack() {
return false;
}

@Override
public PluginResource init() {
LogUtil.info("开始初始化脚本内容 ");
List<UiScript> uiScripts = new LinkedList<>();
String scriptString = getJson("/json/ui_parallel.json");
UiScript script = new UiScript("parallel_controller", "并行控制器", "io.metersphere.plugin.parallel.controller.MsParallelController", scriptString);
script.setJmeterClazz("GenericController");

// 添加可选参数
script.setFormOption(getJson("/json/ui_form.json"));

uiScripts.add(script);
LogUtil.info("初始化脚本内容结束 ");
return new PluginResource("parallel-v1.0.0", uiScripts);
}

@Override
public String customMethod(String req) {
LogUtil.info("Parallel Controller 自定义方法");
return null;
}

public String getJson(String path) {
try {
InputStream in = UiScriptApiImpl.class.getResourceAsStream(path);
String json = org.apache.commons.io.IOUtils.toString(in);
return json;
} catch (Exception ex) {
LogUtil.error(ex.getMessage());
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.metersphere.plugin.parallel.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import io.metersphere.plugin.core.MsParameter;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.plugin.core.utils.LogUtil;
import io.metersphere.plugin.parallel.utils.ElementUtil;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import com.blazemeter.jmeter.controller.ParallelSampler;

import java.util.LinkedList;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = true)
public class MsParallelController extends MsTestElement {
public MsParallelController() {

}
private String clazzName = "io.metersphere.plugin.parallel.controller.MsParallelController";

@JSONField(ordinal = 21)
private Boolean limitMaxThread;
@JSONField(ordinal = 22)
private String maxThreads;

@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, MsParameter config) {
LogUtil.info("===========开始转换MsDummySampler ==================");
if (!this.isEnable()) {
return;
}
ParallelSampler initParallelSampler = initParallelSampler();
if (initParallelSampler != null) {
final HashTree groupTree = tree.add(initParallelSampler());
if (CollectionUtils.isNotEmpty(hashTree)) {
hashTree.forEach(el -> {
// 给所有孩子加一个父亲标志
el.setParent(this);
el.toHashTree(groupTree, el.getHashTree(), config);
});
}
}
else {
LogUtil.error("Connect Sampler 生成失败");
throw new RuntimeException("Connect Sampler生成失败");
}
}

public ParallelSampler initParallelSampler() {
try {
ParallelSampler parallelSampler = new ParallelSampler();
// base 执行时需要的参数
parallelSampler.setProperty("MS-ID", this.getId());
String indexPath = this.getIndex();
parallelSampler.setProperty("MS-RESOURCE-ID", this.getResourceId() + "_" + ElementUtil.getFullIndexPath(this.getParent(), indexPath));
List<String> id_names = new LinkedList<>();
ElementUtil.getScenarioSet(this, id_names);
parallelSampler.setProperty("MS-SCENARIO", JSON.toJSONString(id_names));

parallelSampler.setEnabled(this.isEnable());
parallelSampler.setName(this.getName());
parallelSampler.setProperty(TestElement.GUI_CLASS, "com.blazemeter.jmeter.controller.ParallelControllerGui");
parallelSampler.setProperty(TestElement.TEST_CLASS, "com.blazemeter.jmeter.controller.ParallelSampler");

parallelSampler.setProperty("PARENT_SAMPLE", false);
parallelSampler.setProperty("LIMIT_MAX_THREAD_NUMBER", this.getLimitMaxThread());
if (this.getLimitMaxThread() != null && this.getLimitMaxThread()) {
parallelSampler.setProperty("MAX_THREAD_NUMBER", this.getMaxThreads());
}

return parallelSampler;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.metersphere.plugin.parallel.utils;

import io.metersphere.plugin.core.MsTestElement;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class ElementUtil {
public static void getScenarioSet(MsTestElement element, List<String> id_names) {
if (StringUtils.equals(element.getType(), "scenario")) {
id_names.add(element.getResourceId() + "_" + element.getName());
}
if (element.getParent() == null) {
return;
}
getScenarioSet(element.getParent(), id_names);
}

public static String getFullIndexPath(MsTestElement element, String path) {
if (element == null || element.getParent() == null) {
return path;
}
path = element.getIndex() + "_" + path;
return getFullIndexPath(element.getParent(), path);
}
}
11 changes: 11 additions & 0 deletions src/main/resources/json/ui_form.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"form": {
"inline": false,
"labelPosition": "left",
"size": "mini",
"labelWidth": "200px",
"hideRequiredAsterisk": false,
"showMessage": true,
"inlineMessage": false
}
}
33 changes: 33 additions & 0 deletions src/main/resources/json/ui_parallel.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[
{
"type": "switch",
"field": "limitMaxThread",
"title": "Limit max thread number",
"info": "",
"_fc_drag_tag": "switch",
"hidden": false,
"display": true,
"value": false,
"props": {
"activeText": "",
"inactiveText": ""
},
"control": [
{
"value": true,
"rule": [
{
"type": "inputNumber",
"field": "maxThreads",
"title": "Max threads",
"info": "",
"_fc_drag_tag": "inputNumber",
"hidden": false,
"display": true,
"value": 5
}
]
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
io.metersphere.plugin.parallel.UiScriptApiImpl
26 changes: 26 additions & 0 deletions src/main/resources/xml/assembly.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<assembly
xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.0 http://maven.apache.org/xsd/assembly-1.1.0.xsd">
<id>jar-with-all-dependencies</id>
<formats>
<format>jar</format>
</formats>
<includeBaseDirectory>false</includeBaseDirectory>
<dependencySets>
<dependencySet>
<outputDirectory>/</outputDirectory>
<useProjectArtifact>true</useProjectArtifact>
<unpack>true</unpack>
<scope>runtime</scope>
<useTransitiveDependencies>false</useTransitiveDependencies>
</dependencySet>
<dependencySet>
<outputDirectory>/</outputDirectory>
<unpack>true</unpack>
<scope>system</scope>
<useTransitiveDependencies>false</useTransitiveDependencies>
</dependencySet>
</dependencySets>

</assembly>

0 comments on commit bd499f6

Please sign in to comment.