-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Adding ability to generate any custom load shape with LoadTestShape class #1505
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
673db60
Initial commit with shaper
max-rocket-internet fa4c70d
renaming to shape, add some logging, examples
max-rocket-internet 1782959
fix existing tests by adjusting param order
max-rocket-internet b5e9fdc
add test to ensure LoadTestShape is not in user_classes
max-rocket-internet 428c67f
add a test
max-rocket-internet c53b625
exit with error if a conflicting argument is specified
max-rocket-internet 956a629
Make formatting and style consistent
max-rocket-internet efcef24
Remove double log
max-rocket-internet bf932ae
Add test for scaling down also
max-rocket-internet f593eb2
Revert removing \n
max-rocket-internet 1e20f88
rewrite DoubleWave example
max-rocket-internet d84d0c0
simplify stages example
max-rocket-internet 37271ec
y u round, no round
max-rocket-internet f921aea
Simplify examples, return None to stop test
max-rocket-internet e32ec31
is None? Yes is None.
max-rocket-internet 36738ad
disable editing running test. Print message about user_count/hatch_ra…
max-rocket-internet ecad2f7
Adding doc
max-rocket-internet e3ec440
update docs, change one word in log message
max-rocket-internet 93a41ee
Add full test with workers and master
max-rocket-internet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
.. _generating-custom-load-shape: | ||
|
||
================================= | ||
Generating a custom load shape using a `LoadTestShape` class | ||
================================= | ||
|
||
Sometimes a completely custom shaped load test is required that cannot be achieved by simply setting or changing the user count and hatch rate. For example, generating a spike during a test or ramping up and down at custom times. In these cases using the `LoadTestShape` class can give complete control over the user count and hatch rate at all times. | ||
|
||
How does a `LoadTestShape` class work? | ||
--------------------------------------------- | ||
|
||
Define your class inheriting the `LoadTestShape` class in your locust file. If this type of class is found then it will be automatically used by Locust. Add a `tick()` method to return either a tuple containing the user count and hatch rate or `None` to stop the load test. Locust will call the `tick()` method every second and update the load test with new settings or stop the test. | ||
|
||
Examples | ||
--------------------------------------------- | ||
|
||
There are also some [examples on github](https://github.com/locustio/locust/tree/master/examples/custom_shape) including: | ||
|
||
- Generating a double wave shape | ||
- Time based stages like K6 | ||
- Step load pattern like Visual Studio | ||
|
||
Here is a simple example that will increase user count in blocks of 100 then stop the load test after 10 minutes: | ||
|
||
```python | ||
class MyCustomShape(LoadTestShape): | ||
time_limit = 600 | ||
hatch_rate = 20 | ||
|
||
def tick(self): | ||
run_time = self.get_run_time() | ||
|
||
if run_time < self.time_limit: | ||
user_count = round(run_time, -2) | ||
return (user_count, hatch_rate) | ||
|
||
return None | ||
``` |
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,43 @@ | ||
import math | ||
from locust import HttpUser, TaskSet, task, constant | ||
from locust import LoadTestShape | ||
|
||
|
||
class UserTasks(TaskSet): | ||
@task | ||
def get_root(self): | ||
self.client.get("/") | ||
|
||
|
||
class WebsiteUser(HttpUser): | ||
wait_time = constant(0.5) | ||
tasks = [UserTasks] | ||
|
||
|
||
class DoubleWave(LoadTestShape): | ||
""" | ||
A shape to immitate some specific user behaviour. In this example, midday | ||
and evening meal times. | ||
|
||
Settings: | ||
min_users -- minimum users | ||
peak_one_users -- users in first peak | ||
peak_two_users -- users in second peak | ||
time_limit -- total length of test | ||
""" | ||
|
||
min_users = 20 | ||
peak_one_users = 60 | ||
peak_two_users = 40 | ||
time_limit = 600 | ||
|
||
def tick(self): | ||
run_time = round(self.get_run_time()) | ||
|
||
if run_time < self.time_limit: | ||
user_count = ((self.peak_one_users - self.min_users) * math.e ** -((run_time/(self.time_limit/10*2/3))-5) ** 2 | ||
+ (self.peak_two_users - self.min_users) * math.e ** - ((run_time/(self.time_limit/10*2/3))-10) ** 2 | ||
+ self.min_users) | ||
return (round(user_count), round(user_count)) | ||
else: | ||
return None |
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,49 @@ | ||
from locust import HttpUser, TaskSet, task, constant | ||
from locust import LoadTestShape | ||
|
||
|
||
class UserTasks(TaskSet): | ||
@task | ||
def get_root(self): | ||
self.client.get("/") | ||
|
||
|
||
class WebsiteUser(HttpUser): | ||
wait_time = constant(0.5) | ||
tasks = [UserTasks] | ||
|
||
|
||
class StagesShape(LoadTestShape): | ||
""" | ||
A simply load test shape class that has different user and hatch_rate at | ||
different stages. | ||
|
||
Keyword arguments: | ||
|
||
stages -- A list of dicts, each representing a stage with the following keys: | ||
duration -- When this many seconds pass the test is advanced to the next stage | ||
users -- Total user count | ||
hatch_rate -- Hatch rate | ||
stop -- A boolean that can stop that test at a specific stage | ||
|
||
stop_at_end -- Can be set to stop once all stages have run. | ||
""" | ||
|
||
stages = [ | ||
{'duration': 60, 'users': 10, 'hatch_rate': 10}, | ||
{'duration': 100, 'users': 50, 'hatch_rate': 10}, | ||
{'duration': 180, 'users': 100, 'hatch_rate': 10}, | ||
{'duration': 220, 'users': 30, 'hatch_rate': 10}, | ||
{'duration': 230, 'users': 10, 'hatch_rate': 10}, | ||
{'duration': 240, 'users': 1, 'hatch_rate': 1}, | ||
] | ||
|
||
def tick(self): | ||
run_time = self.get_run_time() | ||
|
||
for stage in self.stages: | ||
if run_time < stage["duration"]: | ||
tick_data = (stage["users"], stage["hatch_rate"]) | ||
return tick_data | ||
|
||
return None |
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,43 @@ | ||
import math | ||
from locust import HttpUser, TaskSet, task, constant | ||
from locust import LoadTestShape | ||
|
||
|
||
class UserTasks(TaskSet): | ||
@task | ||
def get_root(self): | ||
self.client.get("/") | ||
|
||
|
||
class WebsiteUser(HttpUser): | ||
wait_time = constant(0.5) | ||
tasks = [UserTasks] | ||
|
||
|
||
class StepLoadShape(LoadTestShape): | ||
""" | ||
A step load shape | ||
|
||
|
||
Keyword arguments: | ||
|
||
step_time -- Time between steps | ||
step_load -- User increase amount at each step | ||
hatch_rate -- Hatch rate to use at every step | ||
time_limit -- Time limit in seconds | ||
|
||
""" | ||
|
||
step_time = 30 | ||
step_load = 10 | ||
hatch_rate = 10 | ||
time_limit = 600 | ||
|
||
def tick(self): | ||
run_time = self.get_run_time() | ||
|
||
if run_time > self.time_limit: | ||
return None | ||
|
||
current_step = math.floor(run_time / self.step_time) + 1 | ||
return (current_step * self.step_load, self.hatch_rate) |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a None entry could be better used to signal the end of the test? Because you'll never need to do (x, y, True) for any other values of x or y than zero, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well we need to return at least
(Int, Int)
to edit the running test so you are saying to stop the test return(None, None)
? Or(Int, Int, None)
? Or something else?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant that tick() should do
return None
instead ofreturn (0, 0, True)
(which of course requires modifications in runners.py on line 297 as wel)But, I also think that stop_at_end is an unnecessary complication. If you want to run your test forever, cant you just set the last step to use
'duration': 9999999
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is just an example of what I imagine someone might want to use or see before they write their own. But I can remove
stop_at_end
it to make it simpler, no worries.I would say no, that's not very precise. What if you forget it's running and it gets to 10000000 seconds😃
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to reiterate, these are examples. I imagine they would be in the docs or just here for people to find. They aren't to be copy/pasted and used as they are. I just imagined them to help a beginner to see how this class could be used 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Lets not spend too much time on them. On the other hand, if they are just examples then it makes sense to keep them very simplified (and let our users worry about edge cases like how to make it run forever etc).