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?"
Practice
Solution
Step 1: Understand load testing goal
Load testing is done to see how many users or requests an API can handle simultaneously without failing.Step 2: Compare options with load testing purpose
Only To check how many users the API can handle at once talks about handling many users at once, which matches load testing's main goal.Final Answer:
To check how many users the API can handle at once -> Option AQuick Check:
Load testing = check user load [OK]
- Confusing load testing with debugging
- Thinking load testing improves UI
- Assuming load testing adds features
Solution
Step 1: Identify correct Locust import and class
The Locust library uses HttpUser and @task decorator to define user behavior.Step 2: Check method and client usage
from locust import HttpUser, task class MyUser(HttpUser): @task def load(self): self.client.get('/') correctly uses HttpUser, @task, and self.client.get('/'). Other options miss correct imports or method calls.Final Answer:
from locust import HttpUser, task class MyUser(HttpUser): @task def load(self): self.client.get('/') -> Option CQuick Check:
Use HttpUser and @task decorator [OK]
- Missing @task decorator
- Using wrong class name like User instead of HttpUser
- Calling get() without self.client
from locust import HttpUser, task
class MyUser(HttpUser):
@task
def index(self):
self.client.get("/home")
# Simulate 10 users making requests concurrentlySolution
Step 1: Understand Locust user simulation
Locust simulates multiple users running the tasks concurrently when configured to do so.Step 2: Analyze the code behavior
The class defines a task sending GET requests to /home. If run with 10 users, all will send requests concurrently.Final Answer:
10 users will send GET requests to /home concurrently -> Option BQuick Check:
Locust simulates multiple users running tasks [OK]
- Thinking only one user runs the task
- Assuming missing main causes syntax error
- Believing tasks won't run without explicit start
from locust import HttpUser, task
class MyUser(HttpUser):
def index(self):
self.client.get("/")Solution
Step 1: Check for task decorator usage
Locust requires the @task decorator on methods to mark them as tasks to run.Step 2: Verify other parts of the code
HttpUser is correctly imported, self.client.get is correct, and class names are capitalized by convention.Final Answer:
Missing @task decorator before index method -> Option DQuick Check:
Tasks need @task decorator [OK]
- Forgetting @task decorator
- Importing HttpUser from wrong module
- Using self.get instead of self.client.get
Solution
Step 1: Understand realistic load testing
Realistic load testing simulates many users with varied data and natural pauses between actions.Step 2: Evaluate options for realism
Create a task that sends POST requests with different user credentials and add wait times between requests uses different credentials and wait times, mimicking real user behavior. Others either overload or under-test the API.Final Answer:
Create a task that sends POST requests with different user credentials and add wait times between requests -> Option AQuick Check:
Use varied data and wait times for realism [OK]
- Sending requests nonstop without delay
- Testing with only one user
- Using only GET requests for login
Ready to prove it?
Pass the test and this pattern moves unknown
