赞
踩
创建一个maven项目, 引入依赖
- <dependency>
- <groupId>org.apache.jmeter</groupId>
- <artifactId>ApacheJMeter_core</artifactId>
- <version>3.2</version>
- </dependency>
- <dependency>
- <groupId>org.apache.jmeter</groupId>
- <artifactId>ApacheJMeter_java</artifactId>
- <version>3.2</version>
- </dependency>
-
- // 用于函数助手开发
- <dependency>
- <groupId>org.apache.jmeter</groupId>
- <artifactId>ApacheJMeter_functions</artifactId>
- <version>3.2</version>
- </dependency>
若引入依赖失败,找到仓库内的文件,删除,reimport一下依赖即可
默认路径 /Users/lvxueqing/.m2/repository/org/apache/jmeter
- public class First extends AbstractJavaSamplerClient {
- public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
- System.out.println(" 我的demo!!!!");
- return null;
- }
- }
使用方式:
1)到idea终端 打包 mvn clean package
2)将打包后生成的文件,放到jmeter的/apache-jmeter-5.2.1/lib/ext路径下
3)重启jemter
4)添加线程组,添加java请求,可看到刚刚写好的类
5)点击运行 日志打印了
- public class Second implements JavaSamplerClient {
- private static final String URLNAME="URL"; //jmeter属性中显示的变量名称
- private static final String DEFAULTURL="http://www.baidu.com"; //jmeter属性中显示的变量值
- private String resultData; // 存储响应数据,将结果放到查看结果树里
-
- /**
- * 决定了jmeter中要展示哪些属性, 第一个运行
- */
- public Arguments getDefaultParameters() {
- System.out.println("getDefaultParameters run!! ");
- Arguments arguments = new Arguments();
- arguments.addArgument(URLNAME,DEFAULTURL);
- return arguments;
- }
-
- //接收用户输入的URL
- private String inputUrl ;
- /**
- * 初始化的方法
- */
- public void setupTest(JavaSamplerContext javaSamplerContext) {
- System.out.println("setupTest run!! ");
-
- inputUrl = javaSamplerContext.getParameter(URLNAME,DEFAULTURL);
- System.out.println("用户输入的url地址是 " + inputUrl);
-
- }
-
- /**
- * 具体功能逻辑的方法
- */
- public SampleResult runTest(JavaSamplerContext javaSamplerContext) {
- System.out.println("runTest run!! ");
- SampleResult sampleResult = new SampleResult();
- StringBuilder sb = new StringBuilder();
-
- try {
- URL url = new URL(inputUrl);
- URLConnection conn = url.openConnection();
- byte[] buffer = new byte[1024];
- int len;
- sampleResult.sampleStart(); // 标记事务开始
- InputStream in = conn.getInputStream();
- while ((len=in.read(buffer)) != -1){
- resultData = new String(buffer,"UTF-8");
- sb.append(resultData);
-
- }
-
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- // resultData = "这就是响应结果";
- resultData = sb.toString();
- sampleResult.setSuccessful(true); // 告诉查看结果树 请求是否成功
- sampleResult.setSampleLabel("自定义java访问请求");
- sampleResult.setResponseData(resultData,"UTF-8");
- sampleResult.setDataType(SampleResult.TEXT);
-
- return sampleResult;
- }
-
- /**
- * 做一些收尾工作的方法
- */
- public void teardownTest(JavaSamplerContext javaSamplerContext) {
- System.out.println("teardownTest run!! ");
-
- }
-
-
- }
结果展示
注意: 函数相关类要放到functions目录下
- public class Plus extends AbstractFunction {
- private Object[] values;
- private CompoundVariable first,second; // 存储第一个和第二个参数
-
- /**
- * 执行方法
- */
- public String execute(SampleResult sampleResult, Sampler sampler) throws InvalidVariableException {
- System.out.println(" execute run ");
-
- first = (CompoundVariable) values[0];
- System.out.println("第一个参数是 " + first);
- second = (CompoundVariable) values[1];
- System.out.println("第二个参数是 " + second);
- int count = new Integer(first.execute().trim()) + new Integer(second.execute().trim()) ;
- System.out.println("和 " + count);
-
- return String.valueOf(count);
- }
-
- /**
- * 设置参数, 接收用户传递的参数
- */
- public void setParameters(Collection<CompoundVariable> collection) throws InvalidVariableException {
- System.out.println(" setParameters run ");
- checkParameterCount(collection,2);
- values = collection.toArray();
- }
-
- /**
- * 功能名称
- */
- public String getReferenceKey() {
- System.out.println(" getReferenceKey run ");
- return "__MyDemoPlus";
- }
-
- /**
- * 功能描述 参数描述
- */
- public List<String> getArgumentDesc() {
- System.out.println(" getArgumentDesc run ");
- List desc = new ArrayList();
- desc.add("第一个数");
- desc.add("第二个数");
- return desc;
- }
- }
使用方式:
1)到idea终端 打包 mvn clean package
2) 将打包后生成的jar文件,放到jmeter的/apache-jmeter-5.2.1/lib/ext路径下
3)重启jemter
结果展示
结果可展示在日志中
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。