This article introduces Locust's log management during performance testing, which is an indispensable step in the testing process. If there are any problems in the later stage, a detailed or debuggable log mechanism can provide possible clues when troubleshooting or solving problems.
We use a case to illustrate the usage of logging. locust_file.py is as follows, logging uses modules in the python standard library.
from locust import HttpUser, task, between
import logging as log
class MyUser(HttpUser):
wait_time = between(3,5)
@task
def open_blog(self):
with self.client.get("/13734261/2540530",catch_response=True) as response:
if response.status_code != 200:
response.failure("Threre maybe some issues in the requests.")
log.error("请求执行有错误。")
elif response.elapsed.total_seconds() > 5:
response.failure("Request took too long")
log.error("请求响应超过5秒钟")
log.info("请求执行成功!")
In this way, when Locust is performing performance testing, the command line code can be written as:
locust -f locust_file_chapter13.py --logfile=locust.log
In this way, it will create a file "locust.log" in the current directory, instead of displaying the log in the default console. According to the above locust files, the log samples are as follows:
[2020-12-03 21:18:28,159] xx.local/INFO/locust.main: Starting web interface at http://:8089
[2020-12-03 21:18:28,167] xx.local/INFO/locust.main: Starting Locust 1.1.1
[2020-12-03 21:18:58,420] xx.local/INFO/locust.runners: Hatching and swarming 1 users at the rate 1 users/s (0 users already running)...
[2020-12-03 21:18:58,421] xx.local/INFO/locust.runners: All users hatched: MyUser: 1 (0 already running)
[2020-12-03 21:18:58,941] xx.local/INFO/root: 请求执行成功!
[2020-12-03 21:19:04,164] xx.local/INFO/root: 请求执行成功!
[2020-12-03 21:19:09,266] xx.local/INFO/root: 请求执行成功!
[2020-12-03 21:19:12,799] xx.local/INFO/root: 请求执行成功!
[2020-12-03 21:19:17,171] xx.local/INFO/root: 请求执行成功!
[2020-12-03 21:19:20,612] xx.local/INFO/root: 请求执行成功!
[2020-12-03 21:27:18,787] xx.local/INFO/locust.main: Running teardowns...
[2020-12-03 21:27:18,787] xx.local/INFO/locust.main: Shutting down (exit code 0), bye.
[2020-12-03 21:27:18,787] xx.local/INFO/locust.main: Cleaning up runner...