0
0
Testing Fundamentalstesting~15 mins

Performance metrics (response time, throughput) in Testing Fundamentals - Build an Automation Script

Choose your learning style9 modes available
Measure API response time and throughput
Preconditions (2)
Step 1: Send 50 GET requests to https://api.example.com/data as quickly as possible
Step 2: Record the response time for each request
Step 3: Calculate the average response time
Step 4: Calculate throughput as number of requests completed per second
✅ Expected Result: Average response time is less than 500 milliseconds and throughput is at least 10 requests per second
Automation Requirements - Python with requests and time modules
Assertions Needed:
Average response time < 500 ms
Throughput >= 10 requests per second
Best Practices:
Use precise time measurement with time.perf_counter()
Send requests sequentially to measure response time accurately
Calculate throughput based on total time taken for all requests
Handle exceptions for failed requests
Automated Solution
Testing Fundamentals
import requests
import time

url = "https://api.example.com/data"
num_requests = 50
response_times = []

start_time = time.perf_counter()
for _ in range(num_requests):
    try:
        req_start = time.perf_counter()
        response = requests.get(url)
        req_end = time.perf_counter()
        response.raise_for_status()
        response_times.append((req_end - req_start) * 1000)  # milliseconds
    except requests.RequestException:
        response_times.append(float('inf'))  # mark failed request
end_time = time.perf_counter()

total_time = end_time - start_time  # seconds
successful_responses = [t for t in response_times if t != float('inf')]
avg_response_time = sum(successful_responses) / len(successful_responses) if successful_responses else float('inf')
throughput = len(successful_responses) / total_time if total_time > 0 else 0

assert avg_response_time < 500, f"Average response time too high: {avg_response_time} ms"
assert throughput >= 10, f"Throughput too low: {throughput} requests/sec"

print(f"Average response time: {avg_response_time:.2f} ms")
print(f"Throughput: {throughput:.2f} requests/sec")

This script sends 50 GET requests to the API endpoint one after another.

It measures the time taken for each request using time.perf_counter() for accuracy.

Failed requests are marked with infinite time to exclude them from average calculation.

After all requests, it calculates the average response time in milliseconds and throughput as requests per second.

Assertions check if the average response time is below 500 ms and throughput is at least 10 requests per second.

This approach ensures precise timing and handles failures gracefully.

Common Mistakes - 3 Pitfalls
Using time.time() instead of time.perf_counter() for timing
Sending requests in parallel without measuring individual response times
Ignoring failed requests in calculations
Bonus Challenge

Now add data-driven testing with 3 different API endpoints to measure their performance

Show Hint