-
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.
* Add support for tasks sequence * Add TaskSequence documentation * Add TaskSequence and seq_task to the __init__.py
- Loading branch information
Showing
6 changed files
with
220 additions
and
1 deletion.
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
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,50 @@ | ||
# This locust test script example will simulate a user | ||
# browsing the Locust documentation on https://docs.locust.io/ | ||
|
||
import random | ||
from locust import HttpLocust, TaskSquence, seq_task, task | ||
from pyquery import PyQuery | ||
|
||
|
||
class BrowseDocumentationSequence(TaskSquence): | ||
def on_start(self): | ||
self.urls_on_current_page = self.toc_urls | ||
|
||
# assume all users arrive at the index page | ||
@seq_task(1) | ||
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 | ||
] | ||
|
||
@seq_task(2) | ||
@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 | ||
] | ||
|
||
@seq_task(3) | ||
@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 = BrowseDocumentationSequence | ||
host = "https://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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
from .core import HttpLocust, Locust, TaskSet, task | ||
from .core import HttpLocust, Locust, TaskSet, TaskSequence, task, seq_task | ||
from .exception import InterruptTaskSet, ResponseError, RescheduleTaskImmediately | ||
|
||
__version__ = "0.8.1" |
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,74 @@ | ||
import six | ||
|
||
from locust import InterruptTaskSet, ResponseError | ||
from locust.core import HttpLocust, Locust, TaskSequence, events, seq_task, task | ||
from locust.exception import (CatchResponseError, LocustError, RescheduleTask, | ||
RescheduleTaskImmediately) | ||
|
||
from .testcases import LocustTestCase, WebserverTestCase | ||
|
||
|
||
class TestTaskSet(LocustTestCase): | ||
def setUp(self): | ||
super(TestTaskSet, self).setUp() | ||
|
||
class User(Locust): | ||
host = "127.0.0.1" | ||
self.locust = User() | ||
|
||
def test_task_sequence_with_list(self): | ||
def t1(l): | ||
if l._index == 1: | ||
l.t1_executed = True | ||
|
||
def t2(l): | ||
if l._index == 2: | ||
l.t2_executed = True | ||
|
||
def t3(l): | ||
if l._index == 0: | ||
l.t3_executed = True | ||
raise InterruptTaskSet(reschedule=False) | ||
|
||
class MyTaskSequence(TaskSequence): | ||
t1_executed = False | ||
t2_executed = False | ||
t3_executed = False | ||
tasks = [t1, t2, t3] | ||
|
||
l = MyTaskSequence(self.locust) | ||
|
||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertTrue(l.t1_executed) | ||
self.assertTrue(l.t2_executed) | ||
self.assertTrue(l.t3_executed) | ||
|
||
def test_task_with_decorator(self): | ||
class MyTaskSequence(TaskSequence): | ||
t1_executed = 0 | ||
t2_executed = 0 | ||
t3_executed = 0 | ||
|
||
@seq_task(1) | ||
def t1(self): | ||
if self._index == 1: | ||
self.t1_executed += 1 | ||
|
||
@seq_task(2) | ||
@task(3) | ||
def t2(self): | ||
if self._index == 2 or self._index == 3 or self._index == 4: | ||
l.t2_executed += 1 | ||
|
||
@seq_task(3) | ||
def t3(self): | ||
if self._index == 0: | ||
self.t3_executed += 1 | ||
raise InterruptTaskSet(reschedule=False) | ||
|
||
l = MyTaskSequence(self.locust) | ||
|
||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertEquals(l.t1_executed, 1) | ||
self.assertEquals(l.t2_executed, 3) | ||
self.assertEquals(l.t3_executed, 1) |