1、先完成jmeter脚本编写,具体怎么写此处不再详述,可以参考其他文档、
2、有时你会发现jmeter脚本执行后,有些响应信息你需要提取到文件中,以便后续使用;
怎么实现呢?个人思路就是,先写个java类,这个类的功能就是参数保存到指定文件;
然后进行java请求,保存该文件
具体步骤:
2.1 idea打开创建一个java项目,添加依赖的jmeter jar包
2.2 创建 OutputService 和PerformenceTest两个类
1 import java.io.File; 2 import java.io.PrintWriter; 3 4 /** 5 * Created by Administrator on 2017/8/28. 6 */ 7 public class OutputService { 8 public static void output(String filename,String csName,String csPwd,String ghsName,String ghsPwd,String ywyName,String ywyPwd) throws Exception{ 9 PrintWriter out=new PrintWriter(new File(filename));10 out.write("所有平台注册账号:"+"\r\n");11 out.write("超市注册账号:"+"\r\n"+csName+" "+csPwd+"\r\n"+"供货商注册账号:"+"\r\n"+ghsName+" "+ghsPwd+"\r\n"+"业务员注册账号:"+"\r\n"+ywyName+" "+ywyPwd+"\r\n");12 out.close();13 14 }15 16 }
1 package com.test; 2 3 import org.apache.jmeter.config.Arguments; 4 import org.apache.jmeter.protocol.java.sampler.JavaSamplerClient; 5 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 6 import org.apache.jmeter.samplers.SampleResult; 7 8 9 /**10 * Created by Administrator on 2017/8/28.11 */12 public class PerformenceTest implements JavaSamplerClient {13 private SampleResult results;14 //private String orderNumber;15 private String csName;16 private String csPwd;17 private String ghsName;18 private String ghsPwd;19 private String ywyName;20 private String ywyPwd;21 private String filename;22 23 public static void main(String[] args) {24 // TODO Auto-generated method stub25 Arguments params = new Arguments();26 params.addArgument("filename","0");27 // params.addArgument("orderNumber", "0");//设置参数,并赋予默认值028 params.addArgument("csName", "0");//设置参数,并赋予默认值029 params.addArgument("csPwd", "0");30 params.addArgument("ghsName","0");31 params.addArgument("ghsPwd", "0");32 params.addArgument("ywyName","0");33 params.addArgument("ywyPwd", "0");34 35 JavaSamplerContext arg0 = new JavaSamplerContext(params);36 PerformenceTest test = new PerformenceTest();37 test.setupTest(arg0);38 test.runTest(arg0);39 test.teardownTest(arg0);40 }41 // 设置传入的参数,可以设置多个,已设置的参数会显示到Jmeter的参数列表中42 public Arguments getDefaultParameters() {43 Arguments params = new Arguments();44 params.addArgument("filename", "0");//设置参数,并赋予默认值045 //params.addArgument("orderNumber", "0");//设置参数,并赋予默认值046 params.addArgument("csName", "0");//设置参数,并赋予默认值047 params.addArgument("csPwd","0");48 params.addArgument("ghsName", "0");//设置参数,并赋予默认值049 params.addArgument("ghsPwd","0");50 params.addArgument("ywyName", "0");//设置参数,并赋予默认值051 params.addArgument("ywyPwd","0");52 return params;53 }54 55 56 // 初始化方法,实际运行时每个线程仅执行一次,在测试方法运行前执行57 public void setupTest(JavaSamplerContext arg0) {58 results = new SampleResult();59 }60 61 62 // 测试执行的循环体,根据线程数和循环次数的不同可执行多次63 @Override64 public SampleResult runTest(JavaSamplerContext arg0) {65 // orderNumber = arg0.getParameter("orderNumber"); // 获取在Jmeter中设置的参数值66 csName = arg0.getParameter("csName"); // 获取在Jmeter中设置的参数值67 csPwd = arg0.getParameter("csPwd");68 ghsName = arg0.getParameter("ghsName"); // 获取在Jmeter中设置的参数值69 ghsPwd= arg0.getParameter("ghsPwd");70 ywyName=arg0.getParameter("ywyName");71 ywyPwd=arg0.getParameter("ywyPwd");72 filename = arg0.getParameter("filename"); // 获取在Jmeter中设置的参数值73 results.sampleStart();// jmeter 开始统计响应时间标记74 try {75 OutputService test = new OutputService();76 test.output(filename,csName,csPwd,ghsName,ghsPwd,ywyName,ywyPwd);77 results.setSuccessful(true);78 // 被测对象调用79 } catch (Throwable e) {80 results.setSuccessful(false);81 e.printStackTrace();82 } finally {83 results.sampleEnd();// jmeter 结束统计响应时间标记84 }85 return results;86 }87 88 89 // 结束方法,实际运行时每个线程仅执行一次,在测试方法运行结束后执行90 public void teardownTest(JavaSamplerContext arg0) {91 }92 93 }
2.3 打jar包,步骤如图所示
2.4 然后把打好的JavaSampler.jar包cp到jmeter目录\lib\ext下
3、重启jmeter,加载刚导入的jar包
4、创建java请求,选择jar包的类名称,参数会自动加载出来,且默认值均为0,然后给各个参数赋值,下图是我给参数化了,也可以写具体的值
执行该请求,参数值会保存至指定的文件
执行结果如下图所示