-
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.
Merge pull request #1310 from locustio/sequential-taskset
Replace TaskSequence and @seq_task with SequentialTaskSet
- Loading branch information
Showing
8 changed files
with
213 additions
and
162 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
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
from .core import HttpLocust, Locust, TaskSet, TaskSequence, task, seq_task | ||
from .core import HttpLocust, Locust, TaskSet, task | ||
from .event import Events | ||
from .exception import InterruptTaskSet, ResponseError, RescheduleTaskImmediately | ||
from .sequential_taskset import SequentialTaskSet | ||
from .wait_time import between, constant, constant_pacing | ||
from .event import Events | ||
|
||
events = Events() | ||
|
||
__version__ = "0.14.5" |
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,56 @@ | ||
from .core import TaskSet, TaskSetMeta | ||
from .exception import LocustError | ||
|
||
|
||
class SequentialTaskSetMeta(TaskSetMeta): | ||
""" | ||
Meta class for SequentialTaskSet. It's used to allow SequentialTaskSet classes to specify | ||
task execution in both a list as the tasks attribute or using the @task decorator | ||
We use the fact that class_dict order is the order of declaration in Python 3.6 | ||
(See https://www.python.org/dev/peps/pep-0520/) | ||
""" | ||
def __new__(mcs, classname, bases, class_dict): | ||
new_tasks = [] | ||
for base in bases: | ||
# first get tasks from base classes | ||
if hasattr(base, "tasks") and base.tasks: | ||
new_tasks += base.tasks | ||
for key, value in class_dict.items(): | ||
if key == "tasks": | ||
# we want to insert tasks from the tasks attribute at the point of it's declaration | ||
# compared to methods declared with @task | ||
if isinstance(value, list): | ||
new_tasks.extend(value) | ||
else: | ||
raise ValueError("On SequentialTaskSet the task attribute can only be set to a list") | ||
|
||
if "locust_task_weight" in dir(value): | ||
# method decorated with @task | ||
new_tasks.append(value) | ||
|
||
class_dict["tasks"] = new_tasks | ||
return type.__new__(mcs, classname, bases, class_dict) | ||
|
||
|
||
class SequentialTaskSet(TaskSet, metaclass=SequentialTaskSetMeta): | ||
""" | ||
Class defining a sequence of tasks that a Locust user will execute. | ||
Works like TaskSet, but task weight is ignored, and all tasks are executed in order. Tasks can | ||
either be specified by setting the *tasks* attribute to a list of tasks, or by declaring tasks | ||
as methods using the @task decorator. The order of declaration decides the order of execution. | ||
It's possible to combine a task list in the *tasks* attribute, with some tasks declared using | ||
the @task decorator. The order of declaration is respected also in that case. | ||
""" | ||
def __init__(self, *args, **kwargs): | ||
super().__init__(*args, **kwargs) | ||
self._task_index = 0 | ||
|
||
def get_next_task(self): | ||
if not self.tasks: | ||
raise LocustError("No tasks defined. use the @task decorator or set the tasks property of the SequentialTaskSet") | ||
task = self.tasks[self._task_index % len(self.tasks)] | ||
self._task_index += 1 | ||
return task |
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,120 @@ | ||
from locust.core import Locust, task | ||
from locust.sequential_taskset import SequentialTaskSet | ||
from locust.exception import RescheduleTask | ||
from locust.wait_time import constant | ||
from .testcases import LocustTestCase | ||
|
||
|
||
class TestTaskSet(LocustTestCase): | ||
def setUp(self): | ||
super(TestTaskSet, self).setUp() | ||
class User(Locust): | ||
host = "127.0.0.1" | ||
wait_time = constant(0) | ||
_catch_exceptions = False | ||
self.locust = User(self.environment) | ||
|
||
def test_task_sequence_with_list(self): | ||
log = [] | ||
|
||
def t1(ts): | ||
log.append(1) | ||
def t2(ts): | ||
log.append(2) | ||
def t3(ts): | ||
log.append(3) | ||
ts.interrupt(reschedule=False) | ||
|
||
class MyTaskSequence(SequentialTaskSet): | ||
tasks = [t1, t2, t3] | ||
|
||
l = MyTaskSequence(self.locust) | ||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertEqual([1,2,3], log) | ||
|
||
def test_task_sequence_with_methods(self): | ||
log = [] | ||
class MyTaskSequence(SequentialTaskSet): | ||
@task | ||
def t1(self): | ||
log.append(1) | ||
@task | ||
def t2(self): | ||
log.append(2) | ||
@task(1) | ||
def t3(self): | ||
log.append(3) | ||
self.interrupt(reschedule=False) | ||
|
||
l = MyTaskSequence(self.locust) | ||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertEqual([1,2,3], log) | ||
|
||
def test_task_sequence_with_methods_and_list(self): | ||
log = [] | ||
def func_t1(ts): | ||
log.append(101) | ||
def func_t2(ts): | ||
log.append(102) | ||
class MyTaskSequence(SequentialTaskSet): | ||
@task | ||
def t1(self): | ||
log.append(1) | ||
@task | ||
def t2(self): | ||
log.append(2) | ||
|
||
tasks = [func_t1, func_t2] | ||
|
||
@task(1) | ||
def t3(self): | ||
log.append(3) | ||
self.interrupt(reschedule=False) | ||
|
||
l = MyTaskSequence(self.locust) | ||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertEqual([1,2, 101,102,3], log) | ||
|
||
def test_task_sequence_with_inheritance(self): | ||
log = [] | ||
class TS1(SequentialTaskSet): | ||
@task | ||
def t1(self): | ||
log.append(1) | ||
tasks = [lambda ts: log.append(30)] | ||
class TS2(TS1): | ||
tasks = [lambda ts: log.append(20)] | ||
@task | ||
def t2(self): | ||
log.append(2) | ||
class TS3(TS2): | ||
@task | ||
def t3(self): | ||
log.append(3) | ||
self.interrupt(reschedule=False) | ||
|
||
l = TS3(self.locust) | ||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertEqual([1,30,20,2,3], log) | ||
|
||
def test_task_sequence_multiple_iterations(self): | ||
log = [] | ||
class TS(SequentialTaskSet): | ||
iteration_count = 0 | ||
@task | ||
def t1(self): | ||
log.append(1) | ||
@task | ||
def t2(self): | ||
log.append(2) | ||
@task(1) | ||
def t3(self): | ||
log.append(3) | ||
self.iteration_count += 1 | ||
if self.iteration_count == 3: | ||
self.interrupt(reschedule=False) | ||
|
||
l = TS(self.locust) | ||
self.assertRaises(RescheduleTask, lambda: l.run()) | ||
self.assertEqual([1,2,3,1,2,3,1,2,3], log) | ||
|
Oops, something went wrong.