JMeter接口測試-JSR223內置變量使用-2

1、ctx

<code>ctx變量是JMeter JSR223功能最強大的內置變量之一。
通過它可以輕鬆的訪問當前線程的上下文。
在JMeter內部,ctx映射為org.apache.jmeter.threads的JMeterContext類。
參見:
https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html

由於JMeterContext不具有線程安全性,故僅適用於在單線程中使用。/<code>

1.2 ctx 常用方法

1.2.1 getVariables

  • 方法

public JMeterVariables getVariables()

  • 功能

獲取JMeter當前線程的所有變量

<code>vars.put("name","zhangsan");
vv = ctx.getVariables().get("name");
log.info("name value is " + vv);
//output
name value is zhangsan/<code>

1.2.2 getProperties

  • 方法

public Properties getProperties()

  • 功能

獲取所有的JMeter屬性

<code>pv = ctx.getProperties().getProperty("sampleresult.default.encoding")
log.info("sampleresult.default.encoding value is " + pv);
//output
sampleresult.default.encoding value is UTF-8/<code>

1.2.3 getPreviousResult

  • 方法

public SampleResult getPreviousResult()

  • 功能

獲取前一個取樣器的結果

<code>//SampleResult需要import對象
import org.apache.jmeter.samplers.SampleResult
SampleResult rsp = ctx.getPreviousResult()
String rh = rsp.getRequestHeaders()
log.info('\\n' + rh)
//output
Connection: keep-alive
Host: www.baidu.com
User-Agent: Apache-HttpClient/4.5.10 (Java/1.8.0_212)/<code>

1.2.4 getCurrentSampler

  • 方法

public Sampler getCurrentSampler()

  • 功能

獲取當前取樣器對象

<code>csn = ctx.getCurrentSampler().getName();
log.info("csn value is " + csn);
//output
csn value is HTTP Request_test/<code>

1.2.5 getPreviousSampler

  • 方法

public Sampler getPreviousSampler()

  • 功能

獲取前一個取樣器對象

<code>csn = ctx.getCurrentSampler().getName();
log.info("csn value is " + csn);
psn = ctx.getPreviousSampler().getName();
log.info("psn value is " + psn);
//output
csn value is HTTP Request_2
psn value is HTTP Request_1/<code>

1.2.6 getThreadNum

  • 方法

public int getThreadNum()

  • 功能

獲取當前線程組下的線程編號(編號從0開始)

<code>tn = ctx.getThreadNum();
log.info('tn is ' + tn);
//output
tn is 0/<code>

1.2.7 getThread

  • 方法

public JMeterThread getThread()

  • 功能

獲取線程對象

<code>tname = ctx.getThread().getThreadName();
log.info('tname is ' + tname);
//output
tname is Thread Group 1-1/<code>

1.2.8 getThreadGroup

  • 方法

public AbstractThreadGroup getThreadGroup()

  • 功能

獲取線程組對象

<code>ctgn = ctx.getThreadGroup().getName();
log.info('ctgn is ' + ctgn);
//output
ctgn is Thread Group_test/<code>

1.2.9 getTestLogicalAction

  • 方法

public JMeterContext.TestLogicalAction getTestLogicalAction()

  • 功能

獲取線程的運行邏輯

<code>e = ctx.getTestLogicalAction().values();
log.info('enum list is ' + e);
//output
enum list is [CONTINUE, START_NEXT_ITERATION_OF_THREAD, START_NEXT_ITERATION_OF_CURRENT_LOOP, BREAK_CURRENT_LOOP]/<code>

1.2.10 getSamplerContext

  • 方法

public Map<string> getSamplerContext()/<string>

  • 功能

獲取取樣器線程的上下文,返回samplerContext對象

<code>apm = ctx.getSamplerContext().getProperties()
apm.each{pname,pvalue ->
\tlog.info('property name:value is: ' + pname + ':' + pvalue)\t
}

//output
property name:value is: empty:true
property name:value is: class:class java.util.concurrent.ConcurrentHashMap/<code>


分享到:


相關文章: