JMeter遇到全局变量、BeanShell Sampler、JDBC(postgresql)

全局变量、BeanShell Sampler、JDBC(postgresql)

设置全局变量

场景:在压测指定接口时需要先进行登录才能去访问接口,解决方案是先设置一个登录线程,登录成功后通过JSON Extractor提取出token,再通过BeanShell Sampler将环境变量设置成全局变量,使用函数为 __setProperty,例如:${__setProperty(access_token, ${access_token})};
image.png
image.png
读取全局变量,使用函数__property,例如 ${__property(access_token)}
image.png

BeanShell Sampler

写了一段BeanShell脚本,脚本中主要有一些功能

  1. 指定几个变量,每次循环随机获取其中一个
  2. 获取当前时间并转换成指定格式(最后发现其实jmeter中存在响应的函数__timeShift
  3. 将BeanShell中变量设置成环境变量
import java.time.*;
import java.time.format.*;


//Integer a = (Integer)(Math.random() * 750) % 750 + 1;
//String b = String.format("AP1013%04d", a);
//vars.put("mp_code", b);
//
//
//int c = (int)(Math.random() * 150) % 150 + 1;
//String d = String.format("TP1013%04d", c);
//vars.put("t_mp_code", d);


//指定几个变量,每次随机取值
String[] keys = {"equivalentPeak","validValue","equivalentPpValue", "envTruePpValue"};
int k = (int) (Math.random() * 4) % 4 ;
vars.put("key", keys[k]);

String[] data_cate = {"acceleration","speed","displacement"};
int l = (int) (Math.random() * 3) % 3 ;
vars.put("data_cate", data_cate[l]);

// 获取当前时间并转换成指定格式
LocalDateTime time1 = LocalDateTime.now();
LocalDateTime time2 = time1.minusDays(1);
String now_time = time1.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
String start_time = time2.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));

//设置成环境变量
vars.put("start_time", start_time);
vars.put("now_time", now_time);

JDBC(postgresql)

首先是驱动,在网上找到的官网地址地不开,其它路径找了一个,版本42.5.0

链接:https://pan.baidu.com/s/1rE6o1B9CEenGcNTlKuPWIg?pwd=gvd1 
提取码:gvd1

将下载后将jar文件放入到jmeter安装目录下的lib文件夹下,重启启动下jmeter
使用起来还是很方便的,和mysql基本一样

添加 JDBC Connection Configuration

image.png

添加 JDBC Request

image.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mrlijie00

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值