Fastapi 스트레스 테스트
메뚜기 비단뱀
확장성 테스트는 웹 서비스 프로덕션을 준비하는 데 중요한 부분입니다. Gatling, Apache JMeter, The Grinder, Tsung 등과 같은 많은 부하 테스트 도구가 있습니다. Python으로 작성되고 Requests 라이브러리 에 구축된 Locust 도 있습니다 .
Locust 웹사이트에서 알 수 있듯이:
Locust의 기본 기능은 모든 테스트를 Python 코드로 설명한다는 것입니다. 투박한 UI나 부풀려진 XML이 필요하지 않고 일반 코드만 있으면 됩니다.
메뚜기 설치
성능 테스트 Python 모듈 Locust는 PyPI에서 사용할 수 있으며 pip 또는 easy_install을 통해 설치할 수 있습니다.
pip install locustio or: easy_install locust
예 locustfile.py
그런 다음 docs의 예제 에 따라 locustfile.py를 만듭니다 . Django 프로젝트를 테스트하기 위해 csrftoken 지원 및 ajax 요청에 대한 일부 헤더를 추가해야 했습니다. 최종 locustfile.py는 다음과 같을 수 있습니다.
# locustfile.py
from locust import HttpLocust, TaskSet, task
class UserBehavior(TaskSet):
def on_start(self):
self.login()
def login(self):
# GET login page to get csrftoken from it
response = self.client.get('/accounts/login/')
csrftoken = response.cookies['csrftoken']
# POST to login page with csrftoken
self.client.post('/accounts/login/',
{'username': 'username', 'password': 'P455w0rd'},
headers={'X-CSRFToken': csrftoken})
@task(1)
def index(self):
self.client.get('/')
@task(2)
def heavy_url(self):
self.client.get('/heavy_url/')
@task(2)
def another_heavy_ajax_url(self):
# ajax GET
self.client.get('/another_heavy_ajax_url/',
headers={'X-Requested-With': 'XMLHttpRequest'})
class WebsiteUser(HttpLocust):
task_set = UserBehavior
위의 python locust 파일로 Locust를 실행하려면 이름이 locustfile.py 인 경우 다음을 실행할 수 있습니다( locustfile.py 와 동일한 디렉토리에서 ).
locust --host=http://example.com
Python 부하 테스트 앱 Locust가 시작되면 다음을 방문해야 합니다.http://127.0.0.1:8089/거기에서 Locust 인스턴스의 웹 인터페이스를 찾을 수 있습니다. 그런 다음 시뮬레이션할 사용자 수 (예: 300) 및 Hatch rate(초당 생성된 사용자) (예: 10)를 입력 하고 Start swarming 을 누릅니다 . 그 후 Locust는 사용자를 "해칭"하기 시작하고 테이블에서 결과를 볼 수 있습니다.
파이썬 데이터 시각화
따라서 테이블은 훌륭하지만 결과를 그래프로 보는 것이 좋습니다. 사람들이 Locust에 그래픽 인터페이스를 추가하도록 요청하는 문제가 있으며 Locust 데이터에 대한 그래프를 표시하는 방법에 대한 몇 가지 제안이 있습니다. Python 대화형 시각화 라이브러리 인 Bokeh를 사용하기로 결정했습니다 .
pip를 사용하여 PyPI에서 Python 그래프 라이브러리 Bokeh를 쉽게 설치할 수 있습니다.
pip install bokeh
다음은 Bokeh 서버를 실행하는 예 입니다 .
JSON 형식의 Locust 데이터를 얻을 수 있습니다.http://localhost:8089/stats/requests. 데이터는 다음과 같아야 합니다.
{
"errors": [],
"stats": [
{
"median_response_time": 350,
"min_response_time": 311,
"current_rps": 0.0,
"name": "/",
"num_failures": 0,
"max_response_time": 806,
"avg_content_length": 17611,
"avg_response_time": 488.3333333333333,
"method": "GET",
"num_requests": 9
},
{
"median_response_time": 350,
"min_response_time": 311,
"current_rps": 0.0,
"name": "Total",
"num_failures": 0,
"max_response_time": 806,
"avg_content_length": 17611,
"avg_response_time": 488.3333333333333,
"method": null,
"num_requests": 9
}
],
"fail_ratio": 0.0,
"slave_count": 2,
"state": "stopped",
"user_count": 0,
"total_rps": 0.0
}
모두 함께 실행
따라서 Locust가 실행 중이고(아니라면 로 시작 locust --host=http://example.com) 이제 Bokehserver로 시작한 bokeh serve다음 plotter.py를 로 실행해야 합니다 python plotter.py. 스크립트 호출이 표시 되면 문서를 볼 수 있는 올바른 URL까지 브라우저 탭이 자동으로 열립니다.
Locust가 이미 테스트를 실행 중인 경우 그래프에서 즉시 결과를 볼 수 있습니다. 그렇지 않으면 다음에서 새 테스트를 시작합니다.http://localhost:8089/Bokeh 탭으로 돌아가서 테스트 결과를 실시간으로 확인하십시오.
그게 다야. github 에서 전체 코드를 찾을 수 있습니다 . 자유롭게 복제하고 예제를 실행하세요.
git clone https://github.com/steelkiwi/locust-bokeh-load-test.git
cd locust-bokeh-load-test
pip install -r requirements.txt
locust --host=<place here link to your site>
bokeh serve
python plotter.py

![연결된 목록이란 무엇입니까? [1 부]](https://post.nghiatu.com/assets/images/m/max/724/1*Xokk6XOjWyIGCBujkJsCzQ.jpeg)



































