zoukankan      html  css  js  c++  java
  • jmeter,参数、Bean Shell断言

    一、参数说明

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

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

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

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

    2、props :操作jmeter属性,该变量引用了JMeter的配置信息,可以获取Jmeter的属性,它的使用方法与vars类似,但是只能put进去String类型的值。对应于java.util.Properties。 

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

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

        vars和props 主要两点的区别:
        vars 只能在档期线程内使用,props 可以跨线程租使用
        vars 只能保持String 或者Objec, props 是 Hashtable 对象

    3、ctx:该变量引用了当前线程的上下文

    4、prev :获取前面的sample返回的信息,常用方法:

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

        b) getResponseCode() ;    获取响应code

    5、写入信息到jmeber.log文件:log.info(“This is log info!”)

    6、获得当前线程

    thread=Thread.currentThread().getName();
    
    user=thread.replaceAll(" ","_")+"_压力测试";
    
    log.info("====打印线程名称===="+user);
    
    vars.put("user",user);

    二、Jmeter有哪些Bean Shell

    BeanShell Sampler       ,采样器

    BeanShell PreProcessor    ,前置处理器

    BeanShell PostProcessor  ,后置处理器

    BeanShell Timer      ,定时器

    BeanShell Listener     ,监听器

    BeanShell Assertion     ,断言

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

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

    3、打开jmeter,添加一个BeanShell ,导入我们的jar包,调用里面的方法。

    三、BeanShell断言

    //示例脚本
    if (ResponseCode != null && ResponseCode.equals ("200") == false )
    {
        // this is standard stuff
        Failure=true ;
        FailureMessage ="Response code was not a 200 response code it was " + ResponseCode + "." ;
        print ( "the return code is " + ResponseCode);   // this goes to stdout
        log.warn( "the return code is " + ResponseCode); // this goes to the JMeter log file
    } else {
        try
        {
             byte [] arr = (byte[]) ResponseData ;
             if (arr != null){
            String question=vars.get("question");
            String response= new String(ResponseDate,"utf-8");
            
                 Failure= ! response.contains("  "answer": " "+question+"  "  ");
                 FailureMessage = Failure?"The response data size was not as expected
    " 
              +"question"+question+"
    "
              +"response"+response:"";
    
              }else {
                  Failure= true ;
                  FailureMessage = "The response data size was null"  ;
               }
         }
         catch ( Throwable t )
         {
              print ( t ) ;
              log.warn("Error: ",t);
         }
    }

     

  • 相关阅读:
    关于多工序生产中损耗的计算
    ERP相关术语
    linux下如何挂载U盘
    linux boot下的文件
    MPLS简述
    BGP
    linux添加新磁盘
    列表的方法
    python之列表
    python之模块初识-os模块
  • 原文地址:https://www.cnblogs.com/yinwenbin/p/10827943.html
Copyright © 2011-2022 走看看