1.5.25. fejezet, Tesztelés - Locust

Kapcsolódó hivatkozások

Weboldal terheléses tesztelése

import random
from locust import HttpUser, between, task
from pyquery import PyQuery
 
 
class AwesomeUser(HttpUser):
    host = "https://docs.locust.io/en/latest/"
 
    # we assume someone who is browsing the Locust docs,
    # generally has a quite long waiting time (between
    # 10 and 600 seconds), since there's a bunch of text
    # on each page
    wait_time = between(10, 20)
 
    def on_start(self):
        # start by waiting so that the simulated users
        # won't all arrive at the same time
        self.wait()
        # assume all users arrive at the index page
        self.index_page()
        self.urls_on_current_page = self.toc_urls
 
    @task(10)
    def index_page(self):
        r = self.client.get("")
        pq = PyQuery(r.content)
        link_elements = pq(".toctree-wrapper a.internal")
        self.toc_urls = [
            l.attrib["href"] for l in link_elements
        ]
 
    @task(50)
    def load_page(self):
        url = random.choice(self.toc_urls)
        r = self.client.get(url)
        pq = PyQuery(r.content)
        link_elements = pq("a.internal")
        self.urls_on_current_page = [
            l.attrib["href"] for l in link_elements
        ]
 
    @task(30)
    def load_sub_page(self):
        url = random.choice(self.urls_on_current_page)
        r = self.client.get(url)
 
if __name__ == "__main__":
    from locust.env import Environment
    my_env = Environment(user_classes=[AwesomeUser])
    my_env.create_local_runner()
    my_env.create_web_ui()
    my_env.web_ui.greenlet.join()
    #my_env.runner.start(5000, spawn_rate=20)
    #my_env.runner.greenlet.join()

VSCode debug lanch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Current File",
            "type": "debugpy",
            "request": "launch",
            "program": "${file}",
            "console": "integratedTerminal",
            "env": {
                "GEVENT_SUPPORT": "True"  
            }
        }
    ]
}

Weboldal a http://127.0.0.1:8089/ URL-en érhető el.