jmeter傳送java請求
一、建立一個java工程
- 1、新建一個java Project
File->new->JavaProject
- 2、在Eclipse下匯入$\jmeter\lib\ext\下的所有jar包;
匯入額外的5個jar包,在 $\jmeter\lib\ 下
還有被測的介面的jar包也要匯入
- 建立class,將新建的類extends AbstractJavaSamplerClient 類
繼承"getDefaultParameters","setupTest","runTest"和"teardownTest"四個方法:
public Arguments getDefaultParameters();設定可用引數及的預設值;
public void setupTest(JavaSamplerContext arg0):每個執行緒測試前執行一次,做一些初始化工作;
public SampleResult runTest(JavaSamplerContext arg0):開始測試,從arg0引數可以獲得引數值;
public void teardownTest(JavaSamplerContext arg0):測試結束時呼叫;
private String getResponseData(ApplyResult rs):將返回進行json格式化
二、繼承類AbstractJavaSamplerClient(程式碼示例為介面 getApplyProcess)
- 1、getDefaultParameters: 獲取預設引數以及引數值
(這裡只有一個引數 certi_code,介面getApplyProcess 只有一個入參,如果多個入參,應全部都新增上)
- 2、setupTest: 測試方法執行之前的工作(初始化記錄結果;從引數列表中獲取對應變)
(如果是有多個入參,也應都新增)
- 3、runTest:執行測試方法(初始化記錄結果,設定開始/結束記錄時間,測試呼叫Service的主方法,測試成功與否的判斷方法)
(1)配置獲取:(setUrl提供的url,所在的系統地址而定;setInterface是介面的 實現類名)
(2)請求、返回記錄:(setSamplerData為請求資料,getResponseData為返回資料)
- 4、teardownTest 固定寫法
- 5、返回進行json的格式化
三、匯出jar
- 右鍵點選專案:Export… 直接匯出為 jar包就可以
- export後的jar包放在$/jmeter/lib/ext/下($/jmeter為jmeter的安裝路徑)
四、執行java請求
1、在$\jmeter\bin\下執行jmeter.bat
2、右鍵點選“測試計劃”新增一個Thread Group(執行緒組)
3、右鍵點選執行緒組,新增一個Java Request費用狀態查詢
五、介面getApplyProcess示例java檔案
java檔案放在了“檔案”頁籤
備註:JsonFormatTool.java為json格式轉換程式碼
package salesLoad; import org.apache.jmeter.config.Arguments; import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; import org.apache.jmeter.samplers.SampleResult; import com.alibaba.dubbo.config.ApplicationConfig; import com.alibaba.dubbo.config.ReferenceConfig; import com.alibaba.dubbo.config.utils.ReferenceConfigCache; import com.google.gson.Gson; import com.ulic.agent.ws.enter.FreshManEnterInterface; import com.ulic.agent.ws.enter.model.ApplyResult; import com.ulic.im.client.vo.AppInfo; public class GetApplyProcess extends AbstractJavaSamplerClient{ private FreshManEnterInterface enterService; private ReferenceConfig<FreshManEnterInterface> reference; private ReferenceConfigCache cache; private String certiCode; // 這個方法是用來自定義java方法入參的。 // params.addArgument("num1","");表示入參名字叫num1,預設值為空。 public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("certi_code", ""); return params; } //每個執行緒測試前執行一次,做一些初始化工作; public void setupTest(JavaSamplerContext arg0) { certiCode = arg0.getParameter("certi_code"); } @Override public SampleResult runTest(JavaSamplerContext arg0) { setupTest(arg0); reference = new ReferenceConfig<FreshManEnterInterface>(); reference.setApplication(new ApplicationConfig("letmetest")); reference.setTimeout(300000); reference.setUrl("dubbo://10.10.1.11:20881"); reference.setInterface("com.mims.agent.ws.enter.FreshManEnterInterface"); cache = ReferenceConfigCache.getCache(); enterService = cache.get(reference); AppInfo app = new AppInfo(); app.setAppCode("MAP"); app.setAppIP("10.51.4.9"); ApplyResult rs = null; SampleResult sr = new SampleResult(); try { sr.sampleStart(); sr.setSamplerData("請求引數certi_code:"+certiCode); rs = enterService.getApplyProcess(app, certiCode); sr.setResponseData(getResponseData(rs), null); sr.setDataType(SampleResult.TEXT); sr.setResponseCode("0000"); Thread.sleep(10); sr.setSuccessful(true); }catch(Exception e){ e.printStackTrace(); sr.setResponseCode("0001"); sr.setSuccessful(false); }finally{ sr.sampleEnd(); } return sr; } private String getResponseData(ApplyResult rs) { Gson gson = new Gson(); String s = gson.toJson(rs, ApplyResult.class); JsonFormatTool jft = new JsonFormatTool(); return jft.formatJson(s); } public void teardownTest(JavaSamplerContext arg0){ super.teardownTest(arg0); } }
1 package com.ulic.loadtest.esales; 2 3 import java.math.BigDecimal; 4 5 import org.apache.jmeter.config.Arguments; 6 import org.apache.jmeter.protocol.java.sampler.AbstractJavaSamplerClient; 7 import org.apache.jmeter.protocol.java.sampler.JavaSamplerContext; 8 import org.apache.jmeter.samplers.SampleResult; 9 10 import com.alibaba.dubbo.config.ApplicationConfig; 11 import com.alibaba.dubbo.config.ReferenceConfig; 12 import com.alibaba.dubbo.config.utils.ReferenceConfigCache; 13 import com.ulic.esales.ws.EsalesServiceInterface; 14 import com.ulic.esales.ws.vo.AoPolicyProductVO; 15 import com.ulic.im.client.vo.AppInfo; 16 import com.ulic.loadtest.util.JsonFormatTool; 17 18 public class CheckAoProductRule extends AbstractJavaSamplerClient { 19 20 private EsalesServiceInterface esalesService; 21 private ReferenceConfig<EsalesServiceInterface> reference; 22 private ReferenceConfigCache cache; 23 24 private AoPolicyProductVO vo; 25 26 // 這個方法是用來自定義java方法入參的。 27 // params.addArgument("num1","");表示入參名字叫num1,預設值為空。 28 public Arguments getDefaultParameters() { 29 Arguments params = new Arguments(); 30 params.addArgument("item_id", "0"); 31 params.addArgument("policy_id", "0"); 32 params.addArgument("product_id", "0"); 33 params.addArgument("product_num", ""); 34 params.addArgument("master_id", "0"); 35 params.addArgument("main_rider", ""); 36 params.addArgument("amount", "0"); 37 params.addArgument("period_prem", "0"); 38 params.addArgument("unit", "0"); 39 params.addArgument("relation_1", "0"); 40 params.addArgument("insurant_1", "0"); 41 params.addArgument("age_1", "0"); 42 params.addArgument("bonus_choice", "0"); 43 params.addArgument("charge_period", ""); 44 params.addArgument("charge_year", "0"); 45 params.addArgument("coverage_period", ""); 46 params.addArgument("coverage_year", "0"); 47 params.addArgument("pay_year", "0"); 48 params.addArgument("insert_id", "0"); 49 return params; 50 } 51 52 @Override 53 public SampleResult runTest(JavaSamplerContext arg0) { 54 vo = new AoPolicyProductVO(); 55 vo.setItemId(Long.parseLong(arg0.getParameter("item_id"))); 56 vo.setPolicyId(Long.parseLong(arg0.getParameter("policy_id"))); 57 vo.setProductId(Long.parseLong(arg0.getParameter("product_id"))); 58 vo.setProductNum(arg0.getParameter("product_num")); 59 vo.setMasterId(Long.parseLong(arg0.getParameter("master_id"))); 60 vo.setMainRider(arg0.getParameter("main_rider")); 61 vo.setAmount(new BigDecimal(Float.parseFloat(arg0.getParameter("amount")))); 62 vo.setPeriodPrem(new BigDecimal(Float.parseFloat(arg0.getParameter("period_prem")))); 63 vo.setUnit(new BigDecimal(arg0.getParameter("unit"))); 64 vo.setRelation1(Long.parseLong(arg0.getParameter("relation_1"))); 65 vo.setInsurant1(Long.parseLong(arg0.getParameter("insurant_1"))); 66 vo.setAge1(Long.parseLong(arg0.getParameter("age_1"))); 67 vo.setBonusChoice(Long.parseLong(arg0.getParameter("bonus_choice"))); 68 vo.setChargePeriod(arg0.getParameter("charge_period")); 69 vo.setChargeYear(Long.parseLong(arg0.getParameter("charge_year"))); 70 vo.setCoveragePeriod(arg0.getParameter("coverage_period")); 71 vo.setCoverageYear(Long.parseLong(arg0.getParameter("coverage_year"))); 72 vo.setPayYear(Long.parseLong(arg0.getParameter("pay_year"))); 73 vo.setInsertId(Long.parseLong(arg0.getParameter("insert_id"))); 74 75 reference = new ReferenceConfig<EsalesServiceInterface>(); 76 reference.setApplication(new ApplicationConfig("letmetest")); 77 reference.setTimeout(300000); 78 reference.setUrl("dubbo://10.181.3.16:21889"); 79 //reference.setRegistry(registry); 80 reference.setInterface("com.ulic.esales.ws.EsalesServiceInterface"); 81 cache = ReferenceConfigCache.getCache(); 82 esalesService = cache.get(reference); 83 //moaQueryService = reference.get(); 84 85 AppInfo app = new AppInfo(); 86 app.setAppCode("ESALES"); 87 app.setAppIP("10.51.40.4"); 88 String rs = null; 89 90 SampleResult sr = new SampleResult(); 91 try { 92 sr.sampleStart(); 93 94 rs = esalesService.checkAoProductRule(app, vo); 95 96 sr.setResponseData(getResponseData(rs), null); 97 sr.setDataType(SampleResult.TEXT); 98 sr.setResponseCode("0000"); 99 100 Thread.sleep(10); 101 sr.setSuccessful(true); 102 }catch(Exception e){ 103 e.printStackTrace(); 104 sr.setResponseCode("0001"); 105 sr.setSuccessful(false); 106 }finally{ 107 sr.sampleEnd(); 108 } 109 return sr; 110 } 111 112 private String getResponseData(String rs) { 113 JsonFormatTool jft = new JsonFormatTool(); 114 115 return jft.formatJson(rs); 116 } 117 118 //每個執行緒測試前執行一次,做一些初始化工作; 119 public void setupTest(JavaSamplerContext arg0) { 120 super.setupTest(arg0); 121 } 122 123 //每個執行緒測試結束後執行一次 124 public void teardownTest(JavaSamplerContext arg0){ 125 super.teardownTest(arg0); 126 } 127 128 }