Locust性能测试1--简介安装及基本使用
1. Locust简介
Locust是易于使用的分布式用户负载测试工具,旨在对网站(或其他系统)进行负载测试,并弄清一个系统可以处理多少个并发用户,Locust翻译过来是蝗虫的意思,在测试期间,意在一群蝗虫用户会攻击您的网站系统。您可以使用Python代码定义每个用户的行为,并且通过web UI实现监控集群的过程,Locust完全基于事件,因此可以再一台计算机支持数千个并发用户。- 不同于常用的测试工具,
LR,Jmeter,Locust使用的不是线程,而是gevent
2. 环境安装
pip3 install locust
-
Mac也可以直接通过Homebrew安装 -
查看安装结果
locust --help
Usage: locust [OPTIONS] [UserClass ...]
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
--config CONFIG Config file path
-H HOST, --host HOST Host to load test in the following format:
http://10.21.32.33
-u NUM_USERS, --users NUM_USERS
Number of concurrent Locust users. Only used together
with --headless
-r HATCH_RATE, --hatch-rate HATCH_RATE
The rate per second in which users are spawned. Only
used together with --headless
-t RUN_TIME, --run-time RUN_TIME
Stop after the specified amount of time, e.g. (300s,
20m, 3h, 1h30m, etc.). Only used together with
--headless
-l, --list Show list of possible User classes and exit
........
3. 简单使用
- 访问伊洛个人博客主页 https://yiluotalk.com/
from locust import HttpUser, task, between
class LocustDemo(HttpUser):
wait_time = between(2, 5) # 模拟用户等待2到5s然后执行
@task
def index_blog(self): # 定义函数博客首页
header = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/83.0.4103.61 Safari/537.36 "
} # 请求头
response = self.client.get('', headers=header) # 同request用法基本一致
assert response.status_code == 200 # 断言请求返回200
print(response.status_code, response.text[:1000]) # 打印状态码及返回text
if __name__ == "__main__":
import os
os.system("locust -f demo.py --host=https://yiluotalk.com/") # 执行脚本访问伊洛的博客主页 https://yiluotalk.com/
Locust也是用的request库,所以基本请求方式与request库相同
4. 启动测试
locust -f demo.py --host=https://yiluotalk.com/
- GUI模式,浏览器输入 http://127.0.0.1:8089/显示如下页面
![]()
- Number of users to simulate 设置虚拟用户总数
- Hatch rate (users spawned/second) 每秒启动虚拟用户数
- Host 压测地址
- Start swarming '蝗虫入境'

- 接口返回
200 <!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 4.1.1">
<link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
<link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
<link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
<link rel="mask-icon" href="/images/logo.svg" color="#222">
<link rel="stylesheet" href="/css/main.css">
<link rel="stylesheet" href="/lib/font-awesome/css/font-awesome.min.css">
<script id="hexo-configurations">
var NexT = window.NexT || {};
var CONFIG = {
hostname: new URL('https://yiluotalk.com').hostname,
root: '/',
scheme: 'Pisces',
version: '7.6.0',
exturl: false,
sidebar: {"position":"left","display":"post","padding":18,"offset":12,"onmobile":fals ....

- 压测过程中可以随时通过edit来提高用户并发数
![]()
- 表格charts 可以监控rps、平均响应时间、虚拟用户并发数
![]()
- 报告结果也可以下载成
CSV格式
5. 简单的总结
实际工作中确实常用到的还是Jmeter会比较多一些,但是有句老话叫:“用尽天下工具,不如心中有码”, 如果你喜欢更“随性的定制化”,那么就可以试试Locust




浙公网安备 33010602011771号