Mats Johansson
Mats Johansson

Reputation: 87

How to pass a property or thread group variable to sample_variables=<variable_name>

I would like to add a variable from thread group 1 as a custom field to the jtl file for lines reported by thread group 2 by using the property sample_variables=. Tried w making a property of the local variable from Thread Group 1 but sample_variables don't seem to print properties.

Is it anyway I can log a variable from Thread Group 1 for every execution in Thread Group 2?

Test Plan (initiated w property sample_variables=var1)
--> Thread Group 1
----> JDBC request setting var1   # this prints var1 nicely to the jtl file
--> Thread Group 2
---> Another JDBC request   # this prints "null" to jtl (as var1 is local to TG1)

Upvotes: 1

Views: 1273

Answers (1)

Dmitri T
Dmitri T

Reputation: 168197

  1. When you use Sample Variables property you instruct JMeter to save the defined variable(s) value(s) into extra column(s) in the .jtl results file

  2. According to JMeter documentation:

    Properties are not the same as variables. Variables are local to a thread; properties are common to all threads, and need to be referenced using the __P or __property function.

If you just want to copy a single value from the Thread Group 1 to Thread Group 2 you need to:

  • Add JSR223 Sampler to the end of the Thread Group 1

  • Put the following code into "Script" area:

    prev.setIgnore() //don't save this JSR223 Sampler in listeners or .jtl result file
    props.put('var1', vars.get('var1')) // convert var1 variable into a JMeter property
    
  • Add JSR223 Sampler to the beginning of the Thread Group 2

  • Put the following code into "Script" area:

    prev.setIgnore()
    vars.put('var1',props.get('var1')) // convert var1 property into a JMeter Variable
    

In the above code props is an instance of java.util.Properties and vars stands for JMeterVariables, see Top 8 JMeter Java Classes You Should Be Using with Groovy for more information on this and other JMeter API shorthands available for JSR223 Test Elements.

Upvotes: 3

Related Questions