Locust로 하는 부하테스트

림예·2024년 9월 5일
0


Number of users(최종 사용자 수): 10000
Ramp up(초당 사용자 증가수): 100


Python 및 pip 설치

sudo apt update
sudo apt install -y python3 python3-pip

Locust 설치

pip3 install locust

Locustfile 작성

from locust import HttpUser, TaskSet, task, between
import json

class OrdersTaskSet(TaskSet):

    def on_start(self):
        # 로그인이나 인증 토큰
        self.auth_token = "토큰 입력"

    @task
    def create_order(self):
        headers = {
            "Content-Type": "application/json",
            "Authorization": f"Bearer {self.auth_token}",
        }

        payload = {
                "id" : "2",
                "amount" : "1"
                }

        response = self.client.post("/api/orders/create", data=json.dumps(payload), headers=headers)
        print(response.json())


class OrdersUser(HttpUser):
    tasks = [OrdersTaskSet]
    wait_time = between(1, 5)  # 요청 사이에 대기할 시간(초)

Locust 멀티 인스턴스 실행

locust -f locustfile.py --master
locust -f /home/ec2-user/locustfile.py --worker --master-host=ip 주소




profile
Think big 🌏

0개의 댓글