Examples of how to use AI models to create your load tests.
I am using LoadForge and need to create a locustfile that will authenticate against
auth0 to get a bearer token, then use that token to communicate with my API.
Make example requests for me that will help me test my API. Let’s start with:
- GET /api/coins
- POST /api/updateCoinPrice
Also, when you get api/coins save the json list of coins, and then request
- GET /api/coin/{coinId}
from locust import HttpUser, task, between
import json
import random
import requests
AUTH0_DOMAIN = "your-auth0-domain.auth0.com"
AUTH0_CLIENT_ID = "your-client-id"
AUTH0_CLIENT_SECRET = "your-client-secret"
AUTH0_AUDIENCE = "your-api-identifier"
USERNAME = "your-auth0-username"
PASSWORD = "your-auth0-password"
class CoinUser(HttpUser):
wait_time = between(1, 3)
access_token = None
coins = []
def on_start(self):
# Auth0 authentication
payload = {
"grant_type": "password",
"username": USERNAME,
"password": PASSWORD,
"audience": AUTH0_AUDIENCE,
"client_id": AUTH0_CLIENT_ID,
"client_secret": AUTH0_CLIENT_SECRET,
}
res = requests.post(f"https://{AUTH0_DOMAIN}/oauth/token", json=payload)
res.raise_for_status()
self.access_token = res.json()["access_token"]
self.client.headers.update({"Authorization": f"Bearer {self.access_token}"})
# Get coins
with self.client.get("/api/coins", name="GET /api/coins", catch_response=True) as response:
if response.status_code == 200:
self.coins = response.json()
response.success()
else:
response.failure("Failed to load coins")
@task(2)
def get_coin_detail(self):
if not self.coins:
return
coin = random.choice(self.coins)
coin_id = coin.get("id")
if coin_id:
self.client.get(f"/api/coin/{coin_id}", name="GET /api/coin/{coinId}")
@task(1)
def update_coin_price(self):
# Example payload; adjust fields to match your API
payload = {
"coinId": random.randint(1, 10),
"newPrice": round(random.uniform(1, 1000), 2)
}
self.client.post("/api/updateCoinPrice", json=payload, name="POST /api/updateCoinPrice")
Was this page helpful?