Locustとは
Pythonで開発された負荷テストツールです。
https://docs.locust.io/en/stable/
インストール
# インストール
pip3 install locust
# バージョン確認
locust -V
Locustでの負荷テスト
locust.pyを作成します。
locust.py
from locust import tag, task, TaskSet, SequentialTaskSet, HttpUser, constant_pacing
# テストクラス
class TestClass(SequentialTaskSet):
# login info
data = {
"email": "your email", # e.x. "email@locust.com"
"password": "your password"
}
# ログイン
@tag('user')
def on_start(self):
"""
test user login
"""
self.client.post("/login", json=self.data, headers={"User-Agent":"locust"})
# ユーザー編集APIをテストする
@tag('user')
@task
def user(self):
response = self.client.patch("/user", json={
"user_name": "abc"}, headers={"User-Agent":"locust"})
# テストを終了する
class testStop(TaskSet):
@tag('user')
@task
def stop(self):
self.interrupt()
# テストを起動する
class TestRun(HttpUser):
tasks = {TestClass:1}
wait_time = constant_pacing(1)
ターミナルで下記のコマンドを実施します。
locust -f locust.py --headless --users 4 --spawn-rate 1 -t 1m --csv output_csv_report.csv --html output_html_report.csv
--users/--spawn-rate: 毎秒1ユーザーずつ、4ユーザーまで増加させる
-t: テスト時間、デフォルトの単位はs(秒)になります
--csv/--html: 出力レポート
Refrence
https://book.st-hakky.com/docs/locust-intro/
https://note.com/shift_tech/n/n77be58376523