본문 바로가기

카테고리 없음

[Locust] 설치 및 성능 테스트 해보기

 

 

1. Python 설치 확인

Locust는 Python 기반이므로 Python이 설치되어 있어야 합니다.

터미널에서 아래 명령어로 확인

python3 --version
 

 

버전이 안 뜨면 Homebrew로 설치하세요

brew install python
 
 
 

 2. 가상환경 설정 (선택사항)

원하는 디렉토리에서 가상환경을 만들어 깔끔하게 관리할 수 있어요

python3 -m venv venv source venv/bin/activate
 

 

3. Locust 설치

터미널에서 아래 명령어 입력

pip install locust
 

 

설치가 완료되면 아래 명령어로 정상 설치 확인

locust --version
 

 

4. 테스트 실행 예시

from locust import HttpUser, task, between
import random


class BoardServer(HttpUser):
    wait_time = between(0.01, 0.02)

    def on_start(self):
        self.client.post("/users/sign-in", json={"userId": "kimsky",
                                                 "password": "123"})

    @task(3)
    def view_item(self):
        sortStatus = random.choice(["CATEGORIES", "NEWEST", "OLDEST", "HIGH_PRICE", "LOW_PRICE", "GRADE"])
        categoryId = random.randint(1, 10)
        name = '테스트 게시글'.join(str(random.randint(1, 10000)))
        headers = {'Content-Type': 'application/json'}
        data = {"sortStatus": sortStatus,
                "categoryId": categoryId,
                "name": name}
        # print(data)
        self.client.post("/search", json=data, headers=headers)

 

locust 명령어로 실행하기

 locust -f {파일명}.py
 

 

브라우저에서 http://localhost:8089 접속하여 시뮬레이션 시작할 수 있습니다!

 

 

5. 각 항목에 대한 설명

1) Number of users (peak concurrency)

  • 의미: 테스트 중에 동시에 활동하는 최대 사용자 수
  • 예: 500으로 설정하면, Locust는 최대 500명의 가상 사용자가 동시에 활동하도록 설정함
  • 즉, 동시성(concurrency)을 테스트할 때 핵심 지표

2) Ramp up = Spawn rate (users started/second)

  • 의미: 1초당 몇 명의 사용자를 새로 시작할지
  • 예:
    • 50이면 → 초당 50명씩 증가 → 500명 도달하려면 10초 소요
    • 100이면 → 초당 100명씩 증가 → 500명 도달까지 5초
  • 시스템이 점진적인 부하에 얼마나 잘 대응하는지 확인할 수 있음

 3) Host

  • 의미: 테스트할 서버 주소 (예: http://localhost:8080 또는 https://api.example.com)
  • 이 값은 self.client.get() 같은 코드에서 상대 경로로 사용할 때 기반이 되는 호스트 주소임
self.client.get("/api/v1/posts")
# → 실제 호출: http://localhost:8080/api/v1/posts

 

4. Advanced options

설정 가능한 옵션들

옵션 설명
Stop timeout 테스트 종료 시, 사용자들이 즉시 중단되는지 or 요청 다 끝나고 나가는지
Heartbeat interval 마스터-워커 간의 상태 체크 주기 (분산 모드일 때)
Request timeout 요청 최대 허용 시간
Reset stats 중간에 통계 초기화할지 여부

이 옵션들은 필수는 아니지만, 장시간 테스트나 분산 부하 테스트 시 유용합니다.

5. 실전 예시

🔥 "500명의 사용자를 초당 50명씩 증가시켜서 테스트하고 싶다"


항목
Number of users 500
Spawn rate 50
Host http://localhost:8080

 

6. 테스트 후 데이터 확인

1) status

2) charts

 

3) download data 확인

 

 

 

https://locust.io/

 

Locust.io

An open source load testing tool. Define user behaviour with Python code, and swarm your system with millions of simultaneous users.

locust.io