locust-TaskSet class vs function task

4k views Asked by At

I'm new to locust and I'm trying to write load tests. I was wondering what is the difference between defining the tasks I want to do as callable functions rather then defining them as locust TaskSet classes. for example:

class MyTaskSet(TaskSet):
    @task(2)
    def index(self):
       self.client.get("/")

   @task(1)
      def about(self):
        self.client.get("/about/")

class MyLocust(HttpLocust):
    task_set = MyTaskSet

or:

class about(TaskSet)
   @task
   def about(self):
        self.client.get("/about/")
        self.interrupter()

class index(TaskSet)
    @task
    def index(self)
        self.client.get("/")
        self.interrupter()

class MyTaskSet(TaskSet)
     tasks = {index:2 , about: 1}

class MyLocust(HttpLocust):
    task_set = MyTaskSet

what is the difference between the 2 above? thanks

1

There are 1 answers

0
TomVW On

TL;DR

There's a huge difference between the two in terms of load distribution.

Detailed explanation

Imagine the following setup, where one task is much slower than the other:

class MyTaskSet(TaskSet):
    @task(2)
    def fast(self):
        self.locust.client.get("/api",name="fast_check")

    @task(1)
    def slow(self):
        time.sleep(1)
        self.locust.client.get("/api",name="slow_check")

class MyLocust(HttpLocust):
    task_set = MyTaskSet

Below is the same setup with nested TaskSets:

class Fast(TaskSet):
    @task(1)
    def fast(self):
        self.locust.client.get("/api",name="fast_check")

class Slow(TaskSet):
    @task(1)
    def slow(self):
        time.sleep(1)
        self.locust.client.get("/api",name="slow_check")


class MyTaskSet(TaskSet):
    tasks = {Fast:2 , Slow: 1}


class MyLocust(HttpLocust):
    task_set = MyTaskSet

When you run both scripts, you will notice that for the first setup, the load is distributed roughly as you weigh your tasks. 1/3 of the HTTP calls goes to the slow_check task and 2/3 to the fast_check task.

However, in the second setup, way more HTTP calls go to the fast_check. It looks as if a Locust (= a user) is assigned to a TaskSet, according to the 1/3-2/3 weight, but as the Locust finishes much faster, it can schedule a next task from the Fast TaskSet much faster as well.

I'm not sure if this is a bug or a feature, as it isn't mentioned specifically in the documentation.