https://wangym.iteye.com/blog/731729
http://www.cnblogs.com/yangxia-test/p/4019541.html
https://blog.csdn.net/qq_34021712/article/details/78870407
http://www.51testing.com/html/82/n-854182-2.html
https://www.cnblogs.com/zhaoxd07/p/4895224.html
--添包和打包
https://blog.csdn.net/qq_34021712/article/details/78871739
https://www.cnblogs.com/yulia/p/6824058.html
1、核心流程
1.创建一个Java工程;
2.将JMeter的lib目录下的jar文件添加进此工程的Build Path;
3.创建一个类并实现JavaSamplerClient接口或继承AbstractJavaSamplerClient,并重写
public Arguments getDefaultParameters();设置可用参数及的默认值;
public void setupTest(JavaSamplerContext arg0):每个线程测试前执行一次,做一些初始化工作;
public SampleResult runTest(JavaSamplerContext arg0):开始测试,从arg0参数可以获得参数值;
public void teardownTest(JavaSamplerContext arg0):测试结束时调用;
4.Export为Runnable Jar File;
5.将此jar包放入JMETER_HOME\lib\ext目录;
6.以管理员身份打开JMeter;
7.创建线程组、Java Request、查看结果树,进行测试
package org.test; public class Hello { public String sayHello(){
return "hello";
} public String sayHelloToPerson(String s){
if (s == null || s.equals(""))
s="nobady";
return (new StringBuffer()).append("hello").append(s).toString();
} public int sum (int a ,int b ){
return a+b;
}
}
package org.test; 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 perftest extends AbstractJavaSamplerClient {
private String a;
private String b;
private String resultData; private static long start = 0;
private static long end = 0;
// JMeter测试用例入口
@Override
public SampleResult runTest(JavaSamplerContext arg0) {
a=arg0.getParameter("num1");
b=arg0.getParameter("num2");
SampleResult sr=new SampleResult();
sr.setSampleLabel("java请求哦");
try{
sr.sampleStart();//jmeter开始统计响应时间标记 Hello test=new Hello();
// 通过下面的操作就可以将被测方法的响应输出到Jmeter的察看结果树中的响应数据里面了。
resultData=String.valueOf(test.sum(Integer.parseInt(a),Integer.parseInt(b)));
if (resultData != null && resultData.length() > 0){ sr.setResponseData("结果是"+resultData,null);
sr.setDataType(SampleResult.TEXT); }
sr.setSuccessful(true); }catch (Throwable e){ sr.setSuccessful(false );
e.printStackTrace(); }
finally {
sr.sampleEnd();// jmeter 结束统计响应时间标记 }
return null;
}
// 这个方法是用来自定义java方法入参的。
// params.addArgument("num1","");表示入参名字叫num1,默认值为空。
//JMeter界面中可手工输入参数,代码里面通过此方法获取
public Arguments getDefaultParameters() { Arguments params = new Arguments();
params.addArgument("num1","");//设置参数,并赋予默认值0
params.addArgument("num2", "");//设置参数,并赋予默认值0 return params;
}
//执行runTest()方法前会调用此方法,可放一些初始化代码
public void setupTest(JavaSamplerContext arg0){ // 开始时间
start = System.currentTimeMillis(); }
//执行runTest()方法后会调用此方法.
public void teardownTest(JavaSamplerContext arg0) {
//结束时间
end = System.currentTimeMillis();
// 总体耗时
System.err.println("cost time:" + (end - start) / 1000);
} /* public static void main(String[] args) {
Arguments params =new Arguments();
params.addArgument("num1","1");
params.addArgument("num2","2");
JavaSamplerContext arg0 = new JavaSamplerContext(params);
perftest test = new perftest();
test.setupTest(arg0);
test.runTest(arg0);
test.teardownTest(arg0); }*/
}