【Linux】【压测】关于python实现性能自动化工具之Locust

关于python+locust系列:

【Linux】【压测】关于python实现性能自动化工具之Locus: https://www.cnblogs.com/haochuang/articles/13230602.html
【Linux】【压测】基于python Locust库实现自动化压测实践: https://www.cnblogs.com/haochuang/p/13231445.html


 

一、思考❓❔

1.什么是性能自动化测试?

  • 性能
    • 系统负载能力
    • 超负荷运行下的稳定性
    • 系统瓶颈
  • 自动化测试
    • 使用程序代替手工
    • 提升测试效率
  • 性能自动化
    • 使用代码模拟大批量用户
    • 让用户并发请求
    • 多页面多用户并发请求
    • 采集参数,统计系统负载能力
    • 生成报告

2.Python中的性能自动化测试库?

locust库

  • 使用Python
    • 使用代码来创建批量用户
  • 分布式
    • 可以在多台服务器上,进行分布式性能测试
    • 可伸缩性强
  • 稳定、应用广泛
    • 经得住各种场景下的考验
    • 基于web ui界面展示测试详情
    • 能测任何系统


二、基础操作🔨🔨

 

1.安装locust

  • 使用官方pypi源来安装
    • pip install locustio
  • 使用豆瓣pypi源来安装(推荐)
    • pip install -i https://pypi.douban.com/simple locustio

安装成功之后,在cmd控制台将会新增一条命令,可输入如下命令查看:

locust --help

2.基本用法

  • 在项目根目录下创建locustfile.py文件

    from locust import Locust, TaskSet, task
    
    
    class MyTasks(TaskSet):
        """
        创建测试任务类,需要继承TaskSet
        可以添加多个测试任务
        """
        # 每个测试任务,往往会以实例方法的形式来呈现
        # 同时需要使用task装饰器来装饰测试任务
        @task
        def one_task(self):
            print("执行一个伟大的测试任务!")
    
    
    class RunTasks(Locust):
        """
        创建运行测试类,需要继承Locust父类
        """
        task_set = MyTasks   # 指定测试任务类,使用task_set覆盖父类的类属性
        min_wait = 2000      # 指定启动任务间隔的时间范围(单位毫秒):2~5秒之间
        max_wait = 5000	     # 使用min_wait、max_wait覆盖父类的类属性
    
    
  • 执行性能测试

    # 打开Pycharm Terminal控制台
    # 运行如下命令:
    locust
    

     

  • 当前版本下,可以指定文件路径方式运行,如下:
  • locust --host=http://localhost -f .\test_load.py
  • 运行之后,控制台会在本地监听一个端口

a. 程序文件要命名为 locustfile.py才行

b. 假如命名为one_example.py,则需要这样运行

locust -f one_example.py

  • 在浏览器打开UI设置界面

    • 默认使用localhost:8089打开



三、综合案例演练🔨🔨

 

1.编写自动化测试脚本

  • 在项目根目录下创建test_load.py文件
from locust import HttpLocust, TaskSet, task


class AdminLoadTest(TaskSet):
    """
    创建后台管理站点压测类,需要继承TaskSet
    可以添加多个测试任务
    """
    def login(self):
        """
        登录实例方法
        :return: 
        """
        self.client.post("http://localhost:8088/users/login/",
                         {"user_account": "admin", "password": "123456"})

    def logout(self):
        """
        登出实例方法
        :return:
        """
        self.client.get("http://localhost:8088/users/logout/")

    def on_start(self):
        """
        当任何一个task调度执行之前,
        on_start实例方法会被调用
        先登录
        :return:
        """
        self.login()

    def on_stop(self):
        """
        当任何一个task调度执行之后,
        on_stop实例方法会被调用
        后登出
        :return:
        """
        self.logout()

    @task
    def admin_index(self):
        """
        对后台主页进行压测
        :return:
        """
        self.client.get("http://localhost:8088/admin/")


class RunLoadTests(HttpLocust):
    """
    创建运行压测类
    """
    task_set = AdminLoadTest

2.使用命令行运行

  • 打开Pycharm Terminal控制台,运行如下命令:

注意:--host参数指定http主机地址,-f参数指定压测程序文件名

3.打开web ui界面进行配置

设置并发用户数为10,每5秒创建一个用户

压测过程截图

美轮美奂的压测报告

压测失败详情

下载压测统计数据

下载的压测统计数据csv文件

六、总结

  • locust做压测功能极其强大
  • 支持分布式部署
  • 提供的接口简单
  • 压测代码非常容易编写
  • 提供UI界面来配置
  • 美观、详细的图表统计
 
