JMeter运行你区定义自己的取样器sampler,方法很简单,只需继承类org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient,并重写若干方法即可。
首先,创建一个maven项目,并加入jmeter相关的依赖包:
<dependencies>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_core</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_http</artifactId>
<version>2.10</version>
</dependency>
<dependency>
<groupId>org.apache.jmeter</groupId>
<artifactId>ApacheJMeter_java</artifactId>
<version>2.10</version>
</dependency>
</dependencies>
然后,创建一个类,继承自AbstractJavaSamplerClient,并重写setupTest、teardownTest、runTest与getDefaultParameters方法即可
代码示例如下:
package pengliu.me.jmeter.plugins; import org.apache.jmeter.config.Arguments;
import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient;
import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jorphan.logging.LoggingManager;
import org.apache.log.Logger; import java.util.Iterator;
import java.util.Random; public class MySampler extends AbstractJavaSamplerClient
{
private static final Logger log = LoggingManager.getLoggerForClass(); @Override
public void setupTest(JavaSamplerContext context)
{
Iterator<String> it = context.getParameterNamesIterator();
while (it.hasNext())
{
String paramName = it.next();
this.log.info(String.format("-----------------paramName: %s, paramValue: %s-----------------",
paramName,
context.getParameter(paramName)));
} this.log.info(String.format("-----------------Get param 'name' from JMeter: %s-----------------",
context.getParameter("name", "Default Name")));
this.log.info(String.format("-----------------Current Thread Id: %s, Start 'setupTest(JavaSamplerContext context)'--------------------",
Thread.currentThread().getId()));
} @Override
public void teardownTest(JavaSamplerContext context)
{
this.log.info(String.format("-----------------Current Thread Id: %s, Start 'teardownTest(JavaSamplerContext context)'--------------------",
Thread.currentThread().getId()));
} public SampleResult runTest(JavaSamplerContext javaSamplerContext)
{
SampleResult result = new SampleResult();
result.sampleStart();
try
{
Random r = new Random();
long sleepTime = (long)r.nextInt(5000);
this.log.info(String.format("-----------------Current Thread Id: %s, Running Test, sleep time is %s--------------------",
Thread.currentThread().getId(),
sleepTime));
Thread.sleep(sleepTime); if(sleepTime%2 == 0)
{
throw new Exception("--------This is my exception from runTest!!!---------");
} result.sampleEnd();
result.setSuccessful(true);
result.setSampleLabel("My Lable: SUCCESS: " + Thread.currentThread().getId());
}
catch (Throwable e)
{
result.sampleEnd();
result.setSuccessful(false);
result.setSampleLabel("My Lable: FAIL: " + Thread.currentThread().getId()); e.printStackTrace();
} return result;
} @Override
public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("name", "edw"); return params;
}
}
- 其中setupTest与teardownTest方法,在一个线程只被执行一次,无论此Thread Group配置的Loop Count是多少,每个线程只运行一次这两个方法。
- 其中runTest方法,如果一个线程配置了多次的Loop Count,那么此runTest就会运行多次
- 其中getDefaultParameters方法,返回的是JMeter界面的参数列表,如下图:
- 使用”mvn package”命令打包上面的sampler:
- 将打包好的jar包放到JMeter的lib/ext目录下:
- 启动JMeter, 并加入一个Java Request的sampler:
- Classname选择代码中自定义的sampler类:
- 配置Thread Group,线程数为2,循环数为5:
选中Java Request这个sampler,可以设置参数name的值
- 开始执行后,JMeter会产生两个线程,每个线程都跑只跑一遍setupTest与teardownTest方法,但会跑5遍runTest方法
- 查看JMeter的日志输出:
查看”View Results in Table”中的结果: