美文网首页
Locust性能测试(1)

Locust性能测试(1)

作者: 钟微 | 来源:发表于2019-07-23 10:22 被阅读0次

一、安装Locustio模块

pip install locustio
安装好后,查看版本号
pip show locustio


image.png

二、开始测试

# 保存为locust01.py
# coding:utf-8
from locust import HttpLocust, TaskSet, task

# 建一个类BlogDemo(TaskSet),继承TaskSet,该类下面写一些准备请求的行为(访问的接口)
class BlogDemo(TaskSet):
    # 用户行为:打开我的简书首页
    # @task装饰该方法表示为用户行为。
    # 括号里面参数表示该行为挑选执行的权重,数值越大,执行频率越高,不设置默认是1
    @task(1)
    def open_blog(self):
        # 定义requests的请求头
        header = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.80 Safari/537.36"}

        # self.client调用get和post方法,跟requests是一样的
        # requests.get 对应client.get
        # requests.post 对应client.post
        r = self.client.get("/u/125ddf84ee1d",  headers=header, verify=False)
        print(r.status_code)
        assert r.status_code == 200

"""
WebsiteUser()类用于设置性能测试。
task_set :指向一个定义了的用户行为类。
min_wait :用户执行任务之间等待时间的下界,单位:毫秒。
max_wait :用户执行任务之间等待时间的上界,单位:毫秒。
"""
class websitUser(HttpLocust):
    task_set = BlogDemo
    min_wait = 3000  # 单位毫秒
    max_wait = 6000  # 单位毫秒

if __name__ == "__main__":
    import os
    os.system("locust -f locust01.py --host=https://www.jianshu.com")
    # -f 参数是指定运行的脚本
    # --host是指定运行项目的host地址,
    # 这里用的https://www.jianshu.com,
    # 代码里面get访问的是"/u/125ddf84ee1d",拼接起来就是完整地址了

三、运行结果

[2019-07-23 09:37:29,214] DESKTOP-RV9FIIG/INFO/locust.main: Starting web monitor at *:8089
[2019-07-23 09:37:29,214] DESKTOP-RV9FIIG/INFO/locust.main: Starting Locust 0.11.0

8089是该服务启动的端口号。
由于是在本机上搭建的locust,所以可以直接在浏览器输入http://localhost:8089/打开,
如果是在其它机器上搭建的locust服务,那就通过http://其它机器IP:8089/打开

四、效果展示

image.png

Number of users to simulate 设置虚拟用户总数
Hatch rate (users spawned/second) 每秒启动虚拟用户数
点击Start swarming 开始运行性能测试
设置虚拟用户数20000,每秒启动2000个用户,点击Start swarming 开始运行

五、结果分析

image.png

Type:请求类型;
Name:请求路径;
requests:当前请求的数量;
fails:当前请求失败的数量;
Median:中间值,单位毫秒,一般服务器响应时间低于该值,而另一半高于该值;
Average:所有请求的平均响应时间,毫秒;
Min:请求的最小的服务器响应时间,毫秒;
Max:请求的最大服务器响应时间,毫秒;
Content Size:单个请求的大小,单位字节;
reqs/sec:每秒钟请求的个数。

Charts栏三个图标分别是:
吞吐量/每秒响应事务数(rps)实时统计
平均响应时间/平均事务数实时统计
虚拟用户数运行

下图可表明:当前系统每秒可以处理467.5个请求


total requests per second.png

响应时间


response times.png

当前虚拟用户数为3417


Number of user.png

点stop可以停止测试


stop.png
点击new test可重新设置用户数
New test.png

相关文章

  • locust库浅谈

    locust是python的性能测试库。 1、安装locust:pip install locust; 2、安装成...

  • 性能测试工具Locust使用指南(三)

    前文目录:性能测试工具Locust使用指南(一)性能测试工具Locust使用指南(二) 分布式运行Locust 如...

  • Locust—python压力测试工具

    1、Locust与其他性能测试工具对比 优势:1、Locust 完全基本 Python 编程语言,采用 Pure ...

  • 利用python-locust实现接口自动化压测

    Locust简介 Locust[https://locust.io/]为一款通过编写python脚本来执行性能测试...

  • Locust性能测试(1)

    一、安装Locustio模块 pip install locustio安装好后,查看版本号pip show loc...

  • 性能测试工具Locust

    性能测试工具Locust | iCourt技术 Locust介绍 性能测试工具市面上有很多,除了我们耳熟能详的Lo...

  • 初识locust

    什么是locust locust是一种易于使用、可编写脚本且可扩展的性能测试工具。locust具备5大特点:1.用...

  • 性能测试工具

    1.性能测试工具: 1.LoadRunner 2.JMeter 3.Locust 4.Molotov https:...

  • 2019-01-11

    Locust性能测试1-环境准备与基本使用 前言 提到性能测试,大部分小伙伴想到的就是LR和jmeter这种工具,...

  • 性能测试工具Locust和JMeter比较

    Apache JMeter™和Locust都是是最受欢迎的性能测试工具。 JMeter和Locust - 简介 J...

网友评论

      本文标题:Locust性能测试(1)

      本文链接:https://www.haomeiwen.com/subject/srpwlctx.html