1.jmeter测试脚本,有两种方式。
- 实现JavaSamplerClient接口
- 继承AbstractJavaSamplerClient抽象类
本次使用实现JavaSamplerClient接口方式
2.如图,需要重写四个方法
@Override public void setupTest(JavaSamplerContext javaSamplerContext) { } @Override public SampleResult runTest(JavaSamplerContext javaSamplerContext) { return null; } @Override public void teardownTest(JavaSamplerContext javaSamplerContext) { } @Override public Arguments getDefaultParameters() { return null; }
setupTest 启动时,需要做的处理
getDefaultParameters 传参
runTest方法体
teardownTest结束时,需要做的处理
3.因为需要在jmeter中进行参数化,所以需要getDefaultParameters 方法进行传参
// 这个方法是用来自定义java方法入参的。 // params.addArgument("applyAmount", "966100000000");表示入参名字叫applyAmount,默认值为966100000000。 //设置可用参数及它的默认值; public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("applyAmount", "966100000000"); return params; }
左侧是代码中参数获取,右侧是jmeter页面输入参数
4.runTest中进行业务的代码实现或者调用实现方法即可
//开始测试,从javaSamplerContext参数可以获得参数值; public SampleResult runTest(JavaSamplerContext javaSamplerContext) { String applyAmount = javaSamplerContext.getParameter("applyAmount"); String url = "http://172.28.38.81:40505/czCredit/v1/repaychargeplan/repayTrial"; System.out.println("[Test Case] url = " + url); // 必填 String productCode="A3"; //产品编码 信贷核心表product_term.loan_product_code String applyDate="2018-08-13"; //申请日期 //选填 String buyInsurance="1"; //是否购买保险 1是 0否 String channelCode="O20170809100414"; //渠道编码 Map<String,Object> paraMap = new HashMap<String, Object>(); paraMap.put("productCode",productCode); paraMap.put("applyDate",applyDate); paraMap.put("applyAmount",applyAmount); paraMap.put("buyInsurance",buyInsurance); paraMap.put("channelCode",channelCode); System.out.println("[request Data]请求参数 paraMap = " + JSONObject.toJSONString(paraMap)); Map<String, String> headers = new HashMap<String, String>(); headers.put("appId", "100"); headers.put("t",t); headers.put("X-Real-IP","127.0.0.1"); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); // 构造 Header Iterator i$ = headers.keySet().iterator(); while (i$.hasNext()) { String header = String.valueOf(i$.next()); httpPost.setHeader(header, String.valueOf(headers.get(header))); } // 构造 Body Iterator t$ = paraMap.keySet().iterator(); while (t$.hasNext()) { String body = String.valueOf(t$.next()); httpPost.setHeader(body, String.valueOf(paraMap.get(body))); } String signBodyData = HttpUtils.getSignMap(t, paraMap); StringEntity requestEntity = new StringEntity(signBodyData, Consts.UTF_8); httpPost.setEntity(requestEntity); CloseableHttpResponse httpResponse = null; String responseString = null; String responseMessage = null; String status = null; String info = null; String result = null; SampleResult sampleResult = new SampleResult(); try { httpResponse = httpClient.execute(httpPost); HttpEntity responseEntity = httpResponse.getEntity(); if (responseEntity != null && SC_OK == httpResponse.getStatusLine().getStatusCode()) { responseString = EntityUtils.toString(responseEntity, Charset.forName("UTF-8")); System.out.println("[Test Case] responseString = " + responseString); responseMessage = String.valueOf(JSONPath.eval(JSON.parseObject(responseString),"$message")); System.out.println("[Test Case] responseMessage = " + responseMessage); info = String.valueOf(JSONPath.eval(JSON.parseObject(responseString),"$info")); System.out.println("[Test Case] responseMessage = " + info); status = String.valueOf(JSONPath.eval(JSON.parseObject(responseString),"$status")); System.out.println("[Test Case] status = " + status); result = String.valueOf(JSONPath.eval(JSON.parseObject(responseString),"$result")); System.out.println("[Test Case] result = " + result); } EntityUtils.consume(requestEntity); EntityUtils.consume(responseEntity); httpResponse.close(); httpClient.close(); sampleResult.setSampleLabel(getClass().getName()); //设置标签 sampleResult.sampleStart(); sampleResult.setResponseCode(String.valueOf(httpResponse.getStatusLine().getStatusCode())); //设置响应码 sampleResult.setResponseMessage(responseMessage); //设置响应信息 sampleResult.setResponseData(responseString,"utf-8");//设置响应数据 sampleResult.setSamplerData(paraMap.toString());//设置请求参数 sampleResult.setDataType(SampleResult.TEXT);//设置数据类型 if("0000".equals(status) && "success".equals(info) && null != result){ sampleResult.setSuccessful(true);//设置结果成功 }else{ sampleResult.setSuccessful(false);//设置结果成功 } }catch (Exception e){ sampleResult.setSuccessful(false); sampleResult.setResponseMessage(e.toString()); e.printStackTrace(); }finally { sampleResult.sampleEnd(); System.out.println("[Test Case] 接口 " + getClass().getName() + " 测试完毕."); } return sampleResult; }
5.代码写好后,将代码进行打包操作,输出jar包,具体操作如下:
1)右键点击工程,选择open Module Settings,进入页面
2)选择Artifacts->JAR->From modules with dependencies
3)弹出对话框,点击OK,页面显示jar包的输出路径。点击OK,配置完成
4)在idea菜单栏,点击Build->BuildArtifacts->
5)到指定路径 即可获取jar包
6)jmeter添加java请求
给jmeter指定jar包路径:进入jmeter/bin下,找到jmeter.properties文件,修改search_paths参数,参数值为jar包所在路径,可以配置多个,用;隔开,如图
创建线程组->创建Java请求->在jira请求中选择类名称,如图,添加参数->生成脚本
7)linux上执行脚本
a.linux上安装jmeter
将windows上的zip包直接放到linux上
进入bin目录,chmod 777 jmeter
修改环境变量:
# vim /etc/profile export PATH=/opt/apache-jmeter-3.3/bin/:$JAVA_HOME/bin:$PHP_HOME/bin:$PATH # source /etc/profile 使改动生效
b.在linux的jmeter中导入jar包,执行命令:
脚本执行命令:jmeter -n -t /opt/jmx/push5000.jmx -l log.jtl
-n 非 GUI 模式 -> 在非 GUI 模式下运行 JMeter -t 测试文件 -> 要运行的 JMeter 测试脚本文件
-l 日志文件 -> 记录结果的文件
注意: jmeter需要jdk1.8以上版本,否则执行脚本命令时,会报错: Error: Java version is too low to run JMeter. Needs at least Java >= 1.8.0. 解决办法: 升级即可