locust官网http://locust.io/
locust官网描述:Define user behaviour with Python code, and swarm your system with millions of simultaneous users. 这款性能测试工具支持python脚本,当作我最近学习python的实践工具。
1、安装python,官网 http://www.python.org/download/,下载python,安装后配置环境变量,cmd窗口输入python验证是否安装成功;
2、安装pip,官网https://pypi.python.org/pypi/pip,下载pip,解压后,cmd窗口cd到解压后的路径,输入python setup.py install,此时pip还不可使用,需在环境变量中配置path,C:\Python27\Scripts(python安装目录);
3、使用pip安装locust,cmd输入pip install locustio
使用locust进行负载测试需要编写python脚本,locust的官网上有例子和讲解,下面是官网中有关登录的测试脚本
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
""" on_start is called when a Locust start before any task is scheduled """
self.login()
def login(self):
self.client.post("/login", {"username":"ellen_key", "password":"education"})
@task(2)
def index(self):
self.client.get("/")
@task(1)
def profile(self):
self.client.get("/profile")
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 5000
max_wait = 9000
保存脚本后,在cmd中输入命令行locust -f locustfile.py,-f代表python脚本位置;
执行以上命令后,在浏览器中输入127.0.0.1:8089
其中Number of users to simulate表示有多少虚拟用户,hatch rate表示每秒请求数,点击Start swarming,开始负载测试。
更深入的用法及参数意义官网上写的很详细,可以去官网查看。