locustio / locust

Write scalable load tests in plain Python 🚗💨
MIT License
23.9k stars 2.9k forks source link

Integrating with Locust #2646

Closed ichsansaid closed 1 week ago

ichsansaid commented 2 months ago

Prerequisites

Description

Hello,

I'm currently developing a Low-Code Development Platform (LCDP) and want to create a pre-built component for performance testing.

I'm using FastAPI as the platform's backend and Locust for performance testing.

I have an API called execute_scenario that executes tasks. For example, I can execute a scenario called "LocustScenario."

My approach is to use Locust as a library. So, each executed scenario of "LocustScenario" will create an environment and start the performance test. I also capture the statistics every second (using gevent.sleep) and send them to our message broker.

However, I'm facing two issues:

First Issue: monkey.patch_all() causes blocking in FastAPI. Therefore, I decided to remove it from the code manually in init.py for now. Second Issue: When I perform a performance test for HTTP, there are no issues. Our message broker receives the messages every second. However, when I perform a performance test for HTTPS, there is a delay of 3-5 seconds (somewhat randomly) when capturing the statistics every second using gevent.sleep(1).

Command line

library

Locustfile contents

def start(self, max_user: int, ramp: int, duration: int = 10, on_live_stats: Callable = None):
        try:
            gevent.spawn(NewOnLiveStats(on_live_stats), self)
            self.runner.start(max_user, spawn_rate=ramp)
            if duration > -1:
                gevent.spawn_later(duration, self.runner.quit)
            self.runner.greenlet.join()
        finally:
            self.on_finish(self)

This is the method that will start the environment

Python version

3.12

Locust version

latest

Operating system

windows

cyberw commented 2 months ago

Hi! It sounds like you are running locust in the same process as the thing you are testing? Dont do that!

If necessary, use process.Popen(”locust …”, …) in your application to launch locust in a separate process. But ideally, a load testing tool should never be running on the same machine as the thing you are testing.

ichsansaid commented 2 months ago

Hi! It sounds like you are running locust in the same process as the thing you are testing? Dont do that!

If necessary, use process.Popen(”locust …”, …) in your application to launch locust in a separate process. But ideally, a load testing tool should never be running on the same machine as the thing you are testing.

Hi cyber, thanks for the reply. I hope i can separate the Process with Popen but i can't because i want to get the stats in every 1 second. If i do that in different process, i can't get the stats. So that's why i run Locust as Library

cyberw commented 2 months ago

Why cant you get the stats in every second? Maybe you can do gevent.spawn(NewOnLiveStats(on_live_stats), self) in the test_start event handler? (idk what NewOnLiveStats is though :)

github-actions[bot] commented 1 week ago

This issue was closed because it has been stalled for 10 days with no activity. This does not necessarily mean that the issue is bad, but it most likely means that nobody is willing to take the time to fix it. If you have found Locust useful, then consider contributing a fix yourself!