Search code examples
pythonconcurrencygeventlocust

Using Gevent inside locust task increases exponentially


I have a web server whose REST API I want to load test. I use locust==2.5.1, python==3.8.2.

Locust Config:

  • Users:3
  • Spawn Rate: 3
from locust import HttpUser, task, between, constant_throughput
import configparser
from gevent.pool import Group


file_path = "./sample/sample.wav"
num_of_parallel_requests = 3

class FileTranscribeUser(HttpUser):
    @task(1)
    def file_transcribe_request(self):
        group = Group()
        
        for i in range(0, num_of_parallel_requests):
            group.spawn(lambda:self.client.post("/parse", 
                        files = {'audio': open(file_path, 'rb')}))

Expected results: Rquests per second to 2 always

Actual Results: Requests shot up from 0 to 66 and increasingly crashing my server that is running locally (locoust is also running locally).

I referred to this article for implementation:

Blockquote


Solution

  • If all you're looking to do is get 2 requests per second, I don't think that article is what you want at all.

    What you want to do is write your task so it only does your self.client.post() call appropriately. Then in your Locustfile, you need to set a wait_time (see the docs here) that will be used to limit the time between users starting and running your task. You probably want wait_time = constant_pacing(1). Then when you run Locust, give it 2 users and a spawn rate of 2. That will immediately start 2 users and only 2 users which will do your post call, wait one second, then be replaced by 2 users who will make the call again, giving you a constant 2 requests per second.