zoukankan      html  css  js  c++  java
  • Jmeter之Bean shell使用(一)

    一、什么是Bean Shell

    • BeanShell是一种完全符合Java语法规范的脚本语言,并且又拥有自己的一些语法和方法;
    • BeanShell是一种松散类型的脚本语言(这点和JS类似);
    • BeanShell是用Java写成的,一个小型的、免费的、可以下载的、嵌入式的Java源代码解释器,具有对象脚本语言特性,非常精简的解释器jar文件大小为175k。
    • BeanShell执行标准Java语句和表达式,另外包括一些脚本命令和语法。

    官网:http://www.BeanShell.org/

    二、Jmeter有哪些Bean Shell

    • 定时器:  BeanShell Timer

    • 前置处理器:BeanShell PreProcessor

    • 采样器:  BeanShell Sampler

    • 后置处理器:BeanShell PostProcessor

    • 断言:   BeanShell断言

    • 监听器:  BeanShell Listener

    三、BeanShell的用法

       在此介绍下BeanShell PreProcessor的用法,其它的beahshell可以类推。在此我们使用beahshell调用自己写的工具类,工具类实现了密码的加、解密功能:

    1、在eclipse写好代码,然后把该类打成jar包(在类上点击右键->Export->jar file)

    2、把jar包放到jmeter目录apache-jmeter-2.13libext下

    3、打开jmeter,添加一个http sampler(调用登录接口),在sampler下添加一个BeanShell PreProcessor

    4、在beanshell PreProcessor中导入我们的jar包,调用里面的加、解密码方法,把结果保存在jmeter变量中,下面两个方法是beanshell中我们最常用到的:

    • vars.get(String paramStr):获得变量值
    • vars.put(String key,String value):,将数据存到jmeter变量中

    复制代码
    import com.pingan.ff.account.user.utils.*;
    
    //加密
    System.out.println("*****加密*****");
    String password = "123123";
    String encode = SecurityUtils.getKey(password);//调用工具类中的方法进行加密
    System.out.println("Set my encode");
    vars.put("encode",encode);//把值保存到jmeter变量encode中
    String getEncode=vars.get("encode");
    System.out.println("Get my encode: " + getEncode);
    复制代码

    5、把加密后的密码存到jmeter变量中,然后在http sampler中就可以通过${encode}进行使用了:

    6、执行脚本:

    四、Bean Shell常用内置变量

        JMeter在它的BeanShell中内置了变量,用户可以通过这些变量与JMeter进行交互,其中主要的变量及其使用方法如下:

    • log:写入信息到jmeber.log文件,使用方法:log.info(“This is log info!”);

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

    • vars - (JMeterVariables):操作jmeter变量,这个变量实际引用了JMeter线程中的局部变量容器(本质上是Map),它是测试用例与BeanShell交互的桥梁,常用方法:

        a) vars.get(String key):从jmeter中获得变量值

        b) vars.put(String key,String value):数据存到jmeter变量中

        更多方法可参考:org.apache.jmeter.threads.JMeterVariables

    • props - (JMeterProperties - class java.util.Properties):操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值,而不能是一个对象。对应于java.util.Properties。 

        a) props.get("START.HMS");  注:START.HMS为属性名,在文件jmeter.properties中定义 

        b) props.put("PROP1","1234"); 

    • prev - (SampleResult):获取前面的sample返回的信息,常用方法:

        a) getResponseDataAsString():获取响应信息

        b) getResponseCode() :获取响应code

        更多方法可参考:org.apache.jmeter.samplers.SampleResult

    • sampler - (Sampler):gives access to the current sampler

    官网:

    http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_Sampler

    http://jmeter.apache.org/usermanual/component_reference.html#BeanShell_PreProcessor

  • 相关阅读:
    PHP 使用 GET 传递数组变量
    Java实现 蓝桥杯 算法训练 数据交换
    Java实现 蓝桥杯 算法训练 数据交换
    Java实现 蓝桥杯 算法训练 数据交换
    Java实现 蓝桥杯 算法训练 景点游览
    Java实现 蓝桥杯 算法训练 景点游览
    Java实现 蓝桥杯 算法训练 景点游览
    Java实现 蓝桥杯 算法训练 二进制数数
    Java实现 蓝桥杯 算法训练 二进制数数
    Java实现 蓝桥杯 算法训练 二进制数数
  • 原文地址:https://www.cnblogs.com/kaola8023/p/7246573.html
Copyright © 2011-2022 走看看