WKOW
WKOW

Reputation: 366

Locust: How to run task n times on n users and than stop locust run?

I have simple Locust script with one task with http request. I want to run this task 100 times on 10 users and than stop run script. Is there any simple way to do it. I know --run-time parameter but it only stop after the specified amount of time

Below my script:

    from locust import HttpUser, task, between


class QuickstartUser(HttpUser):
    wait_time = between(1, 2)
    host = "https://allegro.pl"

    @task(1)
    def getHome(self):
        self.client.get("/dzial/dom-i-ogrod", name = "Get Home and Garden")

Upvotes: 2

Views: 5075

Answers (2)

Cyberwiz
Cyberwiz

Reputation: 11426

Another option, provided by locust-plugins, is the -i parameter: https://github.com/SvenskaSpel/locust-plugins#command-line-options

It should be a little more reliable, as it explicitly calls runner.quit()

Upvotes: 4

Muhammed Tanır
Muhammed Tanır

Reputation: 197

If you are not running distributed you can have a global counter and increment it in the task and once it reaches the desired count you can stop the runner, like :

from locust import HttpUser, task, between

counter=0
class QuickstartUser(HttpUser):
    wait_time = between(1, 2)
    host = "https://allegro.pl"

    @task(1)
    def getHome(self):
        if counter == 100:
            self.environment.runner.stop()

        self.client.get("/dzial/dom-i-ogrod", name = "Get Home and Garden")
        counter = counter + 1`

and if you are running distributed it is best you use some external counter to keep track of requests like redis or something like that.

Upvotes: 4

Related Questions