getDefaultParameters() 用于获取界面的参数 |
|
runTest(JavaSamplerContext context) 类似于LR的Action |
|
void |
setupTest(JavaSamplerContext context) 初始化方法,类似于LR的init和Junit中的setUp() |
void |
teardownTest(JavaSamplerContext context) 类似于LR的end和Junit中的tearDown() |
1 package com.test.jmeter; 2 3 import java.io.IOException; 4 5 import org.apache.http.client.ClientProtocolException; 6 import org.apache.jmeter.config.Arguments; 7 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 8 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 9 import org.apache.jmeter.samplers.SampleResult; 10 11 public class Jmeter_GetSearchSuggestion extends AbstractJavaSamplerClient { 12 13 private static String label = "Jmeter_GetSearchSuggestion "; //定义label名称,显示在jmeter的结果窗口 14 private String url; 15 private String data; 16 17 public void setupTest(){ 18 //定义测试初始值,setupTest只在测试开始前使用 19 System.out.println("setupTest"); 20 } 21 22 @Override 23 public SampleResult runTest(JavaSamplerContext arg0) { 24 25 url = arg0.getParameter("url"); 26 data = arg0.getParameter("data"); 27 SampleResult sr; 28 sr = new SampleResult(); 29 sr.setSampleLabel(label); 30 31 TestApiGetSearchSuggestion t = new TestApiGetSearchSuggestion(); 32 sr.sampleStart(); 33 try { 34 //调用被压测接口的方法 35 t.PostJson(url, data); 36 sr.setSuccessful(true); 37 } catch (ClientProtocolException e) { 38 sr.setSuccessful(false); 39 e.printStackTrace(); 40 } catch (IOException e) { 41 sr.setSuccessful(false); 42 e.printStackTrace(); 43 } 44 45 sr.sampleEnd(); // jmeter 结束统计响应时间标记 46 return sr; 47 } 48 49 public void teardownTest(JavaSamplerContext arg0){ 50 super.teardownTest(arg0); 51 } 52 53 public Arguments getDefaultParameters(){ 54 //参数定义,显示在前台,也可以不定义 55 Arguments params = new Arguments(); 56 params.addArgument("url", "http://gapp.test.com/merchandise/GetSearchSuggestion"); 57 params.addArgument("data", "data={\"token\": \"aaaaaaaaaa\",\"body\": {\"keywords\": \"蓝月亮\"}}"); 58 return params; 59 } 60 61 }
原文:http://www.cnblogs.com/liu-ke/p/4325607.html