locust icon indicating copy to clipboard operation
locust copied to clipboard

Use `random.choices` when selecting a task instead of `random.choice`

Open bakhtos opened this issue 1 year ago • 4 comments

Prerequisites

Description

According to current implementation/documentation, if weights are given to tasks, tasks are stored in the list duplicated by the amount given as weight, and then the task is selected from the list by using Python's random.choice. This, however, has two shortcomings:

  • Inserting big weights can lead to unreasonably big datastructures
  • Only integers can be weights

It should be fairly easy to rewrite the gen_tasks_from_base_classes function and how it's output is handled in TaskSet class to store the weights separately and pass them together with the list of tasks to random.choices in gen_next_task.

This will allow to store arbitrarily large weights without duplicating tasks in integer or float as well as potentially enable the functionality to modify task weights at runtime by modifying the list storing the weights.

I could try to implement this with a PR if this change is desired.

bakhtos avatar Mar 25 '24 14:03 bakhtos

Sounds great! Looking forward to your PR!

cyberw avatar Mar 25 '24 17:03 cyberw

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar May 25 '24 01:05 github-actions[bot]

@bakhtos Any chance you’ll have time to take a look?

cyberw avatar May 25 '24 16:05 cyberw

@cyberw Yes I actually finally got around to working on this two days ago! I think I already changed the list to the dict in the class itself, now I need to figure out which external things are affected, like helper functions and tests.

bakhtos avatar May 26 '24 12:05 bakhtos

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 10 days.

github-actions[bot] avatar Jul 26 '24 01:07 github-actions[bot]

This issue was closed because it has been stalled for 10 days with no activity. This does not necessarily mean that the issue is bad, but it most likely means that nobody is willing to take the time to fix it. If you have found Locust useful, then consider contributing a fix yourself!

github-actions[bot] avatar Aug 05 '24 01:08 github-actions[bot]