Locust-python 压测工具初始及记一次测试记录

本贴最后更新于 1366 天前,其中的信息可能已经东海扬尘

第一部分 Python3中性能测试工具Locust安装使用

概述

Locust安装使用

安装

  1. python3中 --> pip3 install locust
  2. 验证是否安装成功 --> 终端中输入locust --help 显示帮助信息表示安装成功
  3. locust官网 --> https://www.locust.io/
  4. 大并发量测试时,建议在linux系统下进行

启动

1. 终端中 --> 进入到代码目录:

locust -f xxxx.py --host=xxxxx.com

2. 通过浏览器访问:http://localhost:8089(Locust启动网络监控器,默认端口号为:8089)

3. 模式【no-web模式运行启动】

3.1 终端中 --> 进入代码目录:

locust -f xxoo.py  --no-web -c10  -r2 -t 1m

--no-web 表示不使用web界面运行测试
-c 设置虚拟用户数
-r 设置每秒启动虚拟用户数
-t 设置运行时间

==no-web模式运行将测试结果保存到当前.py目录中:locust -f xxoo.py --csv=起一个名字==

3.2 例如:

locust -f test3.py --csv=foobar --no-web -c2 -t10s

4. 分布式压测


第二部分:记一次测试记录

最近应业务需要,在做压力测试,在测试工具的选型上,尝试了Loadrunner、Jmeter、Locust

后来对比后有如下结论:

jmeter和locust对比

image.png

  1. 通过对比,明显发现在相近的request量下,jmeter的各项指标均大幅度低于locust压测的结果,说明在单就工具本身的压测能力上来说,locust > jmeter

locust测试:某业务接口测试环境自身代码优化前后对比

image.png
说明(某业务接口-test_env):

Locust脚本示例

""" 
@author:  xjy
@file:    locust_study.py 
@date:    2019/03/03 15:22:34
@email:   
@IDE:     PyCharm 
@commitLog:
    - 2019/3/3:

# code is far away from bugs with the god animal protecting
    I love animals. They taste delicious.
              ┏┓      ┏┓
            ┏┛┻━━━┛┻┓
            ┃      ☃      ┃
            ┃  ┳┛  ┗┳  ┃
            ┃      ┻      ┃
            ┗━┓      ┏━┛
                ┃      ┗━━━┓
                ┃  神兽保佑    ┣┓
                ┃ 永无BUG!   ┏┛
                ┗┓┓┏━┳┓┏┛
                  ┃┫┫  ┃┫┫
                  ┗┻┛  ┗┻┛ 
"""
import logging
from locust import HttpLocust, TaskSet, task


# 去除警告用的,https下面使用的是verify=False
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


class WebsitTasks(TaskSet):
    def on_start(self):  # 初始化的工作,每个locust用户开始做的第一件事
        logging.info('--- User Start ---')

    @task(1) # 这个装饰器的意思是:给每个人物设置权重,比如这个函数执行的权重就是1,如果下面是2,那么下面的函数就会更多的被执行
    def enter_classroom(self):
        # www环境
        # with self.client.get('yyyyyy', catch_response=True) as response:

        # pro环境
        with self.client.get('yyyyy', catch_response=True,verify=False) as response:
            if response.status_code == 200:
                response.success()
                print('-' * 50)
                logging.info('返回值状态码为:%s' % response.status_code)
                print('-' * 50)
            else:
                print('*' * 50)
                response.failure('GetActConfig[Failed!]')
                logging.error('错误请求,状态码为:', response.status_code)
                print('*' * 50)

    # @task(1)
    # def yunlu(self):
    #     payload = {
    #         'page': 1,
    #         'per_page': 20
    #     }
    #     with self.client.get('/api/v1/mall/index', data=payload, catch_response=True) as response:
    #         if response.status_code == 200:
    #             response.success()
    #         else:
    #             response.failure('GetActConfig[Failed!]')


class WebsiteUser(HttpLocust):
    # pro环境
    host = 'xxxxxxxxxx'  # 被测系统的host,在终端中启动locust时没有指定--host参数时才会用到

    # www环境
    # host = 'xxxxxxxxx'  # 被测系统的host,在终端中启动locust时没有指定--host参数时才会用到

   
    task_set = WebsitTasks  # TaskSet类,该类定义用户人物信息,必填。这里就是:WebsiteTasks类名,因为该类集成TaskSet
    min_wait = 0  # 每个用户执行两个任务间隔时间的上下限(毫秒),具体数值在上下限中随机取值,若不指定默认间隔时间固定为1秒
    max_wait = 0


import os

if __name__ == '__main__':
    # os.system('locust -f locust_study.py --csv=result --no-web -c2000 -r2000 -t40s --loglevel=INFO --logfile=test.log')

    # 2000用户/2000/120s
    os.system('locust -f locust_study.py --csv=result --no-web -c2000 -r2000 -t120s --loglevel=INFO --logfile=test.log')
    # os.system('locust -f locust_study.py --csv=result --no-web -c10 -r10 -t5s --loglevel=INFO --logfile=test.log')
    # os.system('locust -f locust_study.py')

4 回帖
请输入回帖内容 ...
  • huahua

    不语大佬,666!

  • huixie

    不语大佬666,素质三连

  • superman

    根本没写到具体区别,搬运工你好

  • 867450261

    @不语同学,你好!你代码里面实现的log打印实际上跟print没有区别吧。我本来自己定义了一个log日志,但是log打印的时候发现没有打印到指定的某个目录下。无论是loggin还是print,都是输出到locust后面跟的--logfile=test.log的test.log这个文件里面。