Search code examples
jmeterjmeter-5.0jmeter-4.0jmeter-3.2

Jmeter using LoopCount instead of the ThreadNum variable(to save variable for every count and use it in another request)


I am setting my Jmeter variable using this using beanshell assertion:

${__setProperty(id_${__threadNum},${id},)}

And using this in another request like this:

${__P(id_${__threadNum},)}

and running the requests like this

enter image description here

However I want to run the loop count instead of the thread. So that i can run the request one by one. How can i change it to so it is loopNum instead of the thread

Can i use this below when i want to use the loop instead of the thread??

I am setting my Jmeter variable using this:

${__setProperty(id_${__iterationNum},${id},)}

And using this in another request like this:

${__P(id_${__iterationNum},)}

enter image description here


Solution

  • For getting the current iteration (1st loop - 1, 2nd loop - 2, etc.):

    props.put("id_" + vars.getIteration(), vars.get("id"));
    

    For getting all iterations (each time it will return 10)

    props.put("id_" + ctx.getThreadGroup().getSamplerController().getProperty("LoopController.loops"), vars.get("id"));
    

    Few more hints:

    1. Don't inline JMeter Functions or Variables into Scripts, either use "Parameters" section or go for code-based equivalents instead, like in my above examples props stand for JMeter Properties, vars - for JMeter Variables, etc.), see Top 8 JMeter Java Classes You Should Be Using with Groovy article for more details.
    2. Don't use Beanshell for scripting, since JMeter 3.1 you are supposed to use JSR223 Test Elements and Groovy language