Skip to content
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

Closed
user1020 opened this issue Apr 6, 2014 · 6 comments
Closed

question about putting a delay #139

user1020 opened this issue Apr 6, 2014 · 6 comments

Comments

@user1020
Copy link

user1020 commented Apr 6, 2014

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

get("/products?id=<extractedId>")

The server sends responses like

<li productId="123321">mountain bike (I)</productId>

Thanks.

@heyman
Copy link
Member

heyman commented Apr 6, 2014

Hi!

In the simple example given on http://locust.io/, wonder how to put a sleep for 3sec between get("/") and get("/about").

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

Another question is, how do I extract products from the server response for get("/products") and use the productIDs in the subsequent request like

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.

@user1020
Copy link
Author

user1020 commented Apr 6, 2014

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?
Any examples on this? Thanks.

@heyman
Copy link
Member

heyman commented Apr 6, 2014

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

@user1020
Copy link
Author

user1020 commented Apr 6, 2014

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..
....

@heyman
Copy link
Member

heyman commented Apr 8, 2014

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.

@user1020
Copy link
Author

user1020 commented Apr 8, 2014

Nice. Thanks!

@user1020 user1020 closed this as completed Apr 8, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants