Load Testing Streamlit Apps with Locust Python

Streamlit has reached 1.0, this awesome framework has changed how we build data apps and ML apps with ease. In this article we will be exploring how to perform load testing on our Streamlit apps.

So what is Load Testing? why do we even need it and how do we perform load testing for our apps?

Supposing you build a web application on your system and you have deployed it to production, now you app is online, but then you get ten users using your app and the number of users continue to grow tremendously. The traffic to your deployed app is growing up and up and you app begins to lag and crash. At the dev stage your app was working well but now it is not as performant as it was and you are having challenges.

How do you avoid this issue? One of the solutions to help fix this challenge is what we term as Load Testing.

Load testing is a kind of performance testing which determines a system’s or an application’s performance under real-life load conditions.It is the process of simulating multiple users using your app at the same time or concurrently.

It determines how the software application behaves while being accessed by multiple users simultaneously.
Load testing simply means you are testing how your app or system will behave or perform when multiple users are using your app at the same time.

It is important because it enables you identify where you need to redesign and restructure in order to improve the performance and smooth functioning of your application or system before you release it to the general public.

How to Perform Load Testing

So how do you perform load testing?
There are several libraries and tools that we can use to perform load testing. Some of them include

  • Locust
  • JMeter
  • k6
  • etc

In this tutorial we will be exploring how to use Locust a python library to perform load testing of our app. One thing about locust is that you can use it to test several web applications such as Flask, Django,FastAPI,Express.js App,etc
Let us see how to use locust to perform load testing on our Streamlit App.

For now Streamlit offers only one main endpoint that we can load testing unlike other popular web micro-frameworks like Flask,Django or FastAPI. Howbeit we will still be able to simulate multiple concurrent users using our streamlit app.

Let us start
First you need to install locust via pip as below

pip install locust

Next you will need two main things.

  • Your app
  • A locust_file.py

Locust comes as a package and a CLI.
After designing your app, you will need to create another file – the locust_file.py, it can be any name, where you will place the instructions on the various endpoint you want to test.
A simple example is as below

from locust import HttpUser,task,between 

class AppUser(HttpUser):
	wait_time = between(2,5)

	# Endpoint
	@task
	def home_page(self):
		self.client.get("/")

The @task decorator is essential in informing locust on which route/endpoint to perform load testing on. In our case we will use the main route ‘/’.

We will then start our app separately and start our locust_file.py alongside in a separate terminal.
To start our streamlit app

streamlit run app.py

To start our load testing with locust

locust -f locust_file.py

This will automatically open the Locust UI where you can specify the number of users you want to simulate and the host and port you want to test.[If it doesn’t open you can copy and paste the http://0.0.0.0:8089 into your browser to see the UI]
In our case we will be simulating just 500 users and will be testing the default streamlit port localhost:8501

Locustを触ってみた - Qiita

Alternatively you can specify these parameters in the command line when running the locust_file.py lie this

locust -f locust_file.py --host http://localhost:8501 --users 200

The Locust UI offers information about the number of requests made, the fails, average response time, total response time,etc.

You can also see visualization of the app performance over time in the Charts section.

You can also see the section for increase in users in the chart below



One nice thing about locust is that you can generate a report of the results in multiple format in the Download Data section.

We have seen how to perform load testing for our Streamlit App. For now as I said earlier we only have one endpoint to test but hopefully we will check other methods.

You can check out the video tutorial below for more

Also check out some of our materials on Streamlit such as the courses and the Book – Getting Started with Streamlit for Data Science by Tyler Richards.

Thanks for your time
Jesus Saves
By Jesse E.Agbe(JCharis)

2 thoughts on “Load Testing Streamlit Apps with Locust Python”

Leave a Comment

Your email address will not be published. Required fields are marked *