Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce SimpleTaskSet, a way to make super simple locust files without having to define a Locust class #1274

Closed
wants to merge 9 commits into from
2 changes: 1 addition & 1 deletion locust/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .core import HttpLocust, Locust, TaskSet, TaskSequence, task, seq_task
from .core import HttpLocust, Locust, TaskSet, SimpleTaskSet, TaskSequence, task, seq_task
from .exception import InterruptTaskSet, ResponseError, RescheduleTaskImmediately
from .wait_time import between, constant, constant_pacing
from .event import Events
Expand Down
11 changes: 10 additions & 1 deletion locust/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ class TaskSetMeta(type):

def __new__(mcs, classname, bases, classDict):
new_tasks = []
subtask_of_simple_taskset = False
for base in bases:
if base.__name__ == "SimpleTaskSet": # check by name, because the class might not actually exist yet
subtask_of_simple_taskset = True
if hasattr(base, "tasks") and base.tasks:
new_tasks += base.tasks

Expand All @@ -265,7 +268,10 @@ def __new__(mcs, classname, bases, classDict):

classDict["tasks"] = new_tasks

return type.__new__(mcs, classname, bases, classDict)
cls = type.__new__(mcs, classname, bases, classDict)
if subtask_of_simple_taskset:
HttpLocust.task_set = cls
return cls

class TaskSet(object, metaclass=TaskSetMeta):
"""
Expand Down Expand Up @@ -546,3 +552,6 @@ def get_next_task(self):
task = self.tasks[self._index]
self._index = (self._index + 1) % len(self.tasks)
return task

class SimpleTaskSet(TaskSet):
pass
6 changes: 6 additions & 0 deletions locust/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .stats import (print_error_report, print_percentile_stats, print_stats,
stats_printer, stats_writer, write_stat_csvs)
from .util.timespan import parse_timespan
from .wait_time import constant
from .web import WebUI

_internals = [Locust, HttpLocust]
Expand Down Expand Up @@ -129,6 +130,11 @@ def main():
console_logger.info(" " + name)
sys.exit(0)

if not locusts and HttpLocust.task_set:
if not HttpLocust.wait_time:
HttpLocust.wait_time = constant(0)
locusts = {"HttpLocust": HttpLocust}

if not locusts:
logger.error("No Locust class found!")
sys.exit(1)
Expand Down