1.引入相关依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jmeter-version>5.0</jmeter-version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>${jmeter-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>${jmeter-version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<!--将依赖注入jar包-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>util.Microseer</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2.创建一个实现org.apache.jmeter.protocol.java.sampler.JavaSamplerClient接口的测试实现类,实现四个抽象方法
public interface JavaSamplerClient {
/**
* 测试开始前的初始化方法,如提前获取参数,以避免runTest中获取参数,减少性能开销
*/
void setupTest(JavaSamplerContext context);
/**
* 测试主流程
*/
SampleResult runTest(JavaSamplerContext context);
/**
* 测试流程结束后的收尾工作
*/
void teardownTest(JavaSamplerContext context);
/**
* GUI界面中展示的默认参数
*/
Arguments getDefaultParameters();
}