回到顶部

阅读目录

locust 性能测试工具使用资料

认识和安装 locust 

# windows 10

pip3 install -i https://pypi.douban.com/simple/ locustio==0.14.6

启动参数介绍

举个例子

# car_city.py

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@author: yinzhuoqun
@site: http://xieboke.net/
@email: yin@zhuoqun.info
@time: 2020/9/16 18:15
"""

import os
import requests
from locust import HttpLocust, TaskSet, task

requests.packages.urllib3.disable_warnings()


class WXLogin(TaskSet):

    # @task 装饰该方法表示为用户行为,括号里面参数表示该行为的执行权重:数值越大,执行频率越高,不设置默认是1;
    @task(1)
    def garage_upgrade(self):
        header = {
            "User-Agent": "xxxx/1.0.0 (Linux; U; Android 10; zh-cn) (default; 1000000)",
            "Host": "xxx",
            "token": "xxx"
        }
        json_data = {
            "number": 1,
            "spaceId": 1
        }
        # post 请求使用 client.post,get 请求使用 client.get
        req = self.client.post("/garage/upgrade", headers=header, json=json_data, verify=False)
        if req.status_code == 200:
            print("success")
        else:
            print("failed")


class CarCityUser(HttpLocust):
    task_set = WXLogin  # 指向定义了用户行为的类
    host = "http://xxx.com"  # 被测的 Host

    min_wait = 3000  # 单位为毫秒,模拟负载的任务之间执行时的最小等待时间
    max_wait = 6000  # 单位为毫秒,模拟负载的任务之间执行时的最大等待时间

脚本增强

https://debugtalk.com/post/head-first-locust-advanced-script/

源码分析

分布式启动

先启动 master (主机):访问 http://localhost:8089/ 可查状态

locust -f car_city.py --master

再启动 slave (从机,--master-host 后面写主机 IP)

locust -f car_city.py --slave --master-host=192.168.89.126

启动从机后主机控制台会有连接上的日志

E:\yinzhuoqun\locust_test>locust -f car_city.py --master
[2020-09-17 17:49:04,720] DESKTOP-9K42C1Q/INFO/locust.main: Starting web monitor at http://*:8089
[2020-09-17 17:49:04,721] DESKTOP-9K42C1Q/INFO/locust.main: Starting Locust 0.14.6
[2020-09-17 17:54:52,244] DESKTOP-9K42C1Q/INFO/locust.runners: Client 'DESKTOP-9K42C1Q_bc7e6bdd22c84c48bb910f5be41d80a6' reported as ready. Currently 1 clients ready to swarm.
[2020-09-17 17:55:31,724] DESKTOP-9K42C1Q/INFO/locust.runners: Client 'DESKTOP-9K42C1Q_bc7e6bdd22c84c48bb910f5be41d80a6' quit. Currently 0 clients connected.
[2020-09-17 17:55:49,913] DESKTOP-9K42C1Q/INFO/locust.runners: Client 'DESKTOP-9K42C1Q_52383b135c4f4f9ea39cdaf3eceb0d83' reported as ready. Currently 1 clients ready to swarm.
[2020-09-17 18:05:31,840] DESKTOP-9K42C1Q/INFO/locust.runners: Sending hatch jobs of 2 locusts and 2.00 hatch rate to 1 ready clients
[2020-09-17 18:05:49,141] DESKTOP-9K42C1Q/INFO/locust.runners: Removing DESKTOP-9K42C1Q_52383b135c4f4f9ea39cdaf3eceb0d83 client from running clients
[2020-09-17 18:05:49,142] DESKTOP-9K42C1Q/INFO/locust.runners: Client 'DESKTOP-9K42C1Q_52383b135c4f4f9ea39cdaf3eceb0d83' reported as ready. Currently 1 clients ready to swarm.
[2020-09-17 18:13:44,857] DESKTOP-9K42C1Q/INFO/locust.runners: Client 'XG7TARM8695SQQL_73f8971e70ff47bab372054da9b1db8b' reported as ready. Currently 2 clients ready to swarm.

遇到的问题

在win10,从机启动后无法连接到主机(主机和从机不在同一台电脑),原因是主机的端口 5557 和 5558 没有加入到 入站规则,即该端口没开放别的机器无法访问(入站规则设置方法

运行效果图


^_^
请喝咖啡 ×

文章部分资料可能来源于网络,如有侵权请告知删除。谢谢!

前一篇: Locust package has moved from 'locustio' to 'locust'. Please update your reference (or pin your version to 0.14.6 if you dont want to update to 1.0)
下一篇: locust You are running in distributed mode but have no slave servers connected. Please connect slaves prior to swarming.