Overview
Load testing a REST API is streamlined and intuitive with LoadForge. To demonstrate the capability, we've used our own REST API in the examples below. LoadForge provides the flexibility to define custom headers as required, and the ability to send requests based on the data returned from your API.
Rate Limit Warning
Ensure you disable API rate limits associated with your token to prevent an influx of failure responses during testing.
Below are two illustrative examples:
- Basic API Example: This showcases a straightforward set of GET requests to an API, accompanied by bearer authentication using a custom header.
- Advanced API Example: A more intricate example involving both POST and GET requests and dynamic responses based on returned data.
Basic API Example
The following Python script demonstrates how to conduct simple GET requests to your API with bearer token authentication:
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
# The simulated user will pause for a duration between 5 and 9 seconds before executing the tasks.
wait_time = between(5, 9)
def on_start(self):
# Initializing the Authorization header with the bearer token.
self.client.headers.update({'Authorization': 'Bearer TOKEN_HERE'})
@task(1)
def index_page(self):
# Making four GET requests every wait_time duration.
self.client.get("/api/hosts")
self.client.get("/api/tests")
self.client.get("/api/result/1488")
self.client.get("/api/result/1463")
Advanced API Example
This test provides insights into load testing the LoadForge REST API. The script below contains comprehensive comments to elucidate each step:
The test script comprises four main segments:
- Fetch random hosts and tests.
- During initialization, retrieve all results, save the result IDs, and establish the bearer token.
- Acquire a random result from the previously fetched list.
- Create a new host through the API and subsequently delete it to maintain a clean slate.
This exemplifies the convenience of load testing, be it for a Laravel API or any RESTful API. The capacity to retrieve all results and then fetch random result IDs is particularly beneficial, given the inherent ability to parse responses within the test.
import random
from locust import HttpUser, task, between
class AdvancedUser(HttpUser):
wait_time = between(5, 9)
def get_results(self):
self.results = []
# Retrieve all results and store their unique IDs for subsequent tasks.
response = self.client.get("/api/results")
for result in response.json():
self.results.append(result['id'])
def on_start(self):
# Distribute start times among users.
self.wait()
# Initialize the Bearer authentication token.
self.client.headers.update({'Authorization': 'Bearer TOKEN_HERE'})
# Fetch and store all potential results.
self.get_results()
@task(1)
def read_hosts(self):
self.client.get("/api/hosts")
@task(2)
def read_tests(self):
self.client.get("/api/tests")
@task(5)
def read_results(self):
# Fetch a random result based on the previously retrieved list.
self.client.get("/api/result/" + str(random.choice(self.results)))
@task(1)
def crud_host(self):
# Create a new Host and extract its details from the response.
response = self.client.post("/api/hosts/create", {'url': 'www.test.com', 'port': '443', 'protocol': 'https'})
host = response.json()
# Clean up by deleting the recently created host.
self.client.post("/api/hosts/delete", {"host_id": host['host_id']})
Powered by Locust
LoadForge is built upon the robust foundation of locust, allowing open-source locust users to readily utilize this script. If you're already a locust aficionado, think about importing your script to LoadForge and elevate your testing endeavors!