반응형

locust 모듈 설치

  • python 3.6 버전으로 설치해야함
pip3 install locust

~/vote.py

import datetime
import random
import uuid

from locust import HttpUser, task, between

class VoteAdminUser(HttpUser):
    wait_time = between(1, 5)  # 각 태스크를 1 ~ 5초 텀을 두고 실행

    def on_start(self):  # 각 사용자별로 최초 1번 실행
        response = self.client.post("/api/users/login", json={"id": "john_admin", "password": "1234"})

        self.client.cookies.set('userToken', response.cookies.get('userToken'))

    @task(5)  # 다른 태스크보다 5배 더 많이 실행 실행
    def get_votes(self):
        vote_start_date = (datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 100))) \
            .strftime("%Y-%m-%d %H:%m:%S")
        vote_end_date = (datetime.datetime.now() + datetime.timedelta(days=random.randint(1, 100))) \
            .strftime("%Y-%m-%d %H:%m:%S")

        self.client.get(f"/api/admin/votes?voteStartDate={vote_start_date}&voteEndDate={vote_end_date}")

    @task(1)
    def put_vote(self):
        vote_type = random.choice(['ONCE', 'DAILY'])
        vote_id = str(uuid.uuid4())
        vote_start_date = (datetime.datetime.now() - datetime.timedelta(days=random.randint(1, 100))) \
            .strftime("%Y-%m-%d %H:%m:%S")
        vote_end_date = (datetime.datetime.now() + datetime.timedelta(days=random.randint(1, 100))) \
            .strftime("%Y-%m-%d %H:%m:%S")
        min_answer_count = 1
        max_answer_count = random.randint(1, 5)
        vote_items = list(map(
            lambda num: {"voteItemTitle": f"{123}-{num}"},
            list(range(random.randint(1, 5)))
        ))

        self.client.put("/api/admin/votes", json={
            'voteType': vote_type,
            'voteTitle': vote_id,
            "voteStartDate": vote_start_date,
            "voteEndDate": vote_end_date,
            "minAnswerCount": min_answer_count,
            "maxAnswerCount": max_answer_count,
            "voteItems": vote_items
        })

locust 실행

locust -f vote.py

locust web 접속

  • http://localhost:8089

테스트 시작

  • 초당 20명씩 사용자를 생성해서 200명까지 생성하여 테스트

테스트 결과 확인

  • Statistics

  • Charts

  • Failures

참고

반응형

'Development > Python' 카테고리의 다른 글

[Python] tensorflow 시작하기  (0) 2024.01.30
[Python] 설치  (0) 2021.05.16
[Python] MySQL  (0) 2020.12.30
[Python] 가상환경  (0) 2020.12.30
[Python] 기본 문법  (0) 2020.12.30

+ Recent posts