3
votes

Is there any possibility to iterate or loop through a "User Defined Variables" - set like it's possible with a CSV data set (WHILE-COUNTER-CSV Data Set Config)?

I want to fire a JDBC request (Select Statement) for each variable contained in the "User Defined Variables" - Set. It works fine with a CSV file, but i can't figure out how I can loop through a variables set. Is that even possible? I have various scenarios, where I want to loop through a "User Defined Variables" - set.

2
May I know the issue with using CSV Dataset Config? I don't think there is way to iterate through all UDVs - Naveen Kumar R B
I don't want to use CSV's because of multiple reasons: - Im afraid that too much CSV reads have an negative impact on performance of the test - Information isn't centralized in the Testplan, instead I always have to open the CSV files to look for something - - VolJin

2 Answers

6
votes

In order to be able to iterate the User Defined Variables with ForEach Controller you just need to follow simple naming convention like:

  • var_1=someValue
  • var_2=someOtherValue
  • var_3=someMoreValue
  • etc.

However if you would like to keep the original variable names you can create an extra set of JMeter Variables which can be consumed by the ForEach Controller using the next steps:

  1. Let's assume you have the following User Defined Variables:

    JMeter User Defined Variables

    and you want to use their values in ForEach controller

  2. Add JSR223 Test Element (Sampler, Pre/Post Processor, etc) somewhere in your script and put the following code in its "Script" area

    import org.apache.jmeter.threads.JMeterVariables;
    
    int counter = 1;
    
    JMeterVariables tempVars = new JMeterVariables()
    
    vars.entrySet().each { entry -> 
        def name = entry.getKey()
        if (!name.equals("JMeterThread.last_sample_ok") && !name.equals("JMeterThread.pack") && !name.equals("START.HMS") && !name.equals("START.MS") &&
            !name.equals("START.YMD") && !name.equals("TESTSTART.MS")) {
            tempVars.put("tempVar_" + counter, entry.getValue())
            counter++;
        }
    }
    
    vars.putAll(tempVars);
    

    After script finishes you should have 3 more JMeter Variables

    JMeter variables generated by groovy

  3. Once you have them - you can use ForEach Controller configured like:

    ForEach Controller for extra variables

  4. So you will be able to use ${current} (or whatever you put into the "Output variable name" in the JDBC Request

    Groovy Variables substitution

See Groovy Is the New Black article to learn more about using Groovy in JMeter tests.

-5
votes