Locust class-picker mode auto reset statistic when ramping up users - python

currently I have started working with Locust. I follow the class-picker docs and practice with a simple test, but quickly realized that every time I increase the number of users during the test, Locust will reset the statistics table. Besides, Locust behevior of ramping up users is quite strange: instead of increasing from 2 users to 5 users, it set the number of users to 0 first and then increase to 5. Is that an obvious thing when running Locust in class-picker mode?
Here is test.py
from locust import HttpUser, constant, task
class MyReqRes1(HttpUser):
wait_time = constant(1)
host = "http://example.com"
#task
def get_users(self):
res = self.client.get("/hello")
print(res.status_code)
class MyReqRes2(HttpUser):
wait_time = constant(1)
host = "http://example.com"
#task
def get_users(self):
res = self.client.get("/world")
print(res.status_code)
And here is my command to run:
locust -f test.py --class-picker
I am trying to keep Locust ramping up users normally (the way it do without --class-picker arguments) and keep the statistic table as well.

The user class UI picker is designed to let you choose a user class to use for the test run. This means that user class will be used for the whole duration of the test. If you want to choose a different user class, you need to start a new test which results in the behavior you described: Locust stops all currently running users, resets the stats, switches user classes, starts the new test by spawning new users at your defined spawn rate to reach the number of desired users.
In other words, it's designed to let you have multiple different test scenarios defined in the same file and let you choose the one you want at run time.
The user class UI picker does not allow you to choose one user class, start a test and get X number of users, choose another class, add Y users, choose another class, add Z users, end up with X+Y+Z running users, which sounds like what you're trying to do. There is not currently a way to accomplish that.
Of course, you're welcome to put together a pull request with that kind of behavior and it can be reviewed and perhaps included in a future version.

Related

Django Schedule/run tasks dynamically at particular time periodically from user's order in app (without celery)

I am creating a Django App where the user can schedule some tasks to happen at a particular time.
for example. in google calendar we tell google what we will be doing tomorrow and then at right time it sends us a notification.
similarly here I want it to be flexible, for instance, the user can tell Django the time and function to run.
Django will wait for the time and then run the function.
like he said turn off lights at 12 pm
then Django will do it.
or for example:-
user says remind me to go to the gym in 30 minutes
And then after 30 minutes, he gets a notification.
Actually the tasks are added dynamically so we can't hardcode them at first.
code:-
skills.py # all the functions defined by user are imported in it
# for example take this task
def turn_off_light(room, id, *args, **kwargs):
# turned off light using python (your logic)
print(f"light no {id} is turned off!")
there's a Django model known as Function in which this function is stored and the user can access it easily.
I want users to select this function within the application and also give parameters and time and Django will run it at that time!
In short what I need is that the user from within the application is able to set the time of a function(or task) to run at a particular time(maybe periodically or once or maybe in the situation) and Django runs it on that particular time.
Note: user will also give args and kwargs(I am taking care of that). All I need is Django run the function with those args and kwargs.
(only Django method without celery or something will be appreciated)
Thanks!
Without Celery and using Django the best way to do is to create custom django-admin commands with Cron
For example :
Create customer command called calendar_routine.py
Create a cron schedule to call your function from your server at a given time
Otherwise there is no way to do it in pure Python/Django

Django real time jobs

How Can I create real time actions in python / django?
for more info:
Some user add some thing to database also other user add same one too (not same as they are but they have similar property) at same time (and all times) program should being check if they are objects with similar property {if they are not same check both of them in other time with all other objects that may be added/edited on database}
these actions should be in real time or at last by few minuts appart.
for example:
for every(2min):
do_job()
or
while True:
do_job()
if i use second one program will stop.
You need to run a async task to check the objects in background. You can check this link for reference Celery doc
In case if you have any limitations using celery or a similar approach, the other way is to create a scripts.py inside your app(same level as models.py & views.py) and write logic and schedule this in cron or any scheduler based on your host server.

Locust Load Testing running into infinite loop

i am really new to Python with Locust load testing.
I Created the below Python script but its running into infinite loop.
I have used
Number of total users to simulate = 1
Spawn rate = 1
Please let me know where i am going wrong.
from locust import HttpUser, task, between, TaskSet
class Behaviour(TaskSet):
#task
def first_task(self):
self.client.get('/LoginHome.aspx')
class WebsiteTestUser(HttpUser):
tasks = [Behaviour]
wait_time = between(5, 15)
host = "http://DEV/LoadTesting"
Thanks.
It doesn’t look like you’ve done anything wrong. I assume by “infinite loop” you mean that you tell it to spawn 1 user and then one user starts but then you get repeated requests to your endpoint that you defined in your task until you stop Locust.
This is by design. When one Locust user finishes its tasks, a new one is spawned in its place. Locust will try to keep the specified number of users running indefinitely. As a load test tool, you’re not telling it to run the defined tasks X number of times, you define a user flow from start to finish and then tell it the number of users you want to throw at your system you’re testing to make sure/find out if your system can handle the load you want.

Get Locust IO info for each generated user

The company I work on compromises to deliver 99% of their service responses in less than 1 second and 99.9% or their responses in less that 2 seconds. How can I make Locust report if this rule has been broken for any of the virtual users?
My first approach would be to make a method in my user (inherited
from locust.HttpLocust) that will detect when this event happens and
record it in a user-based log. I think this would work but if I have
1000 user it means I will have 1000 different log files.
A second approach would be to create a single event log, but I guess
that would require me to deal with asynchronos IO handling. I guess
there must be a more pythonesque way.
Locust and performance newbie here. Sorry if my question is misguided.
You can add duration checks at the end of each #task like:
#task
def service_request(self):
r = self.client.get("/your/service/path")
assert r.elapsed < datetime.timedelta(seconds = 1), "Request took more than 1 second"
This way you will have a report on individual HTTP Requests level with regards to which ones are successful and which tool > 1 second.
More information: Locust Assertions - A Complete User Manual
Alternatively you can try considering running your test using Taurus tool as a wrapper. Taurus has powerful and flexible Pass/Fail Criteria subsystem which analyses the results on-the-fly and returns a non-zero exit status code which can be used as an indicator of failure for shell scripts or continuous integration solutions.

django celery for long running task

I have list of players like :
player_list = Participant.objects.all()
participant_count = player_list.count()
I want to randomly select winner from this like:
winner_index = random.randint(0, participant_count-1)
winner = player_list[winner_index]
Lets say I have one million participant then I guess It will take long time to randomly generate winner. Till then my site will be hang I guess.
For this purpose should I use celery or its fine? What if my site go hang for few minutes and only display winner. Any suggestion ?
With proper indexing your database should be able to handle this without needing any special workarounds. If you make it asynchronous with celery, then you won't be able to include that data in your standard request/response cycle.
If you're worried about page speed for the user, you could load a page without the winner, then do an ajax call using javascript to get the winner and update the page, allowing you to display a loading message to the user while they wait.

Categories