Locust 向其他用户界面报告 /stats/requests 数据
Locust report /stats/requests data to other user interface
我发现 Locust UI 会从 http://my-locust-server/stats/requests
端点获取数据,并绘制图表。例如Total request per Second
、Response Time
和 Number of Users
。
我想将 /stats/requests
数据发送到另一台服务器以进行绘图。如何在 locustfile.py
中做到这一点?
以下代码无效,仅供演示。
from locust import HttpUser, task, events
import requests
class User(HttpUser):
@task
def scenario_01(self):
self.client.get('/some_endpoint')
# ==========
# following codes won't work, will block the whole locust.
# ==========
import time
starttime = time.time()
while True:
r = requests.get('http://my-locust-server/stats/requests')
payload = r.json()
requests.post('http://another-server-for-plotting', json=payload)
time.sleep(5.0 - ((time.time() - starttime) % 5.0))
现在我能想到的是运行另一个程序来获取和报告数据。但是如何在 locustfile.py
.
中做到这一点
谢谢!
导入此模块时,while True
代码将被计算,并且 运行 永远。如果您希望在后台循环到 运行,您可以使用 python threading 库。将那个 while 循环放在一个可调用函数中,并将该函数传递给一个新的 Thread 对象并在导入时启动线程。
from locust import HttpUser, task, events
import requests
class User(HttpUser):
@task
def scenario_01(self):
self.client.get('/some_endpoint')
def backgroundloop():
while True:
import time
starttime = time.time()
r = requests.get('http://my-locust-server/stats/requests')
payload = r.json()
requests.post('http://another-server-for-plotting', json=payload)
time.sleep(5.0 - ((time.time() - starttime) % 5.0))
import threading
t = threading.Thread(target=backgroundloop)
t.start()
或者,您可以使用 Locust 的 Event Hooks to send data off directly without getting it from the web route. If you're running in distributed mode, worker_report
可能是最好的。不完整的例子:
from locust import events
import requests
@events.worker_report.add_listener
def send_data_off_somewhere(client_id, data):
requests.post('http://another-server-for-plotting', json=data)
locust-plugins 有关于如何使用事件的好例子,甚至有一个预建的侦听器将数据直接发送到时间刻度数据库。
我发现 Locust UI 会从 http://my-locust-server/stats/requests
端点获取数据,并绘制图表。例如Total request per Second
、Response Time
和 Number of Users
。
我想将 /stats/requests
数据发送到另一台服务器以进行绘图。如何在 locustfile.py
中做到这一点?
以下代码无效,仅供演示。
from locust import HttpUser, task, events
import requests
class User(HttpUser):
@task
def scenario_01(self):
self.client.get('/some_endpoint')
# ==========
# following codes won't work, will block the whole locust.
# ==========
import time
starttime = time.time()
while True:
r = requests.get('http://my-locust-server/stats/requests')
payload = r.json()
requests.post('http://another-server-for-plotting', json=payload)
time.sleep(5.0 - ((time.time() - starttime) % 5.0))
现在我能想到的是运行另一个程序来获取和报告数据。但是如何在 locustfile.py
.
谢谢!
导入此模块时,while True
代码将被计算,并且 运行 永远。如果您希望在后台循环到 运行,您可以使用 python threading 库。将那个 while 循环放在一个可调用函数中,并将该函数传递给一个新的 Thread 对象并在导入时启动线程。
from locust import HttpUser, task, events
import requests
class User(HttpUser):
@task
def scenario_01(self):
self.client.get('/some_endpoint')
def backgroundloop():
while True:
import time
starttime = time.time()
r = requests.get('http://my-locust-server/stats/requests')
payload = r.json()
requests.post('http://another-server-for-plotting', json=payload)
time.sleep(5.0 - ((time.time() - starttime) % 5.0))
import threading
t = threading.Thread(target=backgroundloop)
t.start()
或者,您可以使用 Locust 的 Event Hooks to send data off directly without getting it from the web route. If you're running in distributed mode, worker_report
可能是最好的。不完整的例子:
from locust import events
import requests
@events.worker_report.add_listener
def send_data_off_somewhere(client_id, data):
requests.post('http://another-server-for-plotting', json=data)
locust-plugins 有关于如何使用事件的好例子,甚至有一个预建的侦听器将数据直接发送到时间刻度数据库。