valentinvas
valentinvas

Reputation: 19

JMeter - nested variables

How can I properly reference loop counter i here?

log.info("Property of thread '" + ${__threadNum} + "' in tearDown is: " + ${__P(ah_${__threadNum},)});
log.info("Property of thread '" + 2 + "' in tearDown is: " + ${__P(ah_2,)});
vars.put("assertionResults", ${__P(ah_${__threadNum},)});
log.info("assertionResults = " + vars.get("assertionResults"));

// Loop
log.info("Loop is starting!");
for (int i = 1; i <= 2; i++) {  
    log.info("Property of thread '" + i + "' in tearDown is: " + ${__P(ah_i,)});
}
log.info("Loop is ending!");

The problem is in ${__P(ah_i,)}. When I use ${__P(ah_2,)}, it's OK.

Upvotes: 0

Views: 550

Answers (2)

valentinvas
valentinvas

Reputation: 19

Thank you so much @Dmitri T for your answer! The finally solution that works for me is:

log.info("Property of thread '" + ${__threadNum} + "' in tearDown is: " + ${__P(ah_${__threadNum},)});
log.info("Property of thread '" + 2 + "' in tearDown is: " + ${__P(ah_2,)});
vars.put("assertionResults", ${__P(ah_${__threadNum},)});
log.info("assertionResults = " + vars.get("assertionResults"));

// Loop
log.info("Loop is starting!");
for (int i = 1; i <= 2; i++) {  
    log.info("Property of thread '" + i + "' in tearDown is: " + props.get("ah_" + (ctx.getThreadNum() + i)));
}
log.info("Loop is ending!");

Upvotes: 0

Dmitri T
Dmitri T

Reputation: 168002

Don't inline JMeter Functions or Variables into Groovy scripts.

As per JSR223 Sampler documentation:

The JSR223 test elements have a feature (compilation) that can significantly increase performance. To benefit from this feature:

  • Use Script files instead of inlining them. This will make JMeter compile them if this feature is available on ScriptEngine and cache them.
  • Or Use Script Text and check Cache compiled script if available property.

When using this feature, ensure your script code does not use JMeter variables or JMeter function calls directly in script code as caching would only cache first replacement. Instead use script parameters.

So:

  • instead of ${__threadNum} - ctx.getThreadNum()
  • instead of ${__P(ah_${__threadNum},)} - props.get('ah_' + ctx.getThreadNum())
  • etc.

More information: Top 8 JMeter Java Classes You Should Be Using with Groovy

Upvotes: 1

Related Questions