locust簡介:基于python撰寫,簡單易于上手;支持分布式;腳本撰寫容易,web圖形化界面操作容易,結果簡潔,易讀,
官網:Locust - A modern load testing framework
https://locust.io/一、環境搭建:
1.搭建 python環境
2.安裝:pip3 install locust
2.1windows可以使用pycharm環境下進行搭建 或者 先從windows商店,先安裝個Ubuntu再搭建環境即可
二 、腳本撰寫:
#!/usr/bin/env python
# _*_ coding: UTF-8 _*_
import random,gevent,requests,json
from locust import TaskSet, task, between, HttpUser
# 任務集
class MyTask(TaskSet):
def on_start(self):
print("用戶初始化")
# self.login();
def on_stop(self):
print("用戶結束")
# self.logout();
@task
def goods_list(self):
# url = '/api/light-chain-goods/app/goods/hot-goods-page?size=20¤t=1'
url = '/light-chain-goods/app/goods/hot-goods-page?size=20¤t=1'
header={'cache-control': "no-cache",'postman-token': "dbbc1665-e9a6-0ca4-6c69-3ecbed643866"}
response =self.client.get(url,headers=header)
res = response.json()
if res['code'] == 200 and len(res['data'])!=0:
return
else:
print("商品串列,獲取失敗 !")
# 用戶類
class MyUser(HttpUser):
tasks = [MyTask] # 指定用戶運行的任務類
# wait_time = between(1,1) #等待時間
# if __name__ == '__main__':
# # os.system("locust -f locustfile.py --host=https://xxxxxxxx.ltd ") #括號里面你的請求域名地址
單個執行可以直接取消最后兩行注釋,
三 、分布式環境搭建:
"""
pycharm下可以如下:
目標:locust分布式
角色:
1.主機(控制機) --master
2.從屬主機(執行機) --salve --master-host=mater主機ip地址
從屬主機必須依賴重點(可mac,可linux,可windows):
1.必須有python及locust環境
2.必須有主機的副本
Terminal Local:
locust -f venv\locustfile.py --host=http://47.96.85.8:81 --master
Terminal Local(2):
locust -f venv\locustfile.py --worker --master-host=192.168.0.175
"""
四 、命令列運行:
"""
命令列,實作分布式
a. 命令
locust -f 路徑/檔案.py --master --no-web -c 100 -r 10 --expect-workers 2 --run-time 20 --csv=./master_result.csv
b. 引數
--no-web : 無web頁面運行
-c : 虛擬用戶數
-r : 每秒范訓數
--expect-workers : 指定從屬主機數量
--run-time : 運行指定時長
--csv : 保存結果
"""
五 、后臺服務器監控可以結合nmon使用:
服務端監控工具nmon下載安裝_u013080870的博客-CSDN博客下載網址:http://nmon.sourceforge.net/pmwiki.php?n=Site.Download以centos7為例,下載建議,可以打開網頁,然后全域搜索Ctrl+Fhttps://blog.csdn.net/u013080870/article/details/119605192
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/300452.html
標籤:其他
上一篇:DOS命令關機小程式
