文章目录
  1. 1. 1.安装Python
  2. 2. 2.安装Locust
  3. 3. 3.安装成功,CMD敲入命令验证。
  4. 4. 4.创建load_test.py文件,通过Python编写性能测试脚本。
  5. 5. 5.命令行切换到性能测试脚本所在的目录,启动性能测试:

An open source load testing tool.

一个开源性能测试工具。

define user behaviour with python code, and swarm your system with millions of simultaneous users.

使用Python代码来定义用户行为。用它可以模拟百万计的并发用户访问你的系统。

1.安装Python

2.安装Locust

pip install locust

3.安装成功,CMD敲入命令验证。

​ locust –help

4.创建load_test.py文件,通过Python编写性能测试脚本。

1
2
3
4
5
6
7
8
9
10
11
12
from locust import HttpLocust, TaskSet, task

class UserBehavior(TaskSet):

@task(1)
def indesx(self):
self.client.get("/")

class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 3000
max_wait = 6000

创建UserBehavior()类继承TaskSet类,为用户行为类。

​ 创建index() 方法表示一个行为。用@task() 装饰该方法为一个任务。1表示一个Locust实例被挑选执行的权重,数值越大,执行频率越高。在当前UserBehavior()行为下只有一个index()任务,所以,这里的权重设置为几,并无影响。

WebsiteUser()类用于设置性能测试。

  task_set :指向一个定义了的用户行为类。

  min_wait :用户执行任务之间等待时间的下界,单位:毫秒。

  max_wait :用户执行任务之间等待时间的上界,单位:毫秒。

5.命令行切换到性能测试脚本所在的目录,启动性能测试:

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

​ load_test.py 为测试脚本,https://www.baidu.com 为测试的网站。

​ 打开浏览器访问:http://127.0.0.1:8089或者http://localhost:8089

ps:在pycharm中运行脚本

1
2
3
if __name__ == '__main__':
import os
os.system("locust -f load_test.py --host=https://www.baidu.com")
文章目录
  1. 1. 1.安装Python
  2. 2. 2.安装Locust
  3. 3. 3.安装成功,CMD敲入命令验证。
  4. 4. 4.创建load_test.py文件,通过Python编写性能测试脚本。
  5. 5. 5.命令行切换到性能测试脚本所在的目录,启动性能测试: