在metershere使用中可能会需要一些自定义的变量,这个时候系统自带的几种变量提取方式就无法使用了,需要借助beanshell自定义脚本,来获取想要的变量,
1,首先上传jar包
因为metershere是基于jmeter写的工具,本身的jar包比较少,假如我们需要处理接口返回的JSON数据,就需要手动上传jar包,来引用他,如图。
然后通过接口返回数据,设立变量提取想要数据,代码如下
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.*;
import org.apache.commons.text.StringEscapeUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.http.entity.StringEntity;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import java.util.*;
import java.lang.Integer;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
// 创建Httpclient对象
CloseableHttpClient httpclient = HttpClients.createDefault();
// 参数
String payload = "";
// 定义请求的参数
URI uri = new URIBuilder()
.setScheme("http")
.setHost("192.168.1.0:88")
.setPath("路径/porain/finaCen/sumType")
// .setPort() // int类型端口
.build();
// 创建http请求
HttpGet request = new HttpGet(uri);
request.setHeader("Content-type", "application/json");
request.setHeader("Authorization", " eyJhbGciOiJIUzUxMiJ9.eyJ1c2Vy");
//response 打印请求地址
log.info("************************************" + uri.toString());
//response 对象
CloseableHttpResponse response = httpclient.execute(request);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(), "UTF-8");
log.info("...-----------------------------" + content); //答应接口响应数据
JSONArray jsonArray = JSON.parseObject(content).getJSONArray("data");
log.info("========================================="+jsonArray);
log.info("............................1111..............." + jsonArray.get(1).get("sumNum"));
log.info(".............................2222.............." + jsonArray.get(3).get("sumNum"));
// 成本的提成之和
double costNum = Double.parseDouble(jsonArray.get(1).get("sumNum").toString()) + Double.parseDouble(jsonArray.get(3).get("sumNum").toString());
log.info(".............................333333.............." + costNum);
// 在接下的接口中就可以直接使用变量 costNum
}
响应数据
2023-12-07 15:26:35 INFO StandardJMeterEngine Running the test!
2023-12-07 15:26:35 INFO StandardJMeterEngine List of sample_variables: []
2023-12-07 15:26:35 INFO StandardJMeterEngine Starting ThreadGroup: 1 : ThreadGroup
2023-12-07 15:26:35 INFO StandardJMeterEngine Starting 1 threads for group ThreadGroup.
2023-12-07 15:26:35 INFO StandardJMeterEngine Thread will continue on error
2023-12-07 15:26:35 INFO StandardJMeterEngine Starting thread group... number=1 threads=1 ramp-up=1 delayedStart=false
2023-12-07 15:26:35 INFO StandardJMeterEngine Started thread group number 1
2023-12-07 15:26:35 INFO StandardJMeterEngine All thread groups have been started
2023-12-07 15:26:35 INFO ThreadGroup 1-1 Thread started: ThreadGroup 1-1
2023-12-07 15:26:35 INFO ThreadGroup 1-1 ************************************http://192.168.1.250:88/dev-api/poradmin/financeCenter/sumByType
2023-12-07 15:26:35 INFO ThreadGroup 1-1 ...-----------------------------{"msg":"操作成功","code":200,"data":[,{"brsType":109,"sumNum":104400.0},{"brsType":110,"sumNum":417600.0},{"brsType":209,"sumNum":23.1},{"brsType":210,"sumNum":46.2}]}
2023-12-07 15:26:36 INFO ThreadGroup 1-1 =========================================[{"brsType":109,"sumNum":104400.0},{"brsType":110,"sumNum":417600.0}{"brsType":209,"sumNum":23.1},{"brsType":210,"sumNum":46.2}]
2023-12-07 15:26:36 INFO ThreadGroup 1-1 ............................1111...............417600.0
2023-12-07 15:26:36 INFO ThreadGroup 1-1 .............................2222..............46.2
2023-12-07 15:26:36 INFO ThreadGroup 1-1 .............................333333..............417646.2