jmeter編寫beanshell及內置方法的使用

(一)BeanShell簡介html

  BeanShell是一個小型嵌入式Java源代碼解釋器,具備對象腳本語言特性,可以動態地執行標準JAVA語法,並利用在JavaScript和Perl中常見的的鬆散類型、命令、閉包等通用腳原本對其進行拓展。BeanShell不單單能夠經過運行其內部的腳原本處理Java應用程序,還能夠在運行過程當中動態執行你java應用程序執行java代碼。由於BeanShell是用java寫的,運行在同一個虛擬機的應用程序,所以能夠自由地引用對象腳本並返回結果。java

       BeanShell通常使用在邏輯比較複雜的地方shell

(二)Beanshell特色apache

  ·完整的java語法的動態執行,java代碼片斷,以及鬆散類型的java和其餘腳本。小程序

  ·透明地訪問全部java對象和APIapi

  ·運行在四種模式:命令行,控制檯,小程序,遠程會話服務器。安全

  ·能夠在安全約束的環境中工做服務器

  ·編譯器是一個小於150K的jar文件閉包

(三)BeanShell的六個種類函數

  定時器: 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

(五)BeanShell使用(以BeanShell PreProcessor爲例)

1)運行自定義函數

2)調用外部Java文件,  source("D\\workspace\\TelephoneNumber.java")

3)導入本身打出的jar包,放到lib下的ext文件夾中,再導入類路徑調用,以下圖:

(六)其它用法: 

一、在Test Plan中定義以下三個變量:

二、Bean Shell可腳本以下:

  a、bean shell能夠接受傳入參數,以下圖:${u1} ${u2} ${u3}

  b、參數能夠經過bsh.args[]按順序提取

  c、bean shell提供了一個內置變量Parameters,來保存參數的集合

      (以上把變量存入參數中的方式能夠這樣寫:vars.put("ABCEEFG","${sid}");  結果:ABCEEFG=70695742 )

相關文章
相關標籤/搜索