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

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

Running a request in background every x seconds using Locust

问题

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

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

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

  1. class MyUser(HttpUser):
  2. host = 'http://localhost'
  3. wait_time = between(1, 5)
  4. def on_start(self):
  5. gevent.spawn(self._on_background)
  6. def _on_background(self):
  7. while self._state != LOCUST_STATE_STOPPING:
  8. gevent.sleep(10)
  9. self.client.get('/status')
  10. @task(1)
  11. def main_page(self):
  12. self.client.get('/main')
  13. @task(2)
  14. def hello(self):
  15. self.client.get('/hello')
  1. Note: The code block is provided without translation, as per your request.
  2. <details>
  3. <summary>英文:</summary>
  4. 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.
  5. How can I simulate the background thread in Locust?
  6. 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`.
  7. ```python
  8. class MyUser(HttpUser):
  9. host = &#39;http://localhost&#39;
  10. wait_time = between(1,5)
  11. def on_start(self):
  12. gevent.spawn(self._on_background)
  13. def _on_background(self):
  14. while self._state != LOCUST_STATE_STOPPING:
  15. gevent.sleep(10)
  16. self.client.get(&#39;/status&#39;)
  17. @task(1)
  18. def main_page(self):
  19. self.client.get(&#39;/main&#39;)
  20. @task(2)
  21. def hello(self):
  22. 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:

确定