{"library":"locust","code":"import os\nfrom locust import HttpUser, task, between\n\nclass QuickstartUser(HttpUser):\n    wait_time = between(1, 2)  # Users wait between 1 and 2 seconds after each task\n    host = os.environ.get('LOCUST_TARGET_HOST', 'http://localhost:8089') # Default host for testing\n\n    def on_start(self):\n        \"\"\" on_start is called when a Locust user starts running \"\"\"\n        # Example: Simulating a login if required by the target system\n        # self.client.post(\"/login\", json={\"username\":\"test_user\", \"password\":\"test_password\"})\n        print(f\"Starting user on host: {self.host}\")\n\n    @task\n    def hello_world(self):\n        self.client.get(\"/hello\")\n        self.client.get(\"/world\")\n\n    @task(3)\n    def view_items(self):\n        # Simulate viewing items with dynamic IDs, using 'name' to aggregate stats\n        for item_id in range(10):\n            self.client.get(f\"/item?id={item_id}\", name=\"/item\")\n\n    def on_stop(self):\n        \"\"\" on_stop is called when a Locust user stops running \"\"\"\n        # Example: Simulating a logout\n        # self.client.post(\"/logout\")\n        print(\"Stopping user.\")\n\n# To run this, save as `locustfile.py` and execute `locust` from the terminal.\n# Then open your browser to http://localhost:8089 to use the web UI.\n# Alternatively, run headless: `locust -f locustfile.py --headless --users 10 --spawn-rate 5 -H http://your-target-host.com`","lang":"python","description":"This quickstart defines a `QuickstartUser` that inherits from `HttpUser` for HTTP load testing. It includes `wait_time` to simulate user think time and two tasks (`hello_world` and `view_items`) decorated with `@task` to define user actions. The `view_items` task demonstrates using the `name` parameter for grouping dynamic URLs. `on_start` and `on_stop` methods are shown for per-user setup and teardown, such as login/logout. Save this code as `locustfile.py` and run `locust` from the command line to start the web UI, or use the `--headless` option for command-line execution [2, 10, 20].","tag":null,"tag_description":null,"last_tested":"2026-04-24","results":[{"runtime":"python:3.10-alpine","exit_code":0},{"runtime":"python:3.10-slim","exit_code":0},{"runtime":"python:3.11-alpine","exit_code":0},{"runtime":"python:3.11-slim","exit_code":0},{"runtime":"python:3.12-alpine","exit_code":0},{"runtime":"python:3.12-slim","exit_code":0},{"runtime":"python:3.13-alpine","exit_code":0},{"runtime":"python:3.13-slim","exit_code":0},{"runtime":"python:3.9-alpine","exit_code":0},{"runtime":"python:3.9-slim","exit_code":0}]}