0
0
Testing Fundamentalstesting~10 mins

Performance metrics (response time, throughput) in Testing Fundamentals - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - unittest
Testing Fundamentals
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()
Execution Trace - 7 Steps
StepActionSystem StateAssertionResult
1Test startsTest framework initialized, ready to run test method-PASS
2Sends first GET request to https://api.example.com/dataAPI server receives request and processes itResponse status code is 200PASS
3Checks response time of first request is <= 0.5 secondsResponse received with timing infoResponse elapsed time <= 0.5 secondsPASS
4Repeats sending GET requests until 10 requests are completedAPI server handles multiple requests sequentiallyEach response status code is 200 and response time <= 0.5 secondsPASS
5Calculates total time taken for 10 requestsTest records start and end time-PASS
6Calculates throughput as requests per secondThroughput calculated from total requests and total timeThroughput >= 5 requests per secondPASS
7Test ends with all assertions passedTest framework reports success-PASS
Failure Scenario
Failing Condition: API response time exceeds 0.5 seconds or status code is not 200 or throughput is less than 5 requests per second
Execution Trace Quiz - 3 Questions
Test your understanding
What does the test verify about the API response time?
AResponse time is less than or equal to 0.5 seconds
BResponse time is exactly 1 second
CResponse time is more than 1 second
DResponse time is not measured
Key Result
Measuring both response time and throughput together helps ensure an API is not only fast for single requests but can also handle multiple requests efficiently.