A, the Jmeter complete a java request implementation
Implemented in two ways:
Achieve JavaSamplerClient Interface
Abstract class inherits AbstractJavaSamplerClient
Second, the use AbstractJavaSamplerClient abstract class to write java program
1 , a key step in
1) create a Maven project;
2) to confirm local Maven library path, add pom.xml content, prepared pom-dependent coordinates;
3) Maven project compiling and debugging success;
2) local Maven repository path confirmation, add pom.xml content, pom coordinate the preparation of the JMeter;
3) Create a class and implement JavaSamplerClient interface or inherit AbstractJavaSamplerClient, and rewrite;
the Arguments public getDefaultParameters (); // set the available parameters and their default values, parameters need to pass in the jmeter, parameterize, using this method it is necessary to pass parameters
void public SetupTest (JavaSamplerContext arg0) // each thread start, deal needs to be done , the code implementation for business or to Call
SampleResult public the runTest (JavaSamplerContext arg0) // method body , start the test, parameter values can be obtained from the parameter arg0
void public TeardownTest (JavaSamplerContext arg0) // at the end of the test at the end of call processing needs to be done ;
4) Export to Runnable Jar File; - is preferably derived using eclipse, ideal method of deriving see
5) This jar package into JMETER_HOME \ lib \ ext directory;
6) Administrator, open JMeter;
7) Create a thread group, Java Request, view the results tree, testing;
2, the use of inheritance-based manner AbstractJavaSamplerClient request class java
As shown, four methods need to be rewritten
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
@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; } |
In jmeter JAVA request requires the parameterization, the parameter passing method using getDefaultParameters:
1 2 3 4 5 6 7 8 |
// This method is a method to customize the parameters of java. // params.addArgument ( "getJsonData", "$ {getJsonData}"); parameter represents the name getJsonData, the default value is $ {getJsonData}. //设置可用参数及它的默认值; public Arguments getDefaultParameters() { Arguments params = new Arguments(); params.addArgument("getJsonData", "${getJsonData}");//表示入参名字叫getJsonData,默认值为${getJsonData} return params; } |
上述代码实现左侧是代码中参数获取,右侧是默认值,取jmeter页面CSV输入参数值。
runTest中进行业务的代码实现或者调用方法实现:
1)实现业务http接口-待更新
2)实现api接口
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
//实例化SampleResult,用来记录运行结果 SampleResult result = new SampleResult();
//获取用户输入的变量值 String jsonDataStr = javaSamplerContext.getParameter("jsonDataStr", ""); String temp; logger.info("jsonDataStr : ---->" + jsonDataStr); System.out.println("jsonDataStr : ---->" + jsonDataStr);
boolean isSuc = false;
try { result.sampleStart();//定义事务的开始 FreightRequest freightRequest = JSONObject.parseObject(jsonDataStr, FreightRequest.class); logger.info("freightRequest : ---->" + JSON.toJSONString(freightRequest)); System.out.println("freightRequest : ---->" + JSON.toJSONString(freightRequest)); result.setSampleLabel(getClass().getName());//设置Java Sampler标签
RemoteFtResult<FreightResponse> eResult = freightCalculateService.doFreightCalculate(freightRequest); temp = JSON.toJSONString(eResult); logger.info("freightResponse : ---->" + JSON.toJSONString(eResult)); System.out.println("freightResponse : ---->" + JSON.toJSONString(eResult)); result.setSamplerData(jsonDataStr.toString());//设置请求参数 result.setResponseData(temp.toString(),"utf-8"); result.setDataType(SampleResult.TEXT);//设置数据类型
if (!eResult.isSuccess() && eResult.getErrorCode() == "-99999") { //根据接口响应和脚本目的来判断该脚本成功或失败的条件,此处为判断压测接口是否有效,判定本次请求未成功及错误code为系统错误为无效数据 isSuc = false; logger.error("message :---> false"); result.setResponseCode("-99999"); result.setResponseCodeOK();//设置响应成功
}else { isSuc = true; logger.info("message :---> true"); result.setResponseCode("200"); result.setResponseCodeOK();//设置响应成功 }
} catch (Exception e) { isSuc = false; logger.error("e.message"+e.getMessage()+"e"+e+"e.toString"+e.toString()); result.setSuccessful(false);
} finally { result.setSuccessful(isSuc); result.setSuccessful(true);//设置事务结束 result.sampleEnd();// jmeter 结束统计响应时间标记 System.out.println("[Test Case] 接口 " + getClass().getName() + " 测试完毕."); } return result;
} |
3.代码写好后,将代码进行打包操作,输出jar包,idea导包步骤见下:
(eclipse具体操作见文档 “eclipse导出可执行jar包操作”)
1)右键点击工程,选择open Module Settings或点击File选择Project Structure,进入页面
2)选择Artifacts->JAR->From modules with dependencies
3)弹出对话框
未避免出现“错误: 找不到或无法加载主类 com.jd.freight.dofreight.doFreight”,请按下图步骤打包:
即:
点击OK,页面显示jar包的输出路径。点击OK,配置完成:
4)在idea菜单栏,点击Build->Build Artifacts
->
5)到指定路径 即可获取jar包
打出的包结构为:
4.jmeter添加java请求
将打好的jar包及依赖包目录整体导入jmeter目录/lib/ext下,进入jmeter指定jar包路径:
创建线程组->创建Java请求->在jira请求中选择类名称,如图,添加参数->生成脚本
Jmeter察看结果树如下:
------------------------------------------------------Tanwheey--------------------------------------------------
爱生活,爱工作。