beanshell使用

 

jmeterBeanShell

  定时器: BeanShell Timer

  前置处理器:BeanShell PreProcessor
  采样器: BeanShell Sampler
  后置处理器:BeanShell PostProcessor
  断言:  BeanShell断言
  监听器: BeanShell Listener

Bean Shell常用内置对象

  log(Logger):打印日志信息,写入信息到jmeber.log文件,
    使用方法,log.info("info")可参考:http://excalibur.apache.org/apidocs/org/apache/log/Logger.html

  ctx(JMeterContext):该变量引用了当前线程的上下文,
    使用方法可参考:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html

  vars(JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:
    1).vars.get(String key):从jmeter中获得变量值
    2). vars.put(String key,String value):数据存到jmeter变量中
    可参考:http://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterVariables.html

  props(JMeterProperties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值。
    1) props.get("START.HMS");  注:START.HMS为属性名,在文件jmeter.properties中定义 
    2) props.put("PROP1","1234");

  prev(SampleResult):获取前面的sample返回的信息,常用方法:
    1) getResponseDataAsString():获取响应信息
    2 getResponseCode() :获取响应code
    可参考:http://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html

  sampler(Sampler):gives access to the current sampler
  可参考:http://jmeter.apache.org/api/org/apache/jmeter/samplers/Sampler.html

  threadName - String containing the current thread name

应用实例:

   有一个socket接口的性能测试,服务器启动了多个端口。由于需要做链路鉴权,所以要保证每一个jmeter线程始和固定的端口通讯

 

 

每个线程都有一个线程号,可以考虑通过线程号关联固定端口。

 

 

//线程号取模
int tmp= ctx.getThreadNum() % 5;
//每个线程对应的端口
tmp = tmp + 9000;
//将端口号保存到变量port
vars.put("port", tmp.toString());

TCP取样器配置的端口号就可以通过${port}进行使用了:

 

 

 

 http://jmeter.apache.org/usermanual/functions.html#__BeanShell

 

转载于:https://www.cnblogs.com/talkhello/p/6676672.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值