dev-resources.site
for different kinds of informations.
Getting Started with Load Testing using Locust
Published at
3/29/2024
Categories
loadtesting
python
api
qa
Author
burhanahmeed
Main Article
Author
12 person written this
burhanahmeed
open
What is Locust
Locust is a load testing tool written in Python. The test script is also written in Python. To use this tools, you need at least very basic knowledge of Python.
Across many testing framework, Locust is quite simple and having pretty good and simple UI. You only need to install Python in your machine and just install the tools.
How to install it
Installing Locust is quite simple, just need pip and you can run it.
pip install locust
I usually using virtual env before installing the tools.
Create a virtual env
python3 -m venv venv
Activate the venv
source venv/bin/activate
Then you can run Locust here
Create the testing script
This one simple example to load test on login API.
# index.py
from locust import HttpUser, task, between
class LoginUser(HttpUser):
wait_time = between(2, 5) # Wait time between simulated users
@task
def login(self):
# Define the payload for the login request
login_payload = {
'username': 'your_username',
'password': 'your_password'
}
# Send a POST request to the login endpoint
response = self.client.post('/login', json=login_payload)
# Check if the login was successful
if response.status_code == 200:
print("Login successful")
else:
print("Login failed")
Now let say, your API needs a token before you hit it, this how we will do it.
from locust import HttpUser, task, between
class UserApiTest(HttpUser):
wait_time = between(2, 5) # Wait time between simulated users
def on_start(self):
# Log in and get the authentication token
login_payload = {
'username': 'your_username',
'password': 'your_password'
}
response = self.client.post('/login', json=login_payload)
if response.status_code == 200:
self.auth_token = response.json().get('token')
else:
print("Login failed")
@task
def get_users(self):
# Send a GET request to the /users endpoint with the authentication token
headers = {'Authorization': f'Bearer {self.auth_token}'}
response = self.client.get('/users', headers=headers)
# Check if the request was successful
if response.status_code == 200:
print("Get users successful")
else:
print("Get users failed")
Run it!
To run it, it's very simple.
locust -f index.py
loadtesting Article's
30 articles in total
Everything you need to know about load testing concurrent users
read article
Scaling Node.js: Handling 1 Million Requests Like a Pro
read article
A Critical Performance Issue Led Me to Discover EchoAPI's Load Testing
read article
Comparing the K6 Operator vs Testkube for Load Testing
read article
Performance Testing with NeoLoad
read article
How I created a unique PPT for "Load Testing" KT!
read article
Capacity Planning as a Way to Minimize Unforeseen Business Expenses
read article
Load testing 3scale
read article
Getting Started with Load Testing using Locust
currently reading
JMeter-Dynamic Load Testing of Restful APIs
read article
K6 Development: Beyond The Basic Setup
read article
Load Testing Serverless Application using k6
read article
How to use CSV file for parameterization in JMeter
read article
Installing Apache JMeter using Homebrew
read article
Thread Group in Jmeter: Understanding its Components and Use Cases
read article
Running Load-Test-as-Code Simulations with Gatling
read article
Load-Test-as-Code for Non-Coders!
read article
How Software Performance Testing Can Help Your Business Succeed this Holiday Season
read article
Library for easy implementation of instant load testing
read article
My journey with AWS
read article
Loadtest Websocket Server
read article
How to get started with Load Testing?
read article
AWS Lambda vs. Cloudflare Workers vs. AWS Cloudfront Function cold start comparison with Ddosify Cloud
read article
Performance Testing via Artillery.io - user guide
read article
Load testing with Playwright
read article
Performance Testing Report. The importance of Load and Stress Testing your Systems.
read article
Load Test your NodeJS app using K6
read article
Load Testing with Locust
read article
Vegeta load testing a quick tutorial with GET examples
read article
Load Testing Your API with Postman
read article
Featured ones: