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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM