forked from locustio/locust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example of a bottlenecked server and use that test to make a new …
…graph for the docs.
- Loading branch information
Lars Holmberg
committed
Jul 17, 2024
1 parent
e33b413
commit 7152dc4
Showing
6 changed files
with
40 additions
and
6 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
This example uses extensions in Locust's own WebUI to simulate a bottlenecked server and runs a test against itself. | ||
The purpose of this is mainly to generate nice graphs in the UI to teach new users how to interpret load test results. | ||
See https://docs.locust.io/en/stable/quickstart.html#locust-s-web-interface | ||
""" | ||
|
||
from locust import HttpUser, events, run_single_user, task | ||
|
||
import time | ||
from threading import Semaphore | ||
|
||
# Only allow up to 10 concurrent requests. Similar to how a server with 10 threads might behave. | ||
sema = Semaphore(10) | ||
|
||
|
||
class WebsiteUser(HttpUser): | ||
host = "http://127.0.0.1:8089" | ||
|
||
@task | ||
def index(l): | ||
l.client.get("/slow") | ||
|
||
|
||
@events.init.add_listener | ||
def locust_init(environment, **kwargs): | ||
assert environment.web_ui, "you can't run this headless" | ||
|
||
@environment.web_ui.app.route("/slow") | ||
def my_added_page(): | ||
with sema: # only 10 requests can hold this lock at the same time | ||
time.sleep(1) # pretend each request takes 1 second to execute | ||
return "Another page" | ||
|
||
|
||
if __name__ == "__main__": | ||
run_single_user(WebsiteUser) |