Jmeter Java请求
一、为什么要使用Java请求
现有的post 、 get 无法对加解密及鉴权的接口进行测试,需要进行封装,然后再请求,怎么进行封装?通常使用的方式是使用httpclient 工具类型来操作,下面举一个简单的Java自定义测试代码例子,使用Java编译器编写测试代码
二、完成以下几部曲即可
1、打开IDE(这里我使用的是 eclipse Luna 4.3),创建一个Java工程项目名为enterprise
2、从Jmeter的安装目录lib/ext中拷贝两个文件ApacheJMeter_core.jar(必须)和ApacheJMeter_java.jar(必须)到enterprise项目中,然后引入这两个JAR包;如果需要在eclipse中调试还需要拷贝以下三个工具包可选avalon-framework-4.1.4.jar(jmeter3.2中名称是framework)、jorphan.jar、logkit-2.0.jar。
3、创建一个测试类CipherTypeRSA 继承AbstractJavaSamplerClient存在于ApacheJMeter_java.jar这个JAR包中或实现JavaSamplerClient 接口,两种方式其中一种引用即可调用
4、需要实现四个方法,分别是:
三、直接上具体代码
说明:以下代码已脱敏处理,直接复制是跑不通的,主要是领悟其用法
1 package com.hcb.enterprise;2 import java.util.HashMap;3 import java.util.Iterator;4 import java.util.Map;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 import org.wlqq.codec.enums.CipherType;10 import org.wlqq.codec.exception.CodecHttpException;11 import org.wlqq.codec.http.client.CodecHttpTemplate;12 import org.wlqq.codec.http.client.HttpTemplateBuilder;13 14 public class CipherTypeRSA extends AbstractJavaSamplerClient { 15 private String label;16 private String resultData; 17 private String url;18 private String host;19 private String uri;20 private String appId;21 private String privateKey;22 Map<String, String> map = new HashMap<>();23 private static final CodecHttpTemplate CODEC_HTTP_TEMPLATE = HttpTemplateBuilder.create().build();24 25 public Arguments getDefaultParameters() { 26 Arguments params = new Arguments(); 27 params.addArgument("label","exec name");28 params.addArgument("appId", "open-enterprise"); 29 params.addArgument("privateKey", "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJPq9N65uR+bHxtUKFv4YgSV5Voa6jLKGhYwPGqAItthp"); 30 params.addArgument("host","http://enterprise-api.dev-ag.56qq.com");31 params.addArgument("uri","/server/servicetask/list");32 params.addArgument("content","");33 return params; 34 } 35 36 //每个线程测试前执行一次,做一些初始化工作; 37 public void setupTest(JavaSamplerContext arg0) { 38 Iterator<String> it =arg0.getParameterNamesIterator();39 while(it.hasNext()){40 String key=it.next();41 String value = arg0.getParameter(key);42 if("host".equals(key)){43 host = arg0.getParameter(key);44 }else if("label".equals(key)){45 label = arg0.getParameter(key);46 }else if("uri".equals(key)){47 uri = arg0.getParameter(key);48 }else if("appId".equals(key)){49 appId = arg0.getParameter(key);50 }else if("privateKey".equals(key)){51 privateKey = arg0.getParameter(key);52 }else if("content".equals(key)){53 getContent(arg0.getParameter(key));54 }else{55 map.put(key, value);56 }57 url = host + uri;58 }59 } 60 61 public SampleResult runTest(JavaSamplerContext arg0) { 62 SampleResult sr = new SampleResult(); 63 try { 64 sr.setSampleLabel(label);65 sr.sampleStart();// jmeter 开始统计响应时间标记 66 resultData = doPostSign(url, map, appId, privateKey, CipherType.RSA);67 if (resultData != null && resultData.length() > 0) { 68 sr.setResponseData(resultData, null); //将结果输出到结果树当中69 sr.setDataType(SampleResult.TEXT); //请求头中会多个类型输出 Data type ("text"|"bin"|""): text70 } 71 //System.out.println(resultData); //会输出到控制台,使用Jmeter 会输出到控制台或日志里面72 sr.setSuccessful(true); 73 } catch (Throwable e) { 74 sr.setSuccessful(false); 75 sr.setResponseData("Check the input parameters is correct !",null); 76 e.printStackTrace();77 } finally { 78 sr.sampleEnd(); //Jmeter 结束统计响应时间标记 79 } 80 return sr; 81 } 82 83 public void teardownTest(JavaSamplerContext arg0) { 84 85 } 86 /**87 public static void main(String[] args) { 88 Arguments params = new Arguments(); 89 params.addArgument("appId", "open-enterprise"); 90 params.addArgument("privateKey", "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJPq9N65uR+bHxtUKFv4YgSV5Voa6jLKGhYwPGqAItthp"); 91 params.addArgument("host","http://enterprise-api.dev-ag.com");92 params.addArgument("uri","/server/company/get-company-by-name");93 params.addArgument("content", "fullName=XX科技有限公司");94 JavaSamplerContext arg0 = new JavaSamplerContext(params); 95 CipherTypeRSA test = new CipherTypeRSA(); 96 test.setupTest(arg0); 97 test.runTest(arg0); 98 test.teardownTest(arg0);99 } */ 100 101 private String doPostSign(String url, Map<String, String> params, String appId, String privateKey, 102 CipherType cipherType) throws CodecHttpException { 103 if (params != null) { 104 params.put("__APPID__", appId); 105 } 106 String result = CODEC_HTTP_TEMPLATE.doPostSign(url, params, appId, privateKey, cipherType); 107 return result; 108 } 109 110 // 拼接的字符串转换成键值对存进map 集合中 content = "a=123&b=&c=234"; 111 private void getContent(String content){ 112 if(!content.isEmpty()&&!content.startsWith("&")){ 113 String[] list = content.split("&"); 114 for(String s:list){ 115 Boolean es = s.endsWith("="); 116 if(es){ 117 map.put(s.substring(0,s.length()-1),""); 118 }else{ 119 String[] mapList = s.split("="); 120 map.put(mapList[0],mapList[1]); 121 } 122 } 123 } 124 } 125 }
四、 把Java项目导出成.jar,并放于apache-jmeter-3.0\lib\ext文件夹里面,Java项目lib的依赖包在打成.jar 时生成,所以不用单独处理
导出
五、创建Jmeter java 请求
六、查看响应结果
综上按照以上写个Java请求是没有什么问题的,也没有什么难度,关键的需要准备一个工具类(Java请求加解密、鉴权等的工具类)
补充:封装完成的结果函数
private void setSampleResultFailed(SampleResult sr, int status, String result) {
sr.setSuccessful(false);
sr.setResponseCode(String.valueOf(status));
sr.setResponseData(result, "UTF-8"); //把响应结果返回到jmeter中
sr.setResponseOK();
}
private void setSampleResultSuccess(SampleResult sr, int status, String result) {
sr.setSuccessful(true);
sr.setResponseCode(String.valueOf(status));
sr.setResponseData(result, "UTF-8");
sr.setResponseOK();
}