定制JMeter取样器
-
JMeter运行你区定义自己的取样器sampler,方法很简单,只需继承类org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient,并重写若干方法即可。
-
首先,创建一个maven项目,并加入jmeter相关的依赖包:
1 <dependencies> 2 <dependency> 3 <groupId>org.apache.jmeter</groupId> 4 <artifactId>ApacheJMeter</artifactId> 5 <version>2.10</version> 6 </dependency> 7 <dependency> 8 <groupId>org.apache.jmeter</groupId> 9 <artifactId>ApacheJMeter_core</artifactId> 10 <version>2.10</version> 11 </dependency> 12 <dependency> 13 <groupId>org.apache.jmeter</groupId> 14 <artifactId>ApacheJMeter_http</artifactId> 15 <version>2.10</version> 16 </dependency> 17 <dependency> 18 <groupId>org.apache.jmeter</groupId> 19 <artifactId>ApacheJMeter_java</artifactId> 20 <version>2.10</version> 21 </dependency> 22 </dependencies>
-
然后,创建一个类,继承自AbstractJavaSamplerClient,并重写setupTest、teardownTest、runTest与getDefaultParameters方法即可
-
代码示例如下:
1 package pengliu.me.jmeter.plugins; 2 3 import org.apache.jmeter.config.Arguments; 4 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 5 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 6 import org.apache.jmeter.samplers.SampleResult; 7 import org.apache.jorphan.logging.LoggingManager; 8 import org.apache.log.Logger; 9 10 import java.util.Iterator; 11 import java.util.Random; 12 13 public class MySampler extends AbstractJavaSamplerClient 14 { 15 private static final Logger log = LoggingManager.getLoggerForClass(); 16 17 @Override 18 public void setupTest(JavaSamplerContext context) 19 { 20 Iterator<String> it = context.getParameterNamesIterator(); 21 while (it.hasNext()) 22 { 23 String paramName = it.next(); 24 this.log.info(String.format("-----------------paramName: %s, paramValue: %s-----------------", 25 paramName, 26 context.getParameter(paramName))); 27 } 28 29 this.log.info(String.format("-----------------Get param 'name' from JMeter: %s-----------------", 30 context.getParameter("name", "Default Name"))); 31 this.log.info(String.format("-----------------Current Thread Id: %s, Start 'setupTest(JavaSamplerContext context)'--------------------", 32 Thread.currentThread().getId())); 33 } 34 35 @Override 36 public void teardownTest(JavaSamplerContext context) 37 { 38 this.log.info(String.format("-----------------Current Thread Id: %s, Start 'teardownTest(JavaSamplerContext context)'--------------------", 39 Thread.currentThread().getId())); 40 } 41 42 public SampleResult runTest(JavaSamplerContext javaSamplerContext) 43 { 44 SampleResult result = new SampleResult(); 45 result.sampleStart(); 46 try 47 { 48 Random r = new Random(); 49 long sleepTime = (long)r.nextInt(5000); 50 this.log.info(String.format("-----------------Current Thread Id: %s, Running Test, sleep time is %s--------------------", 51 Thread.currentThread().getId(), 52 sleepTime)); 53 Thread.sleep(sleepTime); 54 55 if(sleepTime%2 == 0) 56 { 57 throw new Exception("--------This is my exception from runTest!!!---------"); 58 } 59 60 result.sampleEnd(); 61 result.setSuccessful(true); 62 result.setSampleLabel("My Lable: SUCCESS: " + Thread.currentThread().getId()); 63 } 64 catch (Throwable e) 65 { 66 result.sampleEnd(); 67 result.setSuccessful(false); 68 result.setSampleLabel("My Lable: FAIL: " + Thread.currentThread().getId()); 69 70 e.printStackTrace(); 71 } 72 73 return result; 74 } 75 76 @Override 77 public Arguments getDefaultParameters() { 78 79 Arguments params = new Arguments(); 80 81 params.addArgument("name", "edw"); 82 83 return params; 84 } 85 }
- 其中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”中的结果: