Unable to set incremental variable in Jmeter

故事扮演 提交于 2019-12-09 03:50:55

问题


Here is my simple Jmeter test plan.

User Parameters look like this:

I'm simply calling one endpoint, reading the response body and according to found IDs with the help of Regex Extractor I'm calling another endpoint. ForEach loop helps make sure for all the found IDs same endpoint is called with ID as parameter in the Path.

What I'm trying to achieve with Another HTTP Request inside ForEach loop is to read the response, and if body contains Monday, increment User Parameter Monday by 1, same for Tuesday and for every other User Parameter. Ideally at the end of the test suite, I should get something like this:

  1. Monday - 5
  2. Tuesday - 3
  3. Wednesday - null or zero
  4. Thursday - null or zero
  5. Friday - 1
  6. Saturday - 12
  7. Sunday - 8

According to my BeanShell script I hope I'm following all the right paths:

import org.apache.commons.lang.StringUtils;

    String response = new String(data);
    int Mondays = 0;
    int Tuesdays = 0;
    int Wednesdays = 0;
    int Thursdays = 0;
    int Fridays = 0;
    int Saturdays = 0;
    int Sundays = 0;

    if(response.contains("'DayOfWeek':'Monday'")){
        Mondays++;
        vars.put("Monday", Mondays.toString);
    };
    if(response.contains("'DayOfWeek':'Tuesday'")){
        Tuesdays++;
        vars.put("Tuesday", Tuesdays.toString);
    };
    if(response.contains("'DayOfWeek':'Wednesday'")){
        Wednesdays++;
        vars.put("Wednesday", Wednesdays.toString);
    };
    if(response.contains("'DayOfWeek':'Thursday'")){
        Thursdays++;
        vars.put("Thursday", Thursdays.toString);
    };
    if(response.contains("'DayOfWeek':'Friday'")){
        Fridays++;
        vars.put("Friday", Fridays.toString);
    };
    if(response.contains("'DayOfWeek':'Saturday'")){
        Saturdays++;
        vars.put("Saturday", Saturdays.toString);
    };
    if(response.contains("'DayOfWeek':'Sunday'")){
        Sundays++;
        vars.put("Sunday", Sundays.toString);
    };

My small problem here is that User Parameters variables never get updated and always at the end of the run equals to 0. What am I doing wrong in this situation? Has anyone faced this task before?


回答1:


  1. Be aware that starting from JMeter version 3.1 it is recommended to use Groovy for any form of scripting to consider migrating to JSR223 PostProcessor

  2. Looking into repeating 'DayOfWeek':'xxx' pattern it seems you don't need to create 7 branches for each weekday, you can extract the current value using Regular Expressions and set or get and increment the relevant JMeter Variable

Example code would be something like:

def day = (prev.getResponseDataAsString() =~ "'DayOfWeek':'(\\w+)'")[0].get(1)
def value = (vars.get(day) ?: '0') as int
value++
vars.put(day, value as String)

See Apache Groovy - Why and How You Should Use It article for more information on using Groovy scripting in JMeter tests



来源:https://stackoverflow.com/questions/50820389/unable-to-set-incremental-variable-in-jmeter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!