Handle task weights efficiently in TaskSet
s
#2820
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #2651
After realising that my initial idea of storing a dictionary of task weights in
TaskSet
would not work due to incompatibility withSequentialTaskSet
and the rest of the code (many things in the codebase assumetasks
is a list, refactoring which will be too much work), I started from scratch compared to #2741 and here are the changes:TaskSet
andSequentialTaskSet
now use the same metaclass, which uses the sameget_tasks_from_base_classes
methodSequentialTaskSet
is moved to thetask.py
, since keeping it in a separate file is no longer sensibleget_tasks_from_base_classes
method will process all tasks in order they are declared and put them in a list (only one of each), adding alocust_task_weight
attribute with the corresponding weight if it is missing (which now all tasks are thus guaranteed to have)TaskSet
andSequentialTaskSet
will process this list of tasks as they need, adding necessary data structures (in new_setup_tasks()
method, see belowTaskSet
will calculate the cumulative weights necessary forrandom.choices
SequentialTaskSet
will expand thetasks
list into a list where tasks are duplicated according to the weight and make a cycle out of it@task
decorator can now receive also afloat
as a weightTo facilitate these changes, also inheritance of classes is changed:
AbstractTaskSet
class, which keeps most of the functionality that was previously in theTaskSet
(init, run, wait_time etc);TaskSet
,SequentialTaskSet
andDefaultTaskSet
all inherit this class_setup_tasks()
andget_next_task()
which concrete classes need to implement_setup_tasks()
is necessary since the originaltasks
attribute can be filtered using tags after class instantiation, so the actual preparation of additional data structures needs to happen later; this method is called byrun()
(also, some tests needed to call it explicitly since they check some things without callingrun()
, but this should not be a problemget_next_task()
is the method that provides the next task torun()
. Arguably, this is where the specialized logic of a newTaskSet
class would be implementedtasks
is empty is now the responsibility ofrun()
during its start. It is, after all, only necessary to do once and not on every call toget_next_task()
. If there are no tasks,LocustError
is raised. It is also raised byget_tasks_from_base_classes
iftasks
is not a dict or a list. Tests are also modified to check for this specific error rather thanException
.Test are modified to account for these changes.
I think this refactoring makes it easier to introduce new specialized Task Sets, since there is now a unified data model/interface that they should accommodate.