FAQ:
如果安装失败,遇到如下问题,可这样解决。
问题:
C:\Python37\Scripts\pip.exe install locustio
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting locustio
  Using cached https://mirrors.aliyun.com/pypi/packages/bd/50/c4bafccbb1dd66a1968f3f6d96933889b338475f4a8e45a7785f7f275fc5/locustio-0.999.tar.gz
Building wheels for collected packages: locustio
  Building wheel for locustio (setup.py) ... error
  ERROR: Command errored out with exit status 1:
   command: 'c:\python37\python.exe' -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'C:\\Users\\Ben\\AppData\\Local\\Temp\\pip-install-hpyccbru\\locustio\\setup.py'"'"'; __file__='"'"'C:\\Users\\Ben\\AppData\\Local\\Temp\\pip-install-hpyccbru\\locustio\\setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(__file__);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, __file__, '"'"'exec'"'"'))' bdist_wheel -d 'C:\Users\Ben\AppData\Local\Temp\pip-wheel-xjwtnqm_' --python-tag cp37
       cwd: C:\Users\Ben\AppData\Local\Temp\pip-install-hpyccbru\locustio\
  Complete output (89 lines):
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib
  creating build\lib\locust
  copying locust\argument_parser.py -> build\lib\locust
  copying locust\clients.py -> build\lib\locust
  copying locust\env.py -> build\lib\locust
  copying locust\event.py -> build\lib\locust
  copying locust\exception.py -> build\lib\locust
  copying locust\log.py -> build\lib\locust
  copying locust\main.py -> build\lib\locust
  copying locust\runners.py -> build\lib\locust
  copying locust\stats.py -> build\lib\locust
  copying locust\web.py -> build\lib\locust
  copying locust\__init__.py -> build\lib\locust
  copying locust\__main__.py -> build\lib\locust
  creating build\lib\locust\contrib
  copying locust\contrib\fasthttp.py -> build\lib\locust\contrib
  copying locust\contrib\__init__.py -> build\lib\locust\contrib
  creating build\lib\locust\rpc
  copying locust\rpc\protocol.py -> build\lib\locust\rpc
  copying locust\rpc\zmqrpc.py -> build\lib\locust\rpc
  copying locust\rpc\__init__.py -> build\lib\locust\rpc
  creating build\lib\locust\test
  copying locust\test\mock_locustfile.py -> build\lib\locust\test
  copying locust\test\mock_logging.py -> build\lib\locust\test
  copying locust\test\testcases.py -> build\lib\locust\test
  copying locust\test\test_client.py -> build\lib\locust\test
  copying locust\test\test_fasthttp.py -> build\lib\locust\test
  copying locust\test\test_locust_class.py -> build\lib\locust\test
  copying locust\test\test_log.py -> build\lib\locust\test
  copying locust\test\test_main.py -> build\lib\locust\test
  copying locust\test\test_old_wait_api.py -> build\lib\locust\test
  copying locust\test\test_parser.py -> build\lib\locust\test
  copying locust\test\test_runners.py -> build\lib\locust\test
  copying locust\test\test_sequential_taskset.py -> build\lib\locust\test
  copying locust\test\test_stats.py -> build\lib\locust\test
  copying locust\test\test_tags.py -> build\lib\locust\test
  copying locust\test\test_taskratio.py -> build\lib\locust\test
  copying locust\test\test_util.py -> build\lib\locust\test
  copying locust\test\test_wait_time.py -> build\lib\locust\test
  copying locust\test\test_web.py -> build\lib\locust\test
  copying locust\test\test_zmqrpc.py -> build\lib\locust\test
  copying locust\test\util.py -> build\lib\locust\test
  copying locust\test\__init__.py -> build\lib\locust\test
  creating build\lib\locust\user
  copying locust\user\inspectuser.py -> build\lib\locust\user
  copying locust\user\sequential_taskset.py -> build\lib\locust\user
  copying locust\user\task.py -> build\lib\locust\user
  copying locust\user\users.py -> build\lib\locust\user
  copying locust\user\wait_time.py -> build\lib\locust\user
  copying locust\user\__init__.py -> build\lib\locust\user
  creating build\lib\locust\util
  copying locust\util\cache.py -> build\lib\locust\util
  copying locust\util\deprecation.py -> build\lib\locust\util
  copying locust\util\exception_handler.py -> build\lib\locust\util
  copying locust\util\rounding.py -> build\lib\locust\util
  copying locust\util\timespan.py -> build\lib\locust\util
  copying locust\util\__init__.py -> build\lib\locust\util
  running egg_info
  writing locustio.egg-info\PKG-INFO
  writing dependency_links to locustio.egg-info\dependency_links.txt
  writing entry points to locustio.egg-info\entry_points.txt
  writing top-level names to locustio.egg-info\top_level.txt
  reading manifest file 'locustio.egg-info\SOURCES.txt'
  reading manifest template 'MANIFEST.in'
  writing manifest file 'locustio.egg-info\SOURCES.txt'
  creating build\lib\locust\static
  copying locust\static\chart.js -> build\lib\locust\static
  copying locust\static\echarts.common.min.js -> build\lib\locust\static
  copying locust\static\jquery-1.11.3.min.js -> build\lib\locust\static
  copying locust\static\jquery.jqote2.min.js -> build\lib\locust\static
  copying locust\static\jquery.tools.min.js -> build\lib\locust\static
  copying locust\static\locust.js -> build\lib\locust\static
  copying locust\static\style.css -> build\lib\locust\static
  copying locust\static\vintage.js -> build\lib\locust\static
  creating build\lib\locust\static\img
  copying locust\static\img\favicon.ico -> build\lib\locust\static\img
  copying locust\static\img\logo.png -> build\lib\locust\static\img
  copying locust\static\img\top_bg.png -> build\lib\locust\static\img
  creating build\lib\locust\templates
  copying locust\templates\index.html -> build\lib\locust\templates
  installing to build\bdist.win-amd64\wheel
  running install

