# 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 没有加入到 入站规则,即该端口没开放别的机器无法访问(入站规则设置方法)