JMeter 5.1。以下链接提供了工作代码Restarting a user thread conditionally in JMeter,其中从setTestLogicalAction调用SampleResult。该方法确实在https://jmeter.apache.org/api/org/apache/jmeter/samplers/SampleResult.html中的方法中列出。
但是,它也在https://jmeter.apache.org/api/org/apache/jmeter/threads/JMeterContext.html中列出,但是使用org.apache.jmeter.threads.JMeterContext.setTestLogicalAction

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``    import org.apache.jmeter.samplers.SampleResult;     import org.apache.jmeter . . . '' : Cannot reach instance method: setTestLogicalAction( org.apache.jmeter.threads.JMeterContext$TestLogicalAction ) from static context: org.apache.jmeter.threads.JMeterContext


为什么不能使用setTestLogicalAction中的JMeterContext?我想念一下Java中的类吗?

另外,上述文章中的代码在beanshell中有效,但是对以上代码的修改和修改均不能在JSR223 + Groovy中使用。在注意到方法不是静态的之后,代码如下所示(尝试了两个路径,最后我将它们一起列出),没有错误,但是线程继续运行,没有重新启动...

jmco = new org.apache.jmeter.threads.JMeterContext();
jmsr = new org.apache.jmeter.samplers.SampleResult();

jmco.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);
jmsr.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);


我可以在groovy脚本中使用setTestLogicalAction吗?

附言有趣的是,beanshell如何调用不带实例化的非静态方法...

添加2019/04/01:

def mycontext = org.apache.jmeter.threads.JMeterContextService.getContext()
mycontext.setTestLogicalAction(org.apache.jmeter.threads.JMeterContext.TestLogicalAction.START_NEXT_ITERATION_OF_THREAD);


在Dmitri答案中提出建议后,代码已更改为上述代码,但代码未重新启动线程。

最佳答案

如果您不太擅长编程,请使用Flow Control Action采样器,而不要尝试在代码中复制其功能,无论如何它都会更快。
因为它们是pre-defined in JSR223 Test Elements,所以无需实例化JMeterContext或SampleResult,而且您做错了,即访问JMeterContext的正确方法是调用JMeterContextService.getContext() function

java - JMeterContext的JMeter setTestLogicalAction给出“无法到达实例方法”-LMLPHP
Since JMeter 3.1 you should be using JSR223 Test Elements and Groovy language for scripting,所以我建议您忘记Beanshell。

关于java - JMeterContext的JMeter setTestLogicalAction给出“无法到达实例方法”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55416643/

10-11 22:47