Search code examples
testinggroovyjmeterbeanshelljsr223

Jmeter: iterate and increment with every request then go back


I have JDBC request that returns a string like: "this is an example". I want to be able to split that string by character (i.e: var_1=t, var_2=h, var_3=i, etc..) and the iterate them in a HTTP Request. Not only iterate, but increment the search pattern every request and then decrease at the end like:

  1. request: t
  2. request: th
  3. request: thi
  4. .....
  5. request: thi
  6. request: th
  7. request: t

right now I am using a thread group composed of jdbc request, jsr223 sampler(I've tried with beanshell sampler also but with no success either..), a forEach controller to iterate trough the values, a http request and a response assertion. I haven't been able to make it work because I got stuck at the jsr223 configuration. I guess the right beanshell script would fix this but I suck at groovy script..


Solution

  • Add a BeanShell Sampler after your JDBC request with the below code in the code area:

    String MyVar = vars.get("MyVar");\\ MyVar is the name of the variable that hold the string returned from your JDBC request.
    int x = MyVar.length();
    
    for(i=3;i<=MyVar.length();i++){
        vars.put("Var_" + (i-2), MyVar.substring(0,i));
        vars.put("Var_" + (x * 2 - i -1), MyVar.substring(0,i));
    
    }
    

    Then configure your ForEach Controller as below:

    • Input variable prefix: Var
    • Output variable name: variable

    Now you can use ${variable} in your HTTP request which will hold the values you want.