JMeter在它的BeanShell中內置了變量,用戶可以通過這些變量與JMeter進行交互,其中主要的變量及其使用方法如下:
-
log:寫入信息到jmeber.log文件,使用方法:log.info(“Agoly”);
-
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