1. Locust简介
  • Locust是易于使用的分布式用户负载测试工具,旨在对网站(或其他系统)进行负载测试,并弄清一个系统可以处理多少个并发用户,Locust翻译过来是蝗虫的意思,在测试期间,意在一群蝗虫用户会攻击您的网站系统。您可以使用Python代码定义每个用户的行为,并且通过web UI实现监控集群的过程,Locust完全基于事件,因此可以再一台计算机支持数千个并发用户。
  • 不同于常用的测试工具,LRJmeterLocust使用的不是线程,而是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. 简单使用
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

最新文章

  1. IOS8解决获取位置坐标信息出错(Error Domain=kCLErrorDomain Code=0)(转)
  2. 关于hadoop 配置文件的一些实验
  3. sikuli实例
  4. 使用commons-fileUpload组件上传文件
  5. 关​于​h​i​b​e​r​n​a​t​e​中​双​向​外​键​关​联​o​n​e​-​t​o​-​o​n​e​的​p​r​o​p​e​r​t​y​-​r​e​f​=​的​问​题(转)
  6. android应用分析之apk文件结构
  7. getOutputStream() has already been called for this response异常的原因和解决方法
  8. iOS生成一个32位的UUID
  9. js之Math对象
  10. hdu3037 Saving Beans
  11. Asp.Net Core中HttpClient的使用方式
  12. Abp IRepository 方法解释(1)
  13. 基于Docker一键部署大规模Hadoop集群及设计思路
  14. MFC 用ShellExecute打开外部文件
  15. HDU 2089 不要62 (数位DP)题解
  16. String() 函数把对象的值转换为字符串。
  17. Bootstrap组件系列之福利篇几款好用的组件(推荐)
  18. 【Newtonsoft.Json.dll】操作列表JSON数据
  19. c++的一些编程技巧和细节
  20. Angularjs学习笔记1_基本技巧

热门文章

  1. Python自动化运维:技术与最佳实践 PDF高清完整版|网盘下载内附地址提取码|
  2. Python网络编程基础 PDF 完整超清版|网盘链接内附提取码下载|
  3. PHP set_file_buffer() 函数
  4. 记不住git命令?试试这个命令浏览网站
  5. selenium WebDriverWait类等待机制的实现
  6. 谈谈Nginx和php之间是交互与通信的方式
  7. Java基础高级篇 NIO
  8. Vulnhub靶场-Me and my girlfriend 学习笔记
  9. Go 中的动态作用域变量
  10. 测试面试题集锦(四)| Linux 与 Python 编程篇(附答案)