gpt4 book ai didi

locust - headless 浏览器和 locust.io

转载 作者:行者123 更新时间:2023-12-01 22:26:53 28 4
gpt4 key购买 nike

是否可以将 headless 浏览器与 Locust 集成?我需要我的负载测试来处理在每次页面加载时触发额外请求的客户端脚本。

最佳答案

这是一个老问题,但我现在遇到了它,并在“realbrowserlocusts”(https://github.com/nickboucart/realbrowserlocusts)中找到了解决方案 - 它使用 selenium 添加了“对 Locust.io 负载测试的真实浏览器支持”。

如果您使用其中一个类(FirefoxLocust、ChromeLocust、PhantomJSLocust)而不是 HttpLocust 作为您的 Locust 用户类

class WebsiteUser(HeadlessChromeLocust):

然后在您的 TaskSet 中,self.client 成为 selenium WebDriver 的一个实例


我的一个缺点是 webdriver(不像 HttpLocust 中的内置客户端)不知道“host”,它强制在 TaskSet 中使用绝对 URL 而不是相对 URL,并且在以下情况下使用相对 URL 真的很方便使用不同的环境(本地、开发、暂存、生产等)。但是有一个简单的解决方法:从 realbrowserlocusts 的蝗虫之一继承并将“主机”传递给 WebDriver 实例:

from locust import TaskSet, task, between
from realbrowserlocusts import HeadlessChromeLocust


class UserBehaviour(TaskSet):
@task(1)
def some_action(self):
# self.client is selenium WebDriver instance
self.client.get(self.client.base_host + "/relative/url")
# and then for inst. using selenium methods:
self.client.find_element_by_name("form-name").send_keys("your text")
# etc.


class ChromeLocustWithHost(HeadlessChromeLocust):
def __init__(self):
super(ChromeLocustWithHost, self).__init__()
self.client.base_host = self.host


class WebsiteUser(ChromeLocustWithHost):
screen_width = 1200
screen_height = 1200
task_set = UserBehaviour
wait_time = between(5, 9)

============

2020 年 9 月 5 日更新:

我在 2020 年 3 月发布了这个解决方案,当时 Locust 的主要版本为 0。从那时起,在 2020 年 5 月,他们发布了 1.0.0 版,其中进行了一些向后不兼容的更改(其中之一 - 将 StopLocust 重命名为 StopUser) . realbrowserlocusts 有一段时间没有更新了,还没有更新到可以与 locust >=1 一起使用。

虽然有一个解决方法。当 locust v1.0.0 发布时,以前的版本以新名称发布 - locustio,最新版本为 0.14.6,因此如果您安装“locustio== 0.14.6”(或“locustio<1”),那么使用 realbrowserlocusts 的解决方案仍然有效(我刚刚检查过)。 (参见 https://github.com/nickboucart/realbrowserlocusts/issues/13)。

你必须限制 locustio 的一个版本,因为它拒绝在没有它的情况下安装:

pip install locustio
...
ERROR: Command errored out with exit status 1:
...
**** Locust package has moved from 'locustio' to 'locust'.
Please update your reference
(or pin your version to 0.14.6 if you dont want to update to 1.0)

关于locust - headless 浏览器和 locust.io,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32890209/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com