jmeter 不会跨线程组保留 cookie
jmeter won't keep cookies across thread groups
(关于CookieManager的问题似乎有很多,但我在搜索时没有找到我的问题)
我想弄清楚为什么 Cookie 管理器不保留我的 cookie,但我 运行 没有主意。
使用jmeter 2.13
我有以下测试计划设置:
- HTTP Headers
- HTTP Cookie Manager ("clear each iteration" is NOT checked, policy=compatibility, tried both implementation options)
- Thread group 1
-- perform login (returns a Set-Cookie header as expected)
- Thread groups 2-n
-- send http queries using cookies from login (this is the plan!)
每个请求returns 200,任何地方都没有重定向(第二个线程因为缺少cookie而重定向到登录页面的地方除外)
我的问题:
在进入线程组 2 之前,日志显示 CookieManager 已清除所有 cookie。我不明白为什么 - 它不应该保留它们吗,因为 header 管理器是一个 top-level 元素?
这是显示此内容的日志部分(有问题的行标有######):
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 1 : TEST LOOP 0 - login
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group TEST LOOP 0 - login.
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Thread will continue on error
2015/06/30 16:19:55 INFO - jmeter.threads.ThreadGroup: Starting thread group number 1 threads 1 ramp-up 1 perThread 1000.0 delayedStart=false
2015/06/30 16:19:55 DEBUG - jmeter.protocol.http.control.CookieManager: Clear all cookies from store
2015/06/30 16:19:55 INFO - jmeter.threads.ThreadGroup: Started thread group number 1
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Waiting for thread group: TEST LOOP 0 - login to finish before starting next group
2015/06/30 16:19:55 INFO - jmeter.threads.JMeterThread: Thread started: TEST LOOP 0 - login 1-1
2015/06/30 16:19:55 DEBUG - jmeter.protocol.http.control.HC3CookieHandler: Found 0 cookies for [url edited]
2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.control.HC3CookieHandler: Received Cookie: JSESSIONID_PUBLIC=swfw6xfdo0es18e5ux1xhad6a;Path=/url/edited;HttpOnly From: [url edited]
2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.control.CookieManager: Add cookie to store server-edited TRUE /url/edited FALSE 0 JSESSIONID_PUBLIC swfw6xfdo0es18e5ux1xhad6a
2015/06/30 16:19:56 INFO - jmeter.threads.JMeterThread: Thread is done: TEST LOOP 0 - login 1-1
2015/06/30 16:19:56 INFO - jmeter.threads.JMeterThread: Thread finished: TEST LOOP 0 - login 1-1
2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.sampler.HTTPHC4Impl: Thread Finished
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 2 : TEST LOOP 1 - search, high frequency, Ssd-Requests
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group TEST LOOP 1 - search, high frequency, Ssd-Requests.
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: Thread will continue on error
2015/06/30 16:19:56 INFO - jmeter.threads.ThreadGroup: Starting thread group number 2 threads 1 ramp-up 20 perThread 20000.0 delayedStart=false
##### 2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.control.CookieManager: Clear all cookies from store ######
2015/06/30 16:19:56 INFO - jmeter.threads.ThreadGroup: Started thread group number 2
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: All thread groups have been started
2015/06/30 16:19:56 INFO - jmeter.threads.JMeterThread: Thread started: TEST LOOP 1 - search, high frequency, Ssd-Requests 2-1
我尝试更改 jmeter.properties 中的一些设置:
CookieManager.save.cookies=true
以及
CookieManager.check.cookies=false
但似乎都不起作用。
我的第一个理论是,它与所讨论的 cookie 没有添加过期这一事实有关,但我在添加它时没有看到错误 - 只是它在不应该被删除的时候又被删除了。
选项 1:
使用 JMeter 的 __setProperty() function to convert variables to properties. JMeter Variables scope is limited to current thread group only, and JMeter Properties are in fact java.util.Properties 因此它们对于整个 JVM 是全局的。
选项 2:
使用脚本提取 cookie 名称和值(以及域、过期时间、安全等字段)。这种方法更复杂,但它提供了更多的控制和灵活性。有关在不同线程组之间共享 cookie 的示例代码,请参阅 How to use BeanShell: JMeter's favorite built-in component 指南。
你误解了线程组和 Cookiemanager。
首先,每个线程都有自己的 Cookie 版本,该版本会在线程完成每个样本后更新
其次,变量或与线程关联的任何对象(如 cookiemanager)都由线程共享,因此根本不会被 2 个线程组共享。
即使 Dmitri 指出 Beanshell,在不同线程组中的线程之间共享东西对于脚本的性能或可维护性来说也不是一个好主意。
我认为您正在使用 2 个不同的线程组,但应该只使用 1 个,这将解决您的问题。
(关于CookieManager的问题似乎有很多,但我在搜索时没有找到我的问题)
我想弄清楚为什么 Cookie 管理器不保留我的 cookie,但我 运行 没有主意。
使用jmeter 2.13
我有以下测试计划设置:
- HTTP Headers
- HTTP Cookie Manager ("clear each iteration" is NOT checked, policy=compatibility, tried both implementation options)
- Thread group 1
-- perform login (returns a Set-Cookie header as expected)
- Thread groups 2-n
-- send http queries using cookies from login (this is the plan!)
每个请求returns 200,任何地方都没有重定向(第二个线程因为缺少cookie而重定向到登录页面的地方除外)
我的问题: 在进入线程组 2 之前,日志显示 CookieManager 已清除所有 cookie。我不明白为什么 - 它不应该保留它们吗,因为 header 管理器是一个 top-level 元素?
这是显示此内容的日志部分(有问题的行标有######):
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 1 : TEST LOOP 0 - login
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group TEST LOOP 0 - login.
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Thread will continue on error
2015/06/30 16:19:55 INFO - jmeter.threads.ThreadGroup: Starting thread group number 1 threads 1 ramp-up 1 perThread 1000.0 delayedStart=false
2015/06/30 16:19:55 DEBUG - jmeter.protocol.http.control.CookieManager: Clear all cookies from store
2015/06/30 16:19:55 INFO - jmeter.threads.ThreadGroup: Started thread group number 1
2015/06/30 16:19:55 INFO - jmeter.engine.StandardJMeterEngine: Waiting for thread group: TEST LOOP 0 - login to finish before starting next group
2015/06/30 16:19:55 INFO - jmeter.threads.JMeterThread: Thread started: TEST LOOP 0 - login 1-1
2015/06/30 16:19:55 DEBUG - jmeter.protocol.http.control.HC3CookieHandler: Found 0 cookies for [url edited]
2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.control.HC3CookieHandler: Received Cookie: JSESSIONID_PUBLIC=swfw6xfdo0es18e5ux1xhad6a;Path=/url/edited;HttpOnly From: [url edited]
2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.control.CookieManager: Add cookie to store server-edited TRUE /url/edited FALSE 0 JSESSIONID_PUBLIC swfw6xfdo0es18e5ux1xhad6a
2015/06/30 16:19:56 INFO - jmeter.threads.JMeterThread: Thread is done: TEST LOOP 0 - login 1-1
2015/06/30 16:19:56 INFO - jmeter.threads.JMeterThread: Thread finished: TEST LOOP 0 - login 1-1
2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.sampler.HTTPHC4Impl: Thread Finished
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: Starting ThreadGroup: 2 : TEST LOOP 1 - search, high frequency, Ssd-Requests
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: Starting 1 threads for group TEST LOOP 1 - search, high frequency, Ssd-Requests.
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: Thread will continue on error
2015/06/30 16:19:56 INFO - jmeter.threads.ThreadGroup: Starting thread group number 2 threads 1 ramp-up 20 perThread 20000.0 delayedStart=false
##### 2015/06/30 16:19:56 DEBUG - jmeter.protocol.http.control.CookieManager: Clear all cookies from store ######
2015/06/30 16:19:56 INFO - jmeter.threads.ThreadGroup: Started thread group number 2
2015/06/30 16:19:56 INFO - jmeter.engine.StandardJMeterEngine: All thread groups have been started
2015/06/30 16:19:56 INFO - jmeter.threads.JMeterThread: Thread started: TEST LOOP 1 - search, high frequency, Ssd-Requests 2-1
我尝试更改 jmeter.properties 中的一些设置:
CookieManager.save.cookies=true
以及
CookieManager.check.cookies=false
但似乎都不起作用。
我的第一个理论是,它与所讨论的 cookie 没有添加过期这一事实有关,但我在添加它时没有看到错误 - 只是它在不应该被删除的时候又被删除了。
选项 1:
使用 JMeter 的 __setProperty() function to convert variables to properties. JMeter Variables scope is limited to current thread group only, and JMeter Properties are in fact java.util.Properties 因此它们对于整个 JVM 是全局的。
选项 2:
使用脚本提取 cookie 名称和值(以及域、过期时间、安全等字段)。这种方法更复杂,但它提供了更多的控制和灵活性。有关在不同线程组之间共享 cookie 的示例代码,请参阅 How to use BeanShell: JMeter's favorite built-in component 指南。
你误解了线程组和 Cookiemanager。
首先,每个线程都有自己的 Cookie 版本,该版本会在线程完成每个样本后更新
其次,变量或与线程关联的任何对象(如 cookiemanager)都由线程共享,因此根本不会被 2 个线程组共享。
即使 Dmitri 指出 Beanshell,在不同线程组中的线程之间共享东西对于脚本的性能或可维护性来说也不是一个好主意。
我认为您正在使用 2 个不同的线程组,但应该只使用 1 个,这将解决您的问题。