【问题标题】:Can we pass the beanshell assertion variable (in a thread) to cookie manager user defined variable (outside the thread) in jmeter?我们可以将 beanshell 断言变量(在线程中)传递给 jmeter 中的 cookie 管理器用户定义变量(线程外)吗?
【发布时间】:2018-04-03 14:58:53
【问题描述】:

我们可以将 beanshell 断言变量(在线程中)传递给 jmeter 中的 cookie 管理器用户定义变量(线程外)吗?

This is the image of user defined cookies which are been used on my request

This is bean shell variable

【问题讨论】:

    标签: jmeter session-cookies assertions beanshell


    【解决方案1】:

    我不认为它会这样工作,你可以使用类似的东西:

    1. 在“init”线程组中:

      import org.apache.jmeter.protocol.http.control.CookieManager;
      
      CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
      props.put("cookiecount",String.valueOf(manager.getCookieCount()));
      for (int i=0;i<manager.getCookieCount();i++){
          // code to convert Cookie information to JMeter Properties
          props.put("cookie_name_" + i, manager.get(i).getName());
          //…..
      }
      
    2. 在“动作”线程组中:

      import org.apache.jmeter.protocol.http.control.CookieManager;
      import org.apache.jmeter.protocol.http.control.Cookie;
      import org.apache.jmeter.testelement.property.JMeterProperty;
      
      CookieManager manager = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager").getObjectValue();
      int count = Integer.parseInt(props.getProperty("cookiecount"));
      for (int i=0;i<count;i++) {
          Cookie cookie = new Cookie(props.getProperty("cookie_name"+i),props.getProperty("cookie_value"+i), props.getProperty("cookie_domain"+i),props.getProperty("cookie_path"+i), Boolean.parseBoolean(props.getProperty("cookie_secure"+i)), Long.parseLong(props.getProperty("cookie_expires"+i)));
          manager.add(cookie);
      }
      
      JMeterProperty cookieprop = ctx.getCurrentSampler().getProperty("HTTPSampler.cookie_manager");
      cookieprop.setObjectValue(manager);
      ctx.getCurrentSampler().setProperty(myprop);
      
    3. 最好使用 Thread Groups 本地的 2 个(或 3 个)单独的而不是全局 HTTP Cookie 管理器。

    4. 考虑迁移到JSR223 Assertion 和Groovy 语言,因为在高负载情况下,Beanshell 性能可能是一个很大的问号。有关更多详细信息,请参阅Scripting JMeter Assertions in Groovy - A Tutorial 文章。

    【讨论】:

    • 非常感谢您的建议,我会尝试实施它。此外,我没有使用本地 cookie 管理器,因为我只有在 Init 线程的请求中生成的会话 ID。因此,我必须从 Init 线程中提取会话 ID 和其他 cookie,并需要在 Action 线程中解析它们。我认为最好提取变量并在全局 cookie 管理器中替换它,这样用户定义的 cookie 就可以通过在任何线程中的请求来使用.....
    • 请建议这是一种明智的方法。如果实现起来相当复杂,请提出更好的方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-08
    • 1970-01-01
    • 2021-11-21
    • 1970-01-01
    相关资源
    最近更新 更多