I am developing a Java Request Sampler with JMeter by extending AbstractJavaSamplerClient. The test goes like this:
public class JmeterTest extends AbstractJavaSamplerClient {
public void setupTest(JavaSamplerContext context) {
System.out.println("Test setup");
}
public SampleResult runTest(JavaSamplerContext context) {
SampleResult result = new SampleResult();
System.out.println("Test..");
result.setSuccessful(true);
result.setResponseData("Success");
}
public void teardownTest(JavaSamplerContext context) {
System.out.println("Test cleanup");
}
}
I created a jar out of this test class and pointed it through the JMeter UI (through Java Request Sampler). The problem in my case is when I run the test with 5 users
, the setUpTest & tearDownTest methods gets called for every user as per design.
Is there any setting where the setUp and Cleanup methods get called only once for all threads in the thread group. My requirement is to do setup and cleanup only once for all threads.
答案 0 :(得分:1)
以下是您可以尝试的选项:
${__BeanShell(vars.getIteration() == 1)}
作为条件修改您的代码,如:
public void setupTest(JavaSamplerContext context) {
if (JMeterContextService.getContext().getVariables().getIteration() == 1) {
System.out.println("Test setup");
}
}
第2点和第3点都假设setUp代码仅在每个线程的第一次测试迭代期间执行。请记住,JMeterVariables.getIteration()数字仅在Thread Group - 级别循环发生时递增。