Stress/Load testing

Hi,

I way wondering if there is any way to load test my dash app with a set of simulated users so that I can find out, how many users my app can handle for computational heavy tasks.

The only thing I found so far is this thread about JMeter.

For end-to-end tests I am using the playwright plugin for pytest which does not seem to contain any option to run tests in parallel besides --numprocesses which is not what I am looking as it is made for speeding up testing and not multi-user tests.

This is my minimal setup:

  1. App:
from dash import Dash, html, callback
from dash.dependencies import Input, Output
import dash_bootstrap_components as dbc
from time import sleep

app = Dash(__name__)

app.layout = html.Div([
    dbc.Button('Click me', id='button'),
    html.Div(id='output')
])


@callback(
    Output('output', 'children'),
    Input('button', 'n_clicks')
)
def click_and_sleep(n_clicks):
    sleep(1)
    return f'Button clicked {n_clicks} times'


if __name__ == '__main__':
    app.run_server(debug=True, port=8000)
  1. pytest.playwright test:
from playwright.sync_api import Page, expect

def test_app(page: Page):
    page.goto("http://localhost:8000/")
    page.wait_for_selector("#output")
    for n in range(3):
        page.click("#button")
        expect(page.locator("#output")).to_have_text(f"Button clicked {n+1} times")

hi @luggie
That’s a good question. I asked my colleague, who said that the use of a tools like “the JMeter should work; locust is the python one that comes to mind.”

With locust (as with jMeter I guess) it kinda works, but it is so far not supported to use pytest with it. Meaining, the results of pytest do not integrate with locust stats and graphs. The only thing one can do is to make the test users run pytest tests and print the results like so:

from locust import HttpUser, task
import subprocess


class DashUser(HttpUser):
    host = "http://0.0.0.0:8050"

    @task
    def load_dash_app(self):

        result = subprocess.run(
            ["pytest",, "-k", "test_app"],
            capture_output=True,
            text=True,
        )

        if result.returncode != 0:
            print(f"Test failed: {result.stdout, result.stderr}")
        else:
            print("Test passed")

I’d very happy if someone came up with a better idea on how to integrate pytest tests into locust

1 Like