1、背景
考虑到jmeter在编写groovy及beanshell脚本时,编辑器工具没有命令行提示的功能,因此特写本文章,配置IDE工具。
2、使用工具说明
- 开发编辑器IntelliJ IDEA 2022.3.1
- Jmeter5.6.3
3、配置步骤
3.1 下载jmeter源码
https://jmeter.apache.org/download_jmeter.cgi
3.2 build源码
#解压缩源码
unzip apache-jmeter-5.6.5_src.zip
#进入解压后的目录,执行编译
./gradlew build --no-verify
#如build过程中报错,请执行如下命令
./gradlew autostyleApply
3.3 idea打开
用idea工具直接打开源码所在目录即可。
4、测试
在源码的src/core/jmh/main/java/util目录下新建一个自己的测试类即可。
关键点:方法的参数中有如下参数,分别对应beanshell中的内置参数。-vars/ctx/props/log/sampler
JMeterContext ctx, JMeterVariables vars, Properties props,
String sr,int ResponseCode,String ResponseMessage,boolean IsSuccess,String Label, Logger log, HTTPSamplerBase sampler
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to you under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jmeter.util;
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerBase;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.threads.JMeterContext;
import org.apache.jmeter.threads.JMeterVariables;
import org.gradle.internal.impldep.com.google.gson.Gson;
import org.slf4j.Logger;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import org.apache.jmeter.protocol.http.control.HeaderManager;
import org.slf4j.Logger;
public class test_yu {
private void mBeenShellScript(JMeterContext ctx, JMeterVariables vars, Properties props,
String sr,int ResponseCode,String ResponseMessage,boolean IsSuccess,String Label, Logger log, HTTPSamplerBase sampler){
String marketPrice_ALL = vars.get("marketPrice_ALL");
String outerSkuCode_ALL = vars.get("outerSkuCode_ALL");
String sellPrice_ALL = vars.get("sellPrice_ALL");
String sellableNum_ALL = vars.get("sellableNum_ALL");
String[] marketPriceArr= marketPrice_ALL.split(",");
String[] outerSkuCodeArr = outerSkuCode_ALL.split(",");
String[] sellPriceArr = sellPrice_ALL.split(",");
String[] sellableNumArr = sellableNum_ALL.split(",");
String[] skus = new String[marketPriceArr.length];
for (int i = 0; i < marketPriceArr.length; i++) {
Map map = new HashMap();
map.put("outerSkuCode", outerSkuCodeArr[i]);
map.put("sellPrice", sellPriceArr[i]);
map.put("marketPrice", marketPriceArr[i]);
map.put("sellableNum", sellableNumArr[i]);
log.info(marketPriceArr[i]);
log.info(outerSkuCodeArr[i]);
log.info(sellPriceArr[i]);
log.info(sellableNumArr[i]);
String jsonStr = new Gson().toJson(map);
skus[i]=jsonStr;
}
vars.put("skus", Arrays.toString(skus));
String b = ctx.getThread().getThreadName();
SampleResult rsp = ctx.getPreviousResult();
String rh = rsp.getResponseDataAsString();
log.info('\n'+rh);
String cname = ctx.getCurrentSampler().getName();
log.info("当前的cname是: " + cname);
// 获得请求头信息
HeaderManager headers = sampler.getHeaderManager();
// 打印删除指定请求头参数前的全部请求头信息
log.info("删除前" + headers.getHeaders().getStringValue());
// 删除请求头指定的信息
sampler.getHeaderManager().removeHeaderNamed("Content-Type");
boolean isRunningVersion = sampler.getHeaderManager().isRunningVersion();
// 打印删除指定请求头参数后的全部请求头信息
log.info("======="+String.valueOf(isRunningVersion));
log.info("删除后" + headers.getHeaders().getStringValue());
log.info(sampler.getPath());
log.info(sampler.getMethod());
log.info(sampler.getContentEncoding());
log.info(sampler.getDomain());
log.info(sampler.getEmbeddedUrlRE());
log.info(sampler.getQueryString());
log.info(sampler.getConcurrentPool());
log.info(sampler.getIpSource());
log.info(sampler.getProtocol());
log.info(sampler.getProxyHost());
log.info(sampler.getProxyPass());
log.info(sampler.getProxyScheme());
try {
log.info(String.valueOf(sampler.getUrl()));
} catch (MalformedURLException e) {
throw new RuntimeException(e);
}
SampleResult prev = ctx.getPreviousResult();
String sc = prev.getResponseCode(); // 响应状态码
boolean yn = prev.isResponseCodeOK(); // 响应状态码是否为200 返回True或者False
String tname = prev.getThreadName(); // 当前的线程名
String ct = prev.getContentType(); // 连接类型
long sb = prev.getSentBytes(); // 请求报文大小
long rb = prev.getBytesAsLong(); // 响应报文大小
long db = prev.getLatency(); // 延迟时间
long cont = prev.getConnectTime(); // 连接时间
URL url = prev.getURL(); // 取样器的URL
long rsb = prev.getBodySizeAsLong(); // 响应正文大小
log.info("响应状态码:" + sc);
log.info("响应状态码是否为200: " + yn);
log.info("线程名: " + tname);
log.info("连接类型:" + ct);
log.info("请求报文大小: " + sb);
log.info("响应报文大小: " +rb);
log.info("延迟时间: " + db);
log.info("连接时间: "+ cont);
log.info("取样器的URL: " + url);
log.info("响应正文大小: " + rsb);
}
}
后续只要在idea中编写完成之后,把方法中的内容复制到jmeter的beanshell中执行即可。
标签:info,jmeter,String,编辑器,Beanshell,sampler,import,Jmeter,log From: https://blog.csdn.net/demonlamei/article/details/136895269