1,引用java源文件:
source("java代码路径");
然后new一个源文件中的class进行方法的调用。
2,引用class文件:
addClassPath("class文件所在目录");
然后再import里面的类及方法即可;
3,引用jar包:
在“测试计划”中添加jar包文件路径,或者直接将jar包放在jmeter目录的lib/ext下,
然后直接引用jar包里的包及类方法即可;
示例(jar包方法):
java源文件如下:
package com.Header;
import java.util.*;
public class Sign {
public static Map<String, String> getSign1(){
Map<String, String> m = new HashMap<>();
m.put("sign1", "this is sign1");
m.put("sign2", "this is sign2");
m.put("sign3", "this is sign3");
return m;
}
public static ArrayList<String> getSign2(){
ArrayList<String> mylist = new ArrayList<>();
mylist.add("sign1");
mylist.add("sign2");
mylist.add("sign3");
return mylist;
}
public static void main(String []args){
Map<String, String> s = Sign.getSign1();
System.out.println(s.get("sign1"));
ArrayList<String> m = Sign.getSign2();
System.out.println(m.get(2));
}
}
步骤1::在idea中的file->Project Structure->Artifacts->点击“+”->选择JAR->“From modules with dependencies...”->选择Main Class->确定;
步骤2:点击Build->Build artifacts->选择build所用的类导出jar包;
步骤3:将jar包放到jmeter的lib/ext目录;
步骤4:添加线程组、http请求组件、响应断言、聚合报告、查看结果树等(略);
步骤5:添加Pre Processer->BeanShell PreProcessor;
在BeanShell中写入以下代码:
import java.util.*;
import com.Header.*;
public static String getTime(){
Long t = new Date().getTime();
return t.toString();
}
Map map_args = Sign.getSign1();
ArrayList list_args = Sign.getSign2();
vars.put("k_arg", map_args.get("sign1"));
vars.put("l_arg", list_args.get(2));
vars.put("_t", getTime());
步骤6:在配置请求参数中的url为http://www.baidu.com?msgid=${__Random(111111,999999,)}&karg=${k_arg}&larg=${l_arg}&_t=${_t};
发送请求即可,测试结果图如下:
PS:
1,关于老版本使用Map及ArrayList参数化报错问题,以上代码在老版本jmeter中报如下错误:
java.net.URISyntaxException: Illegal character in query at index 40: http://www.baidu.com?msgid=335822&karg=${k_arg}&larg=${l_arg}&_t=${_t}
at java.net.URI$Parser.fail(Unknown Source)
at java.net.URI$Parser.checkChars(Unknown Source)
at java.net.URI$Parser.parseHierarchical(Unknown Source)
at java.net.URI$Parser.parse(Unknown Source)
at java.net.URI.<init>(Unknown Source)
at java.net.URL.toURI(Unknown Source)
at org.apache.jmeter.protocol.http.sampler.HTTPHC4Impl.sample(HTTPHC4Impl.java:286)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy.sample(HTTPSamplerProxy.java:74)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1146)
at org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase.sample(HTTPSamplerBase.java:1135)
at org.apache.jmeter.threads.JMeterThread.process_sampler(JMeterThread.java:434)
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:261)
at java.lang.Thread.run(Unknown Source)
貌似老版本对这两种类型的数据不太兼容,升级到最新版本后错误消失,貌似跟什么泛型支持有关系,java新手不太懂。。。。、
参考网址:
1,https://www.cnblogs.com/puresoul/articles/4949889.html
2,http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler