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

Execution order? task lifetime? #195

Closed
jeffandersen opened this issue Sep 11, 2014 · 8 comments
Closed

Execution order? task lifetime? #195

jeffandersen opened this issue Sep 11, 2014 · 8 comments

Comments

@jeffandersen
Copy link

I'm trying to use Locust to test the lifecycle of a user on top of my API. The lifecycle being, create, a heartbeat, update requests and finally a destroy/end of the session; however, I'm not entirely clear on the lifecycle of the tasks.

Locust spawns a greenlet for each "user" which runs the task set, correct?
So if I define specific actions within a single task, they should operate synchronously?
And then the greenlet is destroyed/ended when the tasks have completed?

Illustrated in code: Given a single task in a behavior set, each spawned user should create their session, heartbeat for 60 seconds and then end their session.

import time


@task
def session(self):
    client = SessionClient()
    session = client.create()

    for sec in range(0, 60):
        session.heartbeat()
        time.sleep(1)

    session.end()

Am I approaching this correctly?

@heyman

@Jahaja
Copy link
Member

Jahaja commented Sep 11, 2014

Locust spawns a greenlet for each "user" which runs the task set, correct?

Correct.

So if I define specific actions within a single task, they should operate synchronously?

Correct.

And then the greenlet is destroyed/ended when the tasks have completed?

The task will restart after some wait time. If you want to kill the Locust you can raise StopLocust.

@heyman
Copy link
Member

heyman commented Sep 11, 2014

And then the greenlet is destroyed/ended when the tasks have completed?

Though possible, Locust instances are not designed to die or complete until the test has finished. If they die, Locust will not spawn a new one.

If you want to simulate users starting and ending sessions, I would recommend you to have a task that ends the session and starts a new one in the same Locust instance, and thus recycle the existing locusts/greenlets that have been spawned. Then weight that task appropriately to control how likely it is that a new session will be started.

Something like this:

class UserBehaviour(TaskSet):
    def on_start(self):
        self.new_session()

    @task(1)
    def new_session():
        if self.locust.session:
            self.locust.session.end()
        self.locust.session.create()
    ...

@jeffandersen
Copy link
Author

@heyman @Jahaja thanks for the feedback/information.

@jeffandersen
Copy link
Author

@heyman @Jahaja me again.

I'm seeing some issues with the lifecycle in my behavior where the test goes on forever, but it shouldn ultimately finish, correct? I've modeled it off of your example, @heyman, where the session is terminated when the Task is re-used.

I also played around with raising StopLocust on complete (after calling my session end) which resulted in the test reducing down to 0 clients and just sitting there (not giving the final stats list).

I guess I'm just not completely clear on what constitutes the Locust suite "completing". Any insight that might help me wrap my head around it?

@Jahaja
Copy link
Member

Jahaja commented Sep 15, 2014

Except when setting a maximum number of requests using --num-request there's no concept of a locust test completing. The locust test will run until you stop it.

@jeffandersen
Copy link
Author

I must have been setting that previously when I was seeing it completing. Hm.

@gbrahmanandam
Copy link

How do we publish stats every hour without stopping locust. I am running several threads and I want to collect the stats for each thread, rather than aggregated_stats(). Any suggestions/help?

@harkiranjeetkaur
Copy link

How do we publish stats every hour without stopping locust. I am running several threads and I want to collect the stats for each thread, rather than aggregated_stats(). Any suggestions/help?

Did you get any way to do this?

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

5 participants