Example tests and snippets
API Examples (REST)
Load testing a REST API is extremely easy with LoadForge. We have used our REST API as an example in the tests below. You can set any custom headers you require, and can send requests based on data you receive back from your API.
Rate limit warning
Remember to disable API rate limits on your token, or you will get loads of failures :)
We have two examples below. The first is a simple set of GET requests to your API, with bearer authentication (by setting a custom header).
The second is a more advanced example, with POST and GET requests as well as reacting to data from the API.
Basic API
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
# each fake user waits between 5 and 9 seconds before executing the task below
wait_time = between(5, 9)
def on_start(self):
# Set our Authorization header with our bearer token
self.client.headers.update({'Authorization': 'Bearer TOKEN_HERE'})
@task(1)
def index_page(self):
# each wait_time we will ask for 4 responses
self.client.get("/api/hosts")
self.client.get("/api/tests")
self.client.get("/api/result/1488")
self.client.get("/api/result/1463")
Advanced API
This test is an actual example from us load testing the LoadForge REST API. We have added detailed comments in the test file to help you understand what we are doing and why.
There are four main parts to it:
- Get hosts and tests at random
- When starting, get all the results and save the result ids for later as well as set a bearer token
- Get a random result from the list of results we received earlier
- Create and then delete (clean up after yourself) a host via the API.
You can see how easily we are able to load test our Laravel API, or any REST-based API really. Nice features are being able to fetch all the results, and then get random result ids as we can parse the responses in your test.
import random
from locust import HttpUser, task, between
class QuickstartUser(HttpUser):
wait_time = between(5, 9)
def get_results(self):
self.results = []
# Fetch all the results and store their IDs for later
response = self.client.get("/api/results")
for result in response.json():
self.results.append(result['id'])
def on_start(self):
# Spread out starts
self.wait()
# Set out Bearer auth token
self.client.headers.update({'Authorization': 'Bearer TOKEN_HERE'})
# Load up all the possible 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):
# Select a random result from the get_results query to fetch
self.client.get("/api/result/" + str(random.choice(self.results)))
@task(1)
def crud_host(self):
# Make a new Host and get the json reply
response = self.client.post("/api/hosts/create", {'url': 'www.test.com', 'port': '443', 'protocol': 'https'})
host = response.json()
# Delete what we just made
self.client.post("/api/hosts/delete", {"host_id": host['host_id']})
Locust Test Example
LoadForge is powered by locust, meaning open source locust users can copy this script as well. If you are a locust user, consider importing your script to LoadForge to supercharge your testing!