-
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
question about putting a delay #139
Comments
Hi!
On the example on http://locust.io, the WebSiteUser class sets the attributes min_wait and max_wait. This is the time (in milliseconds) that each Locust user will wait between executing a task. You could also make the locust user wait within a task by simply calling the sleep function e.g: import time
class MyTaskSet(TaskSet):
@task
def my_task(self):
# do something
time.sleep(10.0) # sleep 10 seconds
# do something else
Locust uses requests (http://python-requests.org) for HTTP, so when you do an HTTP request you'll get back a requests Response object (http://docs.python-requests.org/en/latest/api/#requests.Response). You can read the content of the response by accessing the content attribute on the Response object. What you do from there is totally up to you. For example, you could use pyquery (http://pythonhosted.org/pyquery/) to parse the product IDs and save them on the locust user instance, and then access those product IDs in some other locust task. Remember that it's just python code. Locust just provides you with a way of simulating user behaviour, and then spawning a lot of those users, while collecting request/response statistics. |
Thanks for the quick example and tips, They help. On the extracted ID, I can put it in a python global variable, but each task will do this and step over each other. So I need to save it to a per-task variable, right? |
You can store state that is specific to each simulated user, on the Locust instances. Like this: from locust import HttpLocust, TaskSet, task
class UserBehaviour(TaskSet):
@task
def list_products(self):
response = self.client.get("/products/")
self.locust.product_ids = parse_product_ids(response.content) # assuming a parse_product_ids() function existed
@task
def some_other_task(self):
if self.locust.product_ids:
# here we can read the product ids:
print "product_ids:", self.locust.product_ids
class User(HttpLocust):
task_set = UserBehaviour
product_ids = None |
Thanks a lot! It helps. Last question in this thread: Locust can scale much more than implementations by using regular threads, would like to know if there is any limitation compared to the regular thread approach. For example, can I do the following? class UserBehaviour(TaskSet):
@task
def list_products(self):
response = self.client.get("/products/")
self.locust.product_ids = parse_product_ids(response.content)
if self.locust.product_ids.length >2:
#find the last productId, put it in variable last
response = self.client.get("/products/?id=%d" % lastId)
else:
response = self.client.get("/products/?id=1")
#do something else..
.... |
Sure, that should be perfectly fine. The main reason why you can simulate a lot more users, than if you were using a threaded solution, is that Locust uses greenlets which has a much smaller memory footprint than threads. |
Nice. Thanks! |
Thanks for the tip around the bug of locust file name (can't be called locust.py), I am able to run the test.
In the simple example given on http://locust.io/, wonder how to put a sleep for 3sec between get("/") and get("/about").
Another question is, how do I extract products from the server response for get("/products") and use the productIDs in the subsequent request like
The server sends responses like
Thanks.
The text was updated successfully, but these errors were encountered: