栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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 的运行情况,内容类似下面的。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/499176.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号