英文:
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 "background thread" is not stopping when a test is stopped. Apparently that greenlet doesn't see updates on `self._state`.
```python
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')
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论