Prompt
how to simulate concurrent users for an API
Latest observation
Oct 20, 2026
OpenAI APIWeb search: off
To simulate concurrent users for an API, you typically use a load testing tool that sends many requests in parallel and ramps up traffic over time.
Common approaches
1. Use a load testing tool
Popular options:
- k6 — modern, scriptable, great for CI/CD
- Locust — Python-based, good for custom user behavior
- JMeter — classic, GUI-driven, widely used
- Gatling — high-performance, Scala-based
- Artillery — simple and lightweight
2. Define user behavior
Instead of just firing random requests, model realistic actions:
- login
- fetch data
- create/update records
- logout
This helps test:
- concurrency
- throughput
- latency
- error rates
- rate limiting
3. Set up concurrency levels
Examples:
- 10 users for smoke test
- 100 users for moderate load
- 1,000+ users for stress test
Use:
- constant number of virtual users
- ramp-up to gradually increase load
- spike tests to see how the API handles sudden bursts
Example with k6
Install
brew install k6
# or on Linux/Windows via package manager
Basic script
import http from 'k6/http';
import { sleep } from 'k6';
export let options = {
vus: 50, // concurrent virtual users
duration: '30s' // test duration
};
export default function () {
http.get('https://api.example.com/v1/items');
sleep(1);
}
Run
k6 run script.js
Example with Locust
Install
pip install locust
locustfile.py
from locust import HttpUser, task, between
class APIUser(HttpUser):
wait_time = between(1, 3)
@task
def get_items(self):
self.client.get("/v1/items")
Run
locust -f locustfile.py --host=https://api.example.com
Then open the web UI and configure users/spawn rate.
Best practices
- Test in a staging environment, not production
- Use test data or seeded accounts
- Monitor:
- CPU/memory
- DB performance
- response times
- 4xx/5xx errors
- Start small, then scale up
- Add think time between requests to mimic real users
- Handle auth tokens if your API requires login
If you want to simulate from code
You can also write a script with threads/async tasks, but dedicated tools are better for accurate concurrency and reporting.
Example in Python using asyncio + httpx:
import asyncio
import httpx
async def fetch(client):
await client.get("https://api.example.com/v1/items")
async def main():
async with httpx.AsyncClient() as client:
tasks = [fetch(client) for _ in range(100)]
await asyncio.gather(*tasks)
asyncio.run(main())
If you want, I can give you:
- a k6 script for your API,
- a Locust setup,
- or a Python async script tailored to your endpoint.