Java Apache JMeter 中的会话/cookie 管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1389464/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 11:33:12  来源:igfitidea点击:

Session/cookie management in Apache JMeter

javasessioncookiesjmetersession-cookies

提问by Priyank

We have a simple performance test flow in an application.

我们在应用程序中有一个简单的性能测试流程。

  1. We login
  2. Search based on some criteria
  3. repeat searches for different parameters.
  1. 我们登录
  2. 根据某些条件搜索
  3. 重复搜索不同的参数。

We are using Jmeter to do a performance testing. We need to have multiple threads running to test this in a scalable manner.

我们正在使用 Jmeter 进行性能测试。我们需要运行多个线程来以可扩展的方式进行测试。

The way we currently have this arranged is:

我们目前的安排方式是:

-Test Plan
  - Thread Group
      - Cookie Manager
      - Login To application
      - Search on param 1
      - Search on param 2
  - results summary table
  - Summary report

So basically we have summary return table and report present on plan level while cookie manager is present on thread group level.

所以基本上我们有汇总返回表和报告存在于计划级别,而 cookie 管理器存在于线程组级别。

When I run for one thread it runs fine and completes well. When I scale it to multiple threads, as soon as the next thread kicks off, the session for the last thread is invalidated. This causes failures for all the already running threads due to newly spawned thread.

当我运行一个线程时,它运行良好并且完成得很好。当我将它扩展到多个线程时,只要下一个线程开始,最后一个线程的会话就会失效。由于新产生的线程,这会导致所有已经运行的线程失败。

I reached this result with observation:
1. If I run multiple threads, only last thread has got valid responses in result summary tree
2. If I run with 2 threads with ramp up period as 10 seconds, which means each thread gets time to finish itself, then both of them run successfully.

我通过观察得出了这个结果:
1. 如果我运行多个线程,则只有最后一个线程在结果摘要树中得到了有效响应
2. 如果我使用 2 个线程运行,启动周期为 10 秒,这意味着每个线程都有时间完成本身,然后它们都成功运行。

As per my understanding each thread login's into application and since cookie manager is at thread level, the values will be maintained for session id for each thread respectively? But what is causing the override of the session id value between threads?

根据我的理解,每个线程都登录到应用程序中,并且由于 cookie 管理器处于线程级别,因此将为每个线程分别维护会话 id 的值?但是是什么导致线程之间会话 id 值被覆盖?

Any help will be much appreciated.

任何帮助都感激不尽。

采纳答案by Andrey Adamovich

Copied from jmeter documentation:

复制自 jmeter 文档:

The last element is a HTTP Cookie Manager . A Cookie Manager should be added to all web tests - otherwise JMeter will ignore cookies. By adding it at the Thread Group level, we ensure that all HTTP requests will share the same cookies.

最后一个元素是 HTTP Cookie Manager。Cookie 管理器应添加到所有 Web 测试中 - 否则 JMeter 将忽略 cookie。通过在线程组级别添加它,我们确保所有 HTTP 请求将共享相同的 cookie。

From chapter "4.2.2 Logic Controllers" in http://jmeter.apache.org/usermanual/test_plan.html.

来自http://jmeter.apache.org/usermanual/test_plan.html 中的“4.2.2 逻辑控制器”一章。

EDIT: I guess you should use http://jmeter.apache.org/usermanual/component_reference.html#Simple_Controllerto group your requests together with Cookie Manager.

编辑:我想您应该使用http://jmeter.apache.org/usermanual/component_reference.html#Simple_Controller将您的请求与 Cookie 管理器组合在一起。

回答by Isaac

Try to increase the ramp up time. I ran into the same issue where the ramp up time was about 1 second then I increased it to 3 seconds per thread and it ran fine.

尝试增加加速时间。我遇到了相同的问题,其中加速时间约为 1 秒,然后我将其增加到每个线程 3 秒,并且运行良好。

回答by akostadinov

I think that Andrey's answer cannot help. He quotes that each request will use the same cookies BUT according to jmeter manual:

我认为安德烈的回答无济于事。他引用每个请求将使用相同的 cookie 但根据 jmeter 手册:

Each JMeter thread has its own "cookie storage area".

每个 JMeter 线程都有自己的“cookie 存储区”。

As far as I understand the question, you want each thread to share the same session ID cookie. So it seems to me you need to have two thread groups and execute them consecutively. First thread group (with a single thread that executes once only) should login and save the session cookie value to a global parameter (perhaps you need to use jmeter's scripting capabilities). Then set that cookie in the cookie manager of the second thread group.

据我了解这个问题,您希望每个线程共享相同的会话 ID cookie。所以在我看来你需要有两个线程组并连续执行它们。第一个线程组(带有一个只执行一次的单个线程)应该登录并将会话 cookie 值保存到一个全局参数中(也许您需要使用 jmeter 的脚本功能)。然后在第二个线程组的 cookie 管理器中设置该 cookie。

Hope that helps.

希望有帮助。

回答by Jigish Chawda

Try this:

尝试这个:

Open the user.properties present in the bin folder of JMeter

打开 JMeter 的 bin 文件夹中的 user.properties

Edit it and add the following line:

编辑它并添加以下行:

CookieManager.check.cookies=false

Save it and run the script. I hope it will solve your problem.

保存并运行脚本。我希望它能解决你的问题。

回答by user2522062

First change your code to:

首先将您的代码更改为:

jmeter.properties
CookieManager.save.cookies=true
CookieManager.name.prefix=mycookie_

Next, add a HTTP cookie manager in the same thread group as your java sampler.

接下来,在与 Java 采样器相同的线程组中添加一个 HTTP cookie 管理器。

Then in your java sampler add:

然后在您的 Java 采样器中添加:

JMeterVariables jmv = JMeterContextService.getContext().getVariables();
Iterator<Map.Entry<String,Object>> it = jmv.getIterator();
while(it.hasNext()){
    Map.Entry<String,Object> v = it.next();
    System.out.println("name: " + v.getKey() + " value: " + v.getValue());
}