Test Overview
This test measures the performance of a web API by checking its response time and throughput. It verifies that the API responds within an acceptable time and can handle a set number of requests per second.
This test measures the performance of a web API by checking its response time and throughput. It verifies that the API responds within an acceptable time and can handle a set number of requests per second.
import time import requests import unittest class TestAPIPerformance(unittest.TestCase): def test_response_time_and_throughput(self): url = "https://api.example.com/data" request_count = 10 max_response_time = 0.5 # seconds start_time = time.time() for _ in range(request_count): response = requests.get(url) self.assertEqual(response.status_code, 200) self.assertLessEqual(response.elapsed.total_seconds(), max_response_time) total_time = time.time() - start_time throughput = request_count / total_time self.assertGreaterEqual(throughput, 5) # at least 5 requests per second if __name__ == '__main__': unittest.main()
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test framework initialized, ready to run test method | - | PASS |
| 2 | Sends first GET request to https://api.example.com/data | API server receives request and processes it | Response status code is 200 | PASS |
| 3 | Checks response time of first request is <= 0.5 seconds | Response received with timing info | Response elapsed time <= 0.5 seconds | PASS |
| 4 | Repeats sending GET requests until 10 requests are completed | API server handles multiple requests sequentially | Each response status code is 200 and response time <= 0.5 seconds | PASS |
| 5 | Calculates total time taken for 10 requests | Test records start and end time | - | PASS |
| 6 | Calculates throughput as requests per second | Throughput calculated from total requests and total time | Throughput >= 5 requests per second | PASS |
| 7 | Test ends with all assertions passed | Test framework reports success | - | PASS |