Skip to content

Commit

Permalink
Added some random examples that maybe could be useful to someone
Browse files Browse the repository at this point in the history
  • Loading branch information
heyman committed Sep 19, 2017
1 parent c9690f0 commit 6a35949
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 0 deletions.
49 changes: 49 additions & 0 deletions examples/browse_docs_test.py
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
25 changes: 25 additions & 0 deletions examples/dynamice_user_credentials.py
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
31 changes: 31 additions & 0 deletions examples/multiple_hosts.py
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
25 changes: 25 additions & 0 deletions examples/semaphore_wait.py
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

0 comments on commit 6a35949

Please sign in to comment.