从groovy传递jmeter取样器结果

时间:2016-03-22 22:43:37

标签: groovy jmeter

我是JMeter的新手,我正在尝试编写一些Groovy采样器。我无法从脚本代码中找到如何与JMeter框架进行通信的文档/示例。我希望有人能指出我的文档的一个很好的起点。

我在JSR223采样器中尝试了以下内容

import org.apache.jmeter.samplers.SampleResult;
println(" running test") 
SampleResult sr=new SampleResult();
sr.setResponseCode("300");
sr.setSuccessful(false);
sr.setErrorCount(2);
sr.setResponseData("This is the response");
ResponseCode=300
return sr;

但它看起来好像没有效果。查看结果树监听器输出的采样器结果是

Thread Name: Thread Group 1-1
Sample Start: 2016-03-22 17:38:07 CDT
Load time: 12
Connect Time: 0
Latency: 0
Size in bytes: 0
Headers size in bytes: 0
Body size in bytes: 0
Sample Count: 1
Error Count: 0
Response code: 200
Response message: OK

...

2 个答案:

答案 0 :(得分:2)

它不会那样工作。

如果你研究JSR223 Sampler GUI,你会看到一些预定义的变量,如:

  • CTX
  • 道具
  • SampleResult

JSR223 Pre Defined Variables

所以你已经有了一个实例化的SampleResult,它可以直接使用,如:

SampleResult.setResponseCode("300");
SampleResult.setSuccessful(false);
SampleResult.setErrorCount(2);
SampleResult.setResponseData("This is the response");

有关使用groovy脚本和脚本最佳实践的详细信息,请查看Beanshell vs JSR223 vs Java JMeter Scripting: The Performance-Off You've Been Waiting For!指南。

答案 1 :(得分:0)

JMeter 5.4 JSR223 采样器

more

//set Response Code
SampleResult.setResponseCode("201");
SampleResult.setSuccessful(false);
 
//set Response Message
SampleResult.setResponseMessage("This is message returned from JSR223 script");
 
//set Response Data
SampleResult.setResponseData("You will see this sentence in Response Data tab", "UTF-8");

println( "The Sample Label is : " + SampleResult.getSampleLabel() );
println( "The Start Time in miliseconds is : " + SampleResult.getStartTime() );
println( "The Response Code is : " + SampleResult.getResponseCode() );
println( "The Response Message is : " + SampleResult.getResponseMessage() );