在Locust中每隔x秒后台运行一个请求

huangapple go评论66阅读模式
英文:

Running a request in background every x seconds using Locust

问题

我尝试使用Locust来进行负载测试,模拟用户在我的客户端应用程序中发出一些HTTP请求。用户自己触发一些HTTP请求,此外,客户端还有一个后台线程,每隔x秒触发一个HTTP请求。

如何在Locust中模拟后台线程?

我尝试了以下方法,它有点有效,但是当测试停止时,“后台线程”似乎没有停止。显然,那个greenlet看不到self._state上的更新。

class MyUser(HttpUser):
    host = 'http://localhost'
    wait_time = between(1, 5)

    def on_start(self):
        gevent.spawn(self._on_background)

    def _on_background(self):
        while self._state != LOCUST_STATE_STOPPING:
            gevent.sleep(10)
            self.client.get('/status')

    @task(1)
    def main_page(self):
        self.client.get('/main')

    @task(2)
    def hello(self):
        self.client.get('/hello')

Note: The code block is provided without translation, as per your request.

<details>
<summary>英文:</summary>

I try to simulate users using my client application with [Locust][1] for load testing. Users fires some http requests by themselves and besides that the client also has a background thread that fires an http request every x seconds.

How can I simulate the background thread in Locust?

I tried the following and it kind of works but the &quot;background thread&quot; is not stopping when a test is stopped. Apparently that greenlet doesn&#39;t see updates on `self._state`.

```python
class MyUser(HttpUser):
    host = &#39;http://localhost&#39;
    wait_time = between(1,5)

    def on_start(self):
        gevent.spawn(self._on_background)

    def _on_background(self):
        while self._state != LOCUST_STATE_STOPPING:
            gevent.sleep(10)
            self.client.get(&#39;/status&#39;)

    @task(1)
    def main_page(self):
        self.client.get(&#39;/main&#39;)

    @task(2)
    def hello(self):
        self.client.get(&#39;/hello&#39;)

答案1

得分: 1

请注意以下翻译:

"Instead of self._state, try checking self.environment.runner.state."

"Also, 10s is quite a long time. Maybe change it so that the loop runs every second and do the request every ten iterations."

英文:

Instead of self._state, try checking self.environment.runner.state.

Also, 10s is quite a long time. Maybe change it so that the loop runs every second and do the request every ten iterations.

huangapple
  • 本文由 发表于 2023年6月26日 18:32:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76555853.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定