无法在Jmeter中设置增量变量 [英] Unable to set incremental variable in Jmeter

查看:103
本文介绍了无法在Jmeter中设置增量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我简单的Jmeter测试计划.

Here is my simple Jmeter test plan.

用户参数如下:

我只是调用一个端点,在Regex Extractor的帮助下读取响应正文并根据找到的ID调用另一个端点. ForEach循环有助于确保对于所有找到的ID,在路径中以ID作为参数调用相同的终结点.

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.

我要在ForEach循环中使用另一个HTTP请求来实现的目的是读取响应,并且如果正文包含 Monday ,请增加 User Parameter 星期一加1,与星期二和其他所有用户参数相同.理想情况下,在测试套件结束时,我应该得到以下内容:

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. 星期一-5
  2. 星期二-3
  3. 星期三-空或零
  4. 星期四-空或零
  5. 星期五-1
  6. 星期六-12
  7. 周日-8

根据我的BeanShell脚本,我希望我遵循所有正确的路径:

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);
    };

这里的小问题是 User Parameters 变量永远不会更新,并且总是在运行结束时等于0.在这种情况下,我在做什么错?有人遇到过这个任务吗?

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. 请注意,从JMeter 3.1版开始,使用Groovy进行任何形式的脚本编写,以考虑迁移到 JSR223后处理器 >

  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

考虑到重复的'DayOfWeek':'xxx'模式,看来您不必在每个工作日创建7个分支,您可以使用

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

示例代码如下:

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

请参阅 Apache Groovy-为什么和如何使用它文章,在JMeter测试中使用Groovy脚本的更多信息

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

这篇关于无法在Jmeter中设置增量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