一、先完成jmeter腳本編寫,具體怎麼寫此處再也不詳述,能夠參考其餘文檔、java
二、有時你會發現jmeter腳本執行後,有些響應信息你須要提取到文件中,以便後續使用;apache
怎麼實現呢?我的思路就是,先寫個java類,這個類的功能就是參數保存到指定文件;ide
而後進行java請求,保存該文件測試
具體步驟:idea
2.1 idea打開建立一個java項目,添加依賴的jmeter jar包spa
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 stub 25 Arguments params = new Arguments(); 26 params.addArgument("filename","0"); 27 // params.addArgument("orderNumber", "0");//設置參數,並賦予默認值0 28 params.addArgument("csName", "0");//設置參數,並賦予默認值0 29 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");//設置參數,並賦予默認值0 45 //params.addArgument("orderNumber", "0");//設置參數,並賦予默認值0 46 params.addArgument("csName", "0");//設置參數,並賦予默認值0 47 params.addArgument("csPwd","0"); 48 params.addArgument("ghsName", "0");//設置參數,並賦予默認值0 49 params.addArgument("ghsPwd","0"); 50 params.addArgument("ywyName", "0");//設置參數,並賦予默認值0 51 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 @Override 64 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包,步驟如圖所示code
2.4 而後把打好的JavaSampler.jar包cp到jmeter目錄\lib\ext下orm
三、重啓jmeter,加載剛導入的jar包對象
四、建立java請求,選擇jar包的類名稱,參數會自動加載出來,且默認值均爲0,而後給各個參數賦值,下圖是我給參數化了,也能夠寫具體的值
執行該請求,參數值會保存至指定的文件
執行結果以下圖所示