Locust性能测试:上手初体验

废话

今天整理之前的有道云笔记,这也是之前弄的~~~

学习资料

官网:https://www.locust.io/
博客园老张:https://www.cnblogs.com/imyalost/p/9758189.html
blibli某机构:https://www.bilibili.com/video/BV16441157rv?from=search&seid=16493161016333916333

正文

基于python的开源性能测试框架

安装: pip install locust

Web页面启动

快速上手1:此处用到的项目是woniusales,指定了被测主机地址

from locust import task, between, HttpUser


# 继承HttpUser,蝗虫将为它模拟的每个用户创建一个此类的实例
class TestIndex(HttpUser):
    # 等待时间 最小1秒(s) 最大5s
    wait_time = between(1, 5)

    # 使用@task来声明任务,只有使用了@task注解的方法才会被蝗虫并发执行
    @task
    def get_index(self):
        # 请求接口
        self.client.get("/woniusales")
        print("访问首页")

    # 每个模拟用户在启动前都会调用改方法
    def on_start(self):
        self.client.get("/")
        print("在每个用户执行前调用")

# 执行脚本:locust -f TestIndex.py --host=htpp://192.168.0.136:8080 ; --host= 需要监控的服务器的ip地址


def start_locust(monitored_machine):
    """
    执行 locust 启动命令,执行完成后,使用浏览器访问:127.0.0.1:8089 进入用户数设置页面
    :param monitored_machine: 被测项目的主机地址
    :return: 无
    """
    import os
    os.system("locust -f {file_path} --host={host}".format(file_path=__file__, host=monitored_machine))


if __name__ == '__main__':
    start_locust("http://192.168.0.136:8080")
# 启动脚本后:浏览器访问 127.0.0.1:8089

image.png

快速上手2: 未指定被测主机地址

from locust import task, between, HttpUser


# 继承HttpUser,蝗虫将为它模拟的每个用户创建一个此类的实例
class TestIndex(HttpUser):
    # 等待时间 最小1秒(s) 最大5s
    wait_time = between(1, 5)

    # 使用@task来声明任务,只有使用了@task注解的方法才会被蝗虫并发执行
    @task
    def get_index(self):
        # 请求接口
        self.client.get("/woniusales")
        print("访问首页")

    # 每个模拟用户在启动前都会调用改方法
    def on_start(self):
        self.client.get("/")
        print("在每个用户执行前调用")

# 执行脚本:locust -f TestIndex.py --host=htpp://192.168.0.136:8080 ; --host= 需要监控的服务器的ip地址


def start_locust():
    """
    执行 locust 启动命令,执行完成后,使用浏览器访问:127.0.0.1:8089 进入用户数设置页面
    :return: 无
    """
    import os
    os.system("locust -f {file_path}".format(file_path=__file__))


if __name__ == '__main__':
    start_locust()

image1.png

Locust面板解析

面板功能

image23.png

New test:点击该按钮可对模拟的总虚拟用户数和每秒启动的虚拟用户数进行编辑;
Statistics:类似于jmeter中Listen的聚合报告;
Charts:测试结果变化趋势的曲线展示图,分别为每秒完成的请求数(RPS)、响应时间、不同时间的虚拟用户数;
Failures:失败请求的展示界面;
Exceptions:异常请求的展示界面;
Download Data:测试数据下载模块, 提供三种类型的CSV格式的下载,分别是:Statistics、responsetime、exceptions;

Statistics报告解释:

image11.png

Type:请求类型,即接口的请求方法;
Name:请求路径;
requests:当前已完成的请求数量;
fails:当前失败的数量;
Median:响应时间的中间值,即50%的响应时间在这个数值范围内,单位为毫秒;
Average:平均响应时间,单位为毫秒;
Min:最小响应时间,单位为毫秒;
Max:最大响应时间,单位为毫秒;
Content Size:所有请求的数据量,单位为字节;
reqs/sec:每秒钟处理请求的数量,即QPS
作者:zy7y
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须在文章页面给出原文链接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/zy7y/p/13576786.html