-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some random examples that maybe could be useful to someone
- Loading branch information
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
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,49 @@ | ||
# This locust test script example will simulate a user | ||
# browsing the Locust documentation on http://docs.locust.io | ||
|
||
import random | ||
from locust import HttpLocust, TaskSet, task | ||
from pyquery import PyQuery | ||
|
||
|
||
class BrowseDocumentation(TaskSet): | ||
def on_start(self): | ||
# 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=None): | ||
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) | ||
|
||
|
||
class AwesomeUser(HttpLocust): | ||
task_set = BrowseDocumentation | ||
host = "http://docs.locust.io/en/latest/" | ||
|
||
# we assume someone who is browsing the Locust docs, | ||
# generally has a quite long waiting time (between | ||
# 20 and 600 seconds), since there's a bunch of text | ||
# on each page | ||
min_wait = 20 * 1000 | ||
max_wait = 600 * 1000 |
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,25 @@ | ||
# locustfile.py | ||
|
||
from locust import HttpLocust, TaskSet, task | ||
|
||
USER_CREDENTIALS = [ | ||
("user1", "password"), | ||
("user2", "password"), | ||
("user3", "password"), | ||
] | ||
|
||
class UserBehaviour(TaskSet): | ||
def on_start(self): | ||
if len(USER_CREDENTIALS) > 0: | ||
user, passw = USER_CREDENTIALS.pop() | ||
self.client.post("/login", {"username":user, "password":passw}) | ||
|
||
@task | ||
def some_task(self): | ||
# user should be logged in here (unless the USER_CREDENTIALS ran out) | ||
self.client.get("/protected/resource") | ||
|
||
class User(HttpLocust): | ||
task_set = UserBehaviour | ||
min_wait = 5000 | ||
max_wait = 60000 |
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,31 @@ | ||
import os | ||
|
||
from locust import HttpLocust, TaskSet, task | ||
from locust.clients import HttpSession | ||
|
||
class MultipleHostsLocust(HttpLocust): | ||
abstract = True | ||
|
||
def __init__(self, *args, **kwargs): | ||
super(MultipleHostsLocust, self).__init__(*args, **kwargs) | ||
self.api_client = HttpSession(base_url=os.environ["API_HOST"]) | ||
|
||
|
||
class UserTasks(TaskSet): | ||
# but it might be convenient to use the @task decorator | ||
@task | ||
def index(self): | ||
self.locust.client.get("/") | ||
|
||
@task | ||
def index_other_host(self): | ||
self.locust.api_client.get("/stats/requests") | ||
|
||
class WebsiteUser(MultipleHostsLocust): | ||
""" | ||
Locust user class that does requests to the locust web server running on localhost | ||
""" | ||
host = "http://127.0.0.1:8089" | ||
min_wait = 2000 | ||
max_wait = 5000 | ||
task_set = UserTasks |
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,25 @@ | ||
from locust import HttpLocust, TaskSet, task, events | ||
|
||
from gevent.coros import Semaphore | ||
all_locusts_spawned = Semaphore() | ||
all_locusts_spawned.acquire() | ||
|
||
def on_hatch_complete(**kw): | ||
all_locusts_spawned.release() | ||
|
||
events.hatch_complete += on_hatch_complete | ||
|
||
class UserTasks(TaskSet): | ||
def on_start(self): | ||
all_locusts_spawned.wait() | ||
self.wait() | ||
|
||
@task | ||
def index(self): | ||
self.client.get("/") | ||
|
||
class WebsiteUser(HttpLocust): | ||
host = "http://127.0.0.1:8089" | ||
min_wait = 2000 | ||
max_wait = 5000 | ||
task_set = UserTasks |