遇到上面这种报错,请更换命令安装:

C:\Python37\Scripts\pip.exe install locust

 

安装完成之后,可使用 locust --help 查看其基本命令:
PS D:\CODE\CODE_VSCode\HAO_VSCcode\HAO_AutoTest\LOCUST\HAO_Locust\LocustDemo> locust --help
Usage: locust [OPTIONS] [UserClass ...]

Common options:
  -h, --help            show this help message and exit
  -f LOCUSTFILE, --locustfile LOCUSTFILE
                        Python module file to import, e.g. '../other.py'.
                        Default: locustfile
  --config CONFIG       Config file path
  -H HOST, --host HOST  Host to load test in the following format:
                        http://10.21.32.33
  -u NUM_USERS, --users NUM_USERS
                        Number of concurrent Locust users. Only used together
                        with --headless
  -r HATCH_RATE, --hatch-rate HATCH_RATE
                        The rate per second in which users are spawned. Only
                        used together with --headless
  -t RUN_TIME, --run-time RUN_TIME
                        Stop after the specified amount of time, e.g. (300s,
                        20m, 3h, 1h30m, etc.). Only used together with
                        --headless
  -l, --list            Show list of possible User classes and exit

Web UI options:
  --web-host WEB_HOST   Host to bind the web interface to. Defaults to '*'
                        (all interfaces)
  --web-port WEB_PORT, -P WEB_PORT
                        Port on which to run web host
  --headless            Disable the web interface, and instead start the load
                        test immediately. Requires -u and -t to be specified.
  --web-auth WEB_AUTH   Turn on Basic Auth for the web interface. Should be
                        supplied in the following format: username:password
  --tls-cert TLS_CERT   Optional path to TLS certificate to use to serve over
                        HTTPS
  --tls-key TLS_KEY     Optional path to TLS private key to use to serve over
                        HTTPS

Master options:
  Options for running a Locust Master node when running Locust distributed. A Master node need Worker nodes that connect to it before it can run load tests.

  --master              Set locust to run in distributed mode with this
                        process as master
  --master-bind-host MASTER_BIND_HOST
                        Interfaces (hostname, ip) that locust master should
                        bind to. Only used when running with --master.
                        Defaults to * (all available interfaces).
  --master-bind-port MASTER_BIND_PORT
                        Port that locust master should bind to. Only used when
                        running with --master. Defaults to 5557.
  --expect-workers EXPECT_WORKERS
                        How many workers master should expect to connect
                        before starting the test (only when --headless used).

Worker options:

  Options for running a Locust Worker node when running Locust distributed.
  Only the LOCUSTFILE (-f option) need to be specified when starting a Worker, since other options such as -u, -r, -t are specified on the Master node.

  --worker              Set locust to run in distributed mode with this
                        process as worker
  --master-host MASTER_NODE_HOST
                        Host or IP address of locust master for distributed
                        load testing. Only used when running with --worker.
                        Defaults to 127.0.0.1.
  --master-port MASTER_NODE_PORT
                        The port to connect to that is used by the locust
                        master for distributed load testing. Only used when
                        running with --worker. Defaults to 5557.

Tag options:
  Locust tasks can be tagged using the @tag decorator. These options let specify which tasks to include or exclude during a test.

  -T [TAG [TAG ...]], --tags [TAG [TAG ...]]
                        List of tags to include in the test, so only tasks
                        with any matching tags will be executed
  -E [TAG [TAG ...]], --exclude-tags [TAG [TAG ...]]
                        List of tags to exclude from the test, so only tasks
                        with no matching tags will be executed

Request statistics options:
  --csv CSV_PREFIX      Store current request stats to files in CSV format.
                        Setting this option will generate three files:
                        [CSV_PREFIX]_stats.csv, [CSV_PREFIX]_stats_history.csv
                        and [CSV_PREFIX]_failures.csv
  --csv-full-history    Store each stats entry in CSV format to
                        _stats_history.csv file
  --print-stats         Print stats in the console
  --only-summary        Only print the summary stats
  --reset-stats         Reset statistics once hatching has been completed.
                        Should be set on both master and workers when running
                        in distributed mode

Logging options:
  --skip-log-setup      Disable Locust's logging setup. Instead, the
                        configuration is provided by the Locust test or Python
                        defaults.
  --loglevel LOGLEVEL, -L LOGLEVEL
                        Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.
                        Default is INFO.
  --logfile LOGFILE     Path to log file. If not set, log will go to
                        stdout/stderr

Step load options:
  --step-load           Enable Step Load mode to monitor how performance
                        metrics varies when user load increases. Requires
                        --step-users and --step-time to be specified.
  --step-users STEP_USERS
                        User count to increase by step in Step Load mode. Only
                        used together with --step-load
  --step-time STEP_TIME
                        Step duration in Step Load mode, e.g. (300s, 20m, 3h,
                        1h30m, etc.). Only used together with --step-load

Other options:
  --show-task-ratio     Print table of the User classes' task execution ratio
  --show-task-ratio-json
                        Print json data of the User classes' task execution
                        ratio
  --version, -V         Show program's version number and exit
  --exit-code-on-error EXIT_CODE_ON_ERROR
                        Sets the process exit code to use when a test result
                        contain any failure or error
  -s STOP_TIMEOUT, --stop-timeout STOP_TIMEOUT
                        Number of seconds to wait for a simulated user to
                        complete any executing task before exiting. Default is
                        to terminate immediately. This parameter only needs to
                        be specified for the master process when running
                        Locust distributed.
                        _stats_history.csv file
  --print-stats         Print stats in the console
  --only-summary        Only print the summary stats
  --reset-stats         Reset statistics once hatching has been completed.
                        Should be set on both master and workers when running
                        in distributed mode

Logging options:
  --skip-log-setup      Disable Locust's logging setup. Instead, the
                        configuration is provided by the Locust test or Python
                        defaults.
  --loglevel LOGLEVEL, -L LOGLEVEL
                        Choose between DEBUG/INFO/WARNING/ERROR/CRITICAL.
                        Default is INFO.
  --logfile LOGFILE     Path to log file. If not set, log will go to
                        stdout/stderr

Step load options:
  --step-load           Enable Step Load mode to monitor how performance
                        metrics varies when user load increases. Requires
                        --step-users and --step-time to be specified.
  --step-users STEP_USERS
                        User count to increase by step in Step Load mode. Only
                        used together with --step-load
  --step-time STEP_TIME
                        Step duration in Step Load mode, e.g. (300s, 20m, 3h,
                        1h30m, etc.). Only used together with --step-load

Other options:
  --show-task-ratio     Print table of the User classes' task execution ratio
  --show-task-ratio-json
                        Print json data of the User classes' task execution
                        ratio
  --version, -V         Show program's version number and exit
  --exit-code-on-error EXIT_CODE_ON_ERROR
                        Sets the process exit code to use when a test result
                        contain any failure or error
  -s STOP_TIMEOUT, --stop-timeout STOP_TIMEOUT
                        Number of seconds to wait for a simulated user to
                        complete any executing task before exiting. Default is
                        to terminate immediately. This parameter only needs to
                        be specified for the master process when running
                        Locust distributed.

User classes:
  UserClass             Optionally specify which User classes that should be
                        used (available User classes can be listed with -l or
                        --list)

 

 
待续...
posted @ 2020-07-03 15:16  念槐聚  阅读(467)  评论(0编辑  收藏  举报