Locust 性能测试 - log 记录每个 request 的运行情况

性能测试时,最好记录一下每个 request 运行情况,方便遇到异常情况分析。Locust 使用 Python’s built in logging framework 来处理 log.

log 配置

--loglevel--logfile 这两个参数用来配置 log 相关的信息:

--logfile 是配置 log 文件的 path
--loglevel 配置 log 的级别 DEBUG/INFO/WARNING/ERROR/CRITICAL. 默认是 INFO

例子:

logging.error(‘error message’) -----记录 error
logging.info(‘info message’) --------记录 info

当然可以根据 request 具体情况记录更多具有参考价值的信息,如 status code, response time, 某字段的值等等。

 import logging as log
 with self.client.post("/", json={
    
    "foo": 42, "bar": None}, catch_response=True) as response:
    if response.status_code != 200:		
        response.failure("Did not get expected status code")
		log.error("Did not get expected status code")
	try:
        if response.json()["status"] != "Success":
            response.failure("Did not get expected value in status")
			log.error('Failed: Did not get expected value in status')
		response.success()
        log.info("Success")	
    except JSONDecodeError:
            response.failure("Response could not be decoded as JSON")
			log.error('Failed: Response could not be decoded as JSON')
    except KeyError:
            response.failure("Response did not contain expected key 'status'")
			log.error("Response did not contain expected key 'status'")

运行时:配置 log

if __name__ == '__main__':
    os.system('locust --config=performance_test/locust-config.conf --logfile=output/log.txt')

locust --config=config folder/locust-config.conf –logfile=output/log.txt --loglevel=INFO

output 下会生成一个 log.txt 文件,记录 每个request 的运行情况,内容类似下面的。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/wumingxiaoyao/article/details/121315596