一、环境部署

当前环境:

Python环境python3.7

mac系统(版本10.14.6/10.15.7)

二、安装locust

1、方法一

pip3 install locustio (pip安装提示无pip,各种错误提示信息)及安装失败

报错信息如下:

ERROR: Failed building wheel for locustio

2、方法二

从GitHub项目地址:https://github.com/locustio/locust

下载解压之后,在终端 cd 到项目路径,执行:python3 setup.py install

安装完成之后提示信息:Finished processing dependencies for locust==1.5.3(可以明确当前的locust版本为:1.5.3)

如若安装报错提示:Permission denied: '/usr/local/lib/python3.7/site-packages/easy-install.pth'

权限的问题,需要加上sudo,执行命令如下:sudo python3 setup.py install

以上两种方法验证完成之后,使用locust --version 验证当前的版本信息

三、locust实例

DEMO文件对应请求(GET/POST)

1、GET请求(后续以此实例为准)

# -*- coding:utf-8 -*-
# Author : beihe
# Date : 2020/5/24 19:17
import os

from locust import TaskSet, task, between, HttpUser
from locust.contrib.fasthttp import FastHttpUser


class MyGetTask(TaskSet):
    url = '/'

    def on_start(self):
        print('用户初始化')

    def on_stop(self):
        print('用户结束')

    @task
    def get_test(self):
        # 定义一个对象属性
        with self.client.get(self.url, name='get接口', timeout=10, catch_response=True) as response:
            # 接受接口返回值中的响应文本
            resp_str = response.text
            print(f'响应数据为:{resp_str}')
            if 'success' in resp_str:
                # 请求成功
                response.success()
            else:
                # 请求失败
                response.failure(resp_str)


class MyGetUser(HttpUser):
    tasks = [MyGetTask]
    host = 'https://www.baidu.com'
    wait_time = between(2, 2)


if __name__ == '__main__':
    os.system('locust -f GetTest.py')

2、POST请求

#-*- coding:utf-8 -*-
# Author : beihe
# Date : 2020/5/24 19:17
import os

from locust import TaskSet, task, HttpLocust, between, HttpUser


class MyPostTask(TaskSet):

    url = '/pinter/com/login'
    def on_start(self):
        print('用户初始化')

    def on_stop(self):
        print('用户结束')


    @task
    def post_test(self):
        # 定义一个对象属性
        self.post_data = {'userName': 'admin', 'password':'1234'}
        print(f'请求的参数为:{self.post_data}')
        response = self.client.post(self.url, data=self.post_data ,name='post-kv接口', timeout=10, catch_response=True)
        # 将接口返回值中的json提取出来,转换为一个字典
        resp_dict = response.json()
        print(f'响应数据为:{resp_dict}')
        if resp_dict['message'] == 'success':
            # 请求成功
            response.success()
        else:
            # 请求失败
            response.failure(resp_dict['message'])

class MyPostUser(HttpUser):
    tasks = [MyPostTask]
    host = 'http://localhost:8080'
    wait_time = between(2,2)


if __name__ == '__main__':
    os.system('locust -f PostTest.py')

脚本文件载入之后,终端执行:

locust -f test.py --host=https://www.baidu.com

test.py 对应更改当前你当前自己的py文件即可

执行过程中的问题:

执行报错:8089端口号占用等问题(处理方案如下:)

终端执行:lsof -i: 8089(查看当前端口号的占用情况)

除谷歌之外的端口全部杀死: kill PID号即可重新运行

访问 http://localhost:8089 进入压测首页

首页参数:

    Number of total users to simulate---- 模拟用户数

    Spawn rate (users spawned/second)----生成率(用户生成/秒)

    Host (e.g. http://www.example.com)----请求地址

点击 "Start swarming" 进入压测页面

压测界面右上角有:被压测的地址、当前状态、RPS、失败率、开始或重启按钮(一系列参数简介)

    Type 请求的类型,例如GET/POST

    Name 请求的路径

    Request 当前请求的数量

    Fails 当前请求失败的数量

    Median 中间值,单位毫秒,请求响应时间的中间值

    Average 平均值,单位毫秒,请求的平均响应时间

    Min 请求的最小服务器响应时间,单位毫秒

    Max 请求的最大服务器响应时间,单位毫秒

    Average size 单个请求的大小,单位字节

    Current RPS 代表吞吐量(Requests Per Second的缩写),指的是某个并发用户数下单位时间内处理的请求数。等效于QPS,其实可以看作同一个统计方式,只是叫法不同而已。

    Current Failures/s 当前故障数

及最后的报告下载:

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