locust压测 学习使用笔记

记录学习使用locust的过程,接触过程中,也查阅了网上的现有资料,发现有些资料有些过时,使用过程中也踩了些坑。

之前参考了一些代码,发现代码执行也有点问题。所以,把学习过程做个记录。

当然,本人才疏学浅,也刚刚接触,难免会有错误,欢迎指正。共同进步。

1.安装过程

安装过程参考了以下地址:

https://pypi.org/project/locust/
https://www.cnblogs.com/zhupeijun0909/p/15245668.html
spyder使用locust参考以下安装:https://www.cnblogs.com/mahema/p/13649770.html

其他略过,碰到坑了,但是时间有点久了,也没有记笔记

执行下locust --help,看到不报错,就是ok啦。

看下版本信息 locust --v,我用的是locust 2.5.1

E:\4.自动化工具\locust_study>locust --help

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
.....
E:\4.自动化工具\locust_study>locust --v

locust 2.5.1

 

2.使用

2.1先写一个简单代码,新建一个文件locustdemo.py

# -*- coding: utf-8 -*-


"""
Created on Wed Feb  9 17:01:10 2022

@author:

测试通过 
"""

from locust import HttpUser, TaskSet, task


class UserBehavior(TaskSet):
    "Locust任务集,定义每个lucost的行为"
    @task(1)  # 任务的权重为1,如果有多个任务,可以将权重值定义成不同的值,
    def get_root(self):
        "模拟发送数据"
        response = self.client.get('/s?wd=hello', name='get_root')
        if not response.ok:
            print(response.text)
            response.failure('Got wrong response')

    @task(2)  # 任务的权重为2,如果有多个任务,可以将权重值定义成不同的值,
    def get_root2(self):
        "模拟发送数据"
        response = self.client.get('/s?wd=hello2', name='get_root2')
        if not response.ok:
            print(response.text)
            response.failure('Got wrong response')

class TestLocust(HttpUser):
    """自定义Locust类,可以设置Locust的参数。"""
    tasks = [UserBehavior]
    host = "https://www.baidu.com"  # 被测服务器地址
    min_wait = 5000
    # 最小等待时间,即至少等待多少秒后Locust选择执行一个任务。

    max_wait = 9000
    # 最大等待时间,即至多等待多少秒后Locust选择执行一个任务。

2.2 在cmd窗口执行下:

E:\4.自动化工具\locust_study>locust -f locustdemo.py

2.3 到页面进行查看执行结果

http://localhost:8089/

测试结果数据如下图所示:

TypeName# Requests# FailsMedian (ms)90%ile (ms)Average (ms)Min (ms)Max (ms)Average size (bytes)Current RPSCurrent Failures/s
GET get_root 361 5 43 52 45 14 259 1472 7.1 0.2
GET get_root2 741 7 43 53 45 16 144 1473 14.9 0.1
  Aggregated 1102 12 43 53 45 14 259 1473 22 0.3

  • 1.Type:请求类型;
  • 2.Name:请求路径;
  • 3.Requests:当前请求的数量;
  • 4.Fails:当前请求失败的数量;
  • 5.Median:中间值,单位毫秒,一半服务器响应时间低于该值,而另一半高于该值;
  • 6.90%ile (ms)
  • 7.Average:所有请求的平均响应时间,毫秒;
  • 8.Min:请求的最小的服务器响应时间,毫秒;
  • 9.Max:请求的最大服务器响应时间,毫秒;
  • 10.Average Size:平均请求的大小,单位字节;
  • 11.Current RPS
  • 12.Current Failures/s

 

可以看到有两个任务

get_root 和  get_root2,对应代码中任务。

执行比例和代码中约定的@task近似。

 

posted @ 2022-02-10 09:52  mandym  阅读(332)  评论(0)    收藏  举报