Load testing in Rest API - Time & Space Complexity
Load testing checks how a system handles many requests at once.
We want to know how the system's work grows as more users send requests.
Analyze the time complexity of the following code snippet.
// Simulate handling multiple API requests
function handleRequests(requests) {
requests.forEach(request => {
processRequest(request);
});
}
function processRequest(request) {
// Process each request (fixed time)
}
This code processes each incoming request one by one.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Loop over all requests.
- How many times: Once for each request received.
As the number of requests grows, the work grows in the same way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 processRequest calls |
| 100 | 100 processRequest calls |
| 1000 | 1000 processRequest calls |
Pattern observation: Doubling requests doubles the work.
Time Complexity: O(n)
This means the time to handle requests grows directly with the number of requests.
[X] Wrong: "Handling more requests takes the same time as handling one."
[OK] Correct: Each request needs its own processing, so more requests mean more total work.
Understanding how work grows with users helps you design systems that stay fast under pressure.
"What if processRequest took longer for some requests? How would that affect the time complexity?"