在MeterSphere中简单使用beanshell语言

本文介绍了如何在metershere工具中,当系统自带变量提取方式不足时,通过Beanshell自定义脚本处理接口返回的JSON数据,包括上传jar包、发送HTTP请求并解析JSON内容以获取和使用变量
摘要由CSDN通过智能技术生成

在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



    
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值