JMeter運行你區定義本身的取樣器sampler,方法很簡單,只需繼承類org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient,並重寫若干方法便可。java
首先,建立一個maven項目,並加入jmeter相關的依賴包:apache
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方法便可dom
代碼示例以下:maven
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 }
選中Java Request這個sampler,能夠設置參數name的值ide
查看」View Results in Table」中的結果:
oop