การทดสอบความเครียด Fastapi

Dec 05 2022
การทดสอบความสามารถในการปรับขนาดของ Locust Python เป็นส่วนสำคัญในการเตรียมบริการเว็บให้พร้อม มีเครื่องมือมากมายสำหรับการทดสอบโหลด เช่น Gatling, Apache JMeter, The Grinder, Tsung และอื่นๆ

ตั๊กแตนหลาม

การทดสอบความสามารถในการขยายเป็นส่วนสำคัญในการเตรียมการผลิตบริการเว็บให้พร้อม มีเครื่องมือมากมายสำหรับการทดสอบโหลด เช่น Gatling, Apache JMeter, The Grinder, Tsung และอื่นๆ นอกจากนี้ยังมีหนึ่งรายการ (และรายการโปรดของฉัน) ที่เขียนด้วย Python และสร้างขึ้นบนไลบรารีRequests : Locust

ดังที่สังเกตได้จากเว็บไซต์ Locust:

คุณสมบัติพื้นฐานของ Locust คือคุณอธิบายการทดสอบทั้งหมดของคุณในโค้ด Python ไม่จำเป็นต้องใช้ UI ที่เกะกะหรือ XML ที่บวม เพียงแค่โค้ดธรรมดา

การติดตั้งตั๊กแตน

โมดูลหลามทดสอบประสิทธิภาพ Locust พร้อมใช้งานบน PyPI และสามารถติดตั้งผ่าน pip หรือ easy_install

pip install locustio or: easy_install locust

ตัวอย่าง locustfile.py

จากนั้นสร้างlocustfile.pyตามตัวอย่างจาก docs ในการทดสอบโปรเจ็กต์ 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

หากต้องการรัน Locust ด้วยไฟล์ python locust ด้านบน หากไฟล์ชื่อlocustfile.pyเราสามารถเรียกใช้ (ในไดเร็กทอรีเดียวกับlocustfile.py ):

locust --host=http://example.com

เมื่อแอพทดสอบการโหลดไพ ธ อน Locust เริ่มต้นขึ้นคุณควรไปเยี่ยมชมhttp://127.0.0.1:8089/และคุณจะพบเว็บอินเทอร์เฟซของอินสแตนซ์ Locust ของเรา จากนั้นป้อนจำนวนผู้ใช้ที่จะจำลอง (เช่น 300) และอัตราการฟัก (ผู้ใช้เกิด/วินาที) (เช่น 10) และกดเริ่มจับกลุ่ม หลังจากนั้น Locust จะเริ่ม "ฟักไข่" ผู้ใช้ และคุณจะเห็นผลลัพธ์ในตาราง

การแสดงข้อมูล Python

ดังนั้น ตารางก็ดี แต่เราต้องการดูผลลัพธ์เป็นกราฟ มีปัญหาที่คนขอให้เพิ่มอินเทอร์เฟซแบบกราฟิกให้กับ Locust และมีหลายข้อเสนอในการแสดงกราฟสำหรับข้อมูล Locust ฉันได้ตัดสินใจใช้ Python ไลบรารีการแสดงภาพแบบโต้ตอบBokeh

มันง่ายในการติดตั้งไลบรารีการสร้างกราฟหลาม Bokeh จาก PyPI โดยใช้ pip:

pip install bokeh

นี่คือตัวอย่างการใช้งานเซิร์ฟเวอร์ Bokeh

เราสามารถรับข้อมูล Locust ในรูปแบบ JSON ได้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