1.性能測試工具:
1.LoadRunner
2.JMeter
3.Locust
4.Molotov
https://mp.weixin.qq.com/s/Sm9OAnd0k8_E-I7Se2TlbQ
安裝
GitHub:https://github.com/loads/molotov
安裝:
pip install molotov
文檔:https://molotov.readthedocs.io/en/stable/
例子
我們來進行接口測試,創(chuàng)建創(chuàng)建腳本loadtest.py。
"""
molotov 性能測試腳本"""
from molotovimport scenario
_API ='http://127.0.0.1:5000/'
@scenario(weight=40)
async def scenario_one(session):
"""測試默認接口(get)"""
? ? async with session.get(_API)as resp:
res =await resp.json()
assert resp.status ==200
? ? ? ? assert res['code'] ==10200
? ? ? ? assert res['message'] =='Welcome to API testing'
@scenario(weight=60)
async def scenario_two(session):
"""添加用戶信息接口(post->JSON)"""
? ? payload = {"name":"jack", "age":22, "height":177}
async with session.post(_API +"add_user", json=payload)as resp:
res =await resp.json()
assert resp.status ==200
? ? ? ? assert res['code'] ==10200
? ? ? ? assert res['message'] =='add success'
weight表示接口的虛擬用戶權重。比如100個用戶40%用來請求第一個接口,60%用來請求第二個接口。
我們還可以使用assert非常方便的為接口寫斷言。
被測接口來自:https://github.com/defnngj/learning-API-test
運行
當Molotov測試使用該擴展時,該函數(shù)將收集執(zhí)行時間,并打印出Molotov發(fā)出的所有請求的平均響應時間:
>molotov loadtest.py -w 10 -r 100
**** Molotov v1.6. Happy breaking!****
Preparing 10 workers...
OK
SUCCESSES: 1000|FAILURES: 0|WORKERS: 10
*** Bye ***
主要參數(shù):
-w 指定worker的虛擬用戶。
-r 指定每個虛擬用戶的請求個數(shù)。
-d 指定運行的時長。
-q 靜默情況執(zhí)行。
-x 當出現(xiàn)失敗時停止。
這樣輕量的性能工具還是非常好用的,很合適接口的性能測試。