參考文檔:https://docs.locust.io/en/latest/quickstart.html
1、安裝
這個工具是用python寫的,首先我們要安裝框架
首先升級pip,不然可能會報錯
pip install --upgrade pip
然後安裝Locust
pip install Locust
安裝完成
2、編寫Locust file
這裏是壓測軟件的主要功能
vim locustfile.py
from locust import HttpLocust, TaskSet
#登陸操作
def login(l):
l.client.post("/phpadmin/index.php", {"pma_username":"hugw", "pma_password":"redhat"})
#登出操作
def logout(l):
l.client.post("/phpadmin/logout.php", {"db":"&", "token":"be2cb767a5829a398ef5a4c0dcafe504"})
#請求index
def index(l):
l.client.get('/phpadmin/index.php')
class UserBehavior(TaskSet):
tasks = {index}
#執行登陸和登出
def on_start(self):
login(self)
def on_stop(self):
logout(self)
class WebsiteUser(HttpLocust):
task_set = UserBehavior
min_wait = 3000
max_wait = 6000
啓動壓測程序
如果配置文件名字是locustfile.py,那麼直接運行就好
啓動成功會監聽8089端口,
這裏的 http://192.168.1.9 是要壓測的網站域名
[root@slave locust]# locust -H http://192.168.1.9
[2018-04-12 19:41:37,086] slave/INFO/locust.main: Starting web monitor at *:8089
[2018-04-12 19:41:37,086] slave/INFO/locust.main: Starting Locust 0.8.1
配置壓測集羣(可選)
啓動壓測主程序,跟上面一樣,後面加上參數 --master
啓動成功會監聽8089端口,
這裏的 http://192.168.1.9 是要壓測的網站域名
[root@slave locust]# locust -H http://192.168.1.9 --master
[2018-04-12 22:20:12,889] slave/INFO/locust.main: Starting web monitor at *:8089
[2018-04-12 22:20:12,891] slave/INFO/locust.main: Starting Locust 0.8.1
[2018-04-12 22:20:47,669] slave/INFO/locust.runners: Client 'k8s_cf9d5e7c4238f74363180e5b3dcb0ae4' reported as ready. Currently 1 clients ready to swarm.
[2018-04-12 22:21:02,066] slave/INFO/locust.runners: Sending hatch jobs to 1 ready clients
[2018-04-12 22:21:12,069] slave/INFO/locust.runners: Resetting stats
啓動slave
先把 locustfile.py 分發到slave機器上,安裝locust,然後啓動
[root@k8s locust]# locust -H http://192.168.1.9 --slave --master-host=master_IP
[2018-04-12 02:21:48,045] k8s/INFO/locust.main: Starting Locust 0.8.1
[2018-04-12 02:22:02,487] k8s/INFO/locust.runners: Hatching and swarming 20 clients at the rate 2 clients/s...
[2018-04-12 02:22:12,509] k8s/INFO/locust.runners: All locusts hatched: WebsiteUser: 20
[2018-04-12 02:22:12,509] k8s/INFO/locust.runners: Resetting stats
然後登陸master壓測就行了
3、開始壓測
填寫你要模擬的用戶數量,這裏填500用戶,以每秒50數量增加,點擊start 開始
查看壓測請求情況
查看圖表