【问题标题】:how to clear JSESSIONID of each Thread(user) in Jmeter on demand如何按需清除Jmeter中每个线程(用户)的JSESSIONID
【发布时间】:2012-09-24 07:55:46
【问题描述】:

我想随时(根据我的要求)清除 Jmeter JsessionID 变量。

我知道 Jmeter CookieManager 中有一个名为“Clear Cookie on each Iteration”的复选框选项。
但它会在每次迭代中清除会话,而我想在迭代中的任何时间清除它。

我如何在 Jmeter 中做到这一点?

【问题讨论】:

    标签: testing jmeter qa performance-testing


    【解决方案1】:

    您可以,只需添加后/预处理 beanShell 并使用此代码

    import org.apache.jmeter.protocol.http.control.CookieManager;
    import org.apache.jmeter.protocol.http.control.Cookie;
    CookieManager manager = sampler.getCookieManager();
    for (int i=0;i<manager.getCookieCount();i++){
        Cookie cookie = manager.get(i);
        //remove a cookie 
        if (cookie.getName().equals("BAD_COOKIE")){
            sampler.getCookieManager().remove(i);
        }
    }
    

    【讨论】:

      【解决方案2】:

      目前你不能简单地,特别是如果你想清除一个特定的 cookie。

      您应该在 JMeter Bugzilla 提出增强请求,准确说明您想要做什么。

      我认为自定义函数会是一个不错的功能,请参阅:

      【讨论】:

        【解决方案3】:

        我的方法与上述方法相距不远(抱歉,这对我不起作用),但它更短,包含循环内索引的重要更新,以及一些用于清除脚本使用的额外演示(我希望 ;) )

        JSESSIONID 是令牌之一(第一个或一些后续), 因此,为了删除包括 JSESSIONID 在内的所有令牌,我建议在您需要的 JSR223 Pre-和/或 PostProcessor 中使用以下 Java 脚本:

        import org.apache.jmeter.protocol.http.control.CookieManager;
        
        CookieManager cManager = sampler.getCookieManager();
            int count = cManager.getCookieCount();
            for (int index = 0; index < count; index++) {
                cManager.remove(0);
                }
        

        Example of adding the script to PostProcessor in jMeter

        注意:for循环里面是(0),而不是(index),这有助于避免OutOfBoundary异常,因为每次迭代后CookieManager实例的大小都会变小。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-02-09
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多