Jmeter之RPC協議指令碼開發實現
阿新 • • 發佈:2019-02-01
1、首先通過本文是通過jmeter的java請求方式實現RPC協議指令碼請求,具體關於java請求如果基於jmeter實現見:jmeter之java請求;如果還想了解rpc指令碼開發詳細見:rpc協議之hprose介面測試 。
2、分別需要建立兩個類 send類、ApiServiceRPCInterface類(完成jmeter一些操作):
首先需要新建一個java工程,然後匯入hprose的依賴jar包
send類如下:
package com.apiservicerpc.test; import java.util.HashMap; import java.util.Map; import hprose.client.HproseHttpClient; public class Send { @SuppressWarnings("unchecked") public static Map<String, Object> sendRequest(int id,String arg1) throws Throwable{ Map<String,Object> map=new HashMap<String,Object>(); //例項化一個HproseHttpClient物件 HproseHttpClient client = new HproseHttpClient(); //建立一個client 連線 client.useService("http://www.test.com/ordertest/Ordertest"); map = (Map<String, Object>) client.invoke("getOrderId",new Object[]{id,arg1}); System.out.println("傳送請求完成:----------響應資訊為:-" + map ); return map; } //注意:如果在jmeter中執行時候一定要註釋掉main方法部分哦 public static void main(String[] args) throws Throwable { // TODO Auto-generated method stub String responseMsg; int id = 1111170144; String fields = "id,num,price"; Map<String,Object> map=new HashMap<String,Object>(); map = Send.sendRequest(id, arg1); System.out.println("*********返回結果:" + map); responseMsg = map.get("code").toString(); System.out.println(); if(responseMsg.equals("0")) { System.out.println("********成功*********"); }else { System.out.println("---------失敗----------" + map); } } }
ApiServiceRPCInterface類如下:
package com.apiservicerpc.test; import java.util.HashMap; import java.util.Map; 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; public class ApiServiceRPCInterface extends AbstractJavaSamplerClient { Map<String,Object> map; String result; String url = "" ; private int id = 0; private String arg1 = null; String responseMsg = null; //自定義java方法入參 //params.addArgument("num1","");表示入參名為num1,預設值為空 //設定可用引數及預設值 public Arguments getDefaultParameters(){ Arguments params = new Arguments(); params.addArgument("url","輸入http://IP:port"); params.addArgument("id","123456789"); params.addArgument("arg1","id,num,price"); return params; } //執行緒執行前的初始化工作 public void setupTest(JavaSamplerContext arg0){ map=new HashMap<String,Object>(); } @Override public SampleResult runTest(JavaSamplerContext arg0) { url = arg0.getParameter("url"); order_id = arg0.getIntParameter("id"); // System.out.println("---------------" + id); fields = arg0.getParameter("arg1"); // System.out.println("---------------" + arg1); SampleResult sr = new SampleResult(); sr.setSampleLabel("通過定單id獲取定單資訊"); //事物開始標記 ,jmeter開始統計響應時間 sr.sampleStart(); try { map = Send.sendRequest(id, arg1); // System.out.println("我是伺服器響應的map物件" + map); responseMsg = map.get("code").toString(); if(responseMsg.equals("0")){ //在檢視結果樹種展現結果 //sr.setResponseData("正確結果是:" +responseCode,null); sr.setResponseData("正確結果是:" + map,null); sr.setDataType(SampleResult.TEXT); //設定事物是否通過 sr.setSuccessful(true); }else{ sr.setResponseData("錯誤結果是:" + map,null); //sr.setResponseData("錯誤結果是:" + responseCode,null); sr.setDataType(SampleResult.TEXT); sr.setSuccessful(false); } } catch (Exception e) { // TODO Auto-generated catch block sr.setSuccessful(false); } catch (Throwable e) { // TODO Auto-generated catch block sr.setSuccessful(false); e.printStackTrace(); } sr.sampleEnd(); //事物結束 return sr; } //測試結束時呼叫SendTest2.java public void teardownTest(){ } }
3、最後匯出jar包、直接放入jmeter中執行就可以了(有問題就直接留言哦)