askrobots / dbbasic-sessions
Public
Sessions with no server storage: HMAC-signed cookies for Python web apps.
test_performance.py · 5.6 KB · 158 lines
| 1 | """ |
| 2 | Performance tests for dbbasic-sessions |
| 3 | |
| 4 | Tests that the implementation meets performance requirements: |
| 5 | - Create session: < 0.1ms per operation |
| 6 | - Verify session: < 0.1ms per operation |
| 7 | - Handles 10,000+ operations within reasonable time |
| 8 | """ |
| 9 | |
| 10 | import time |
| 11 | import pytest |
| 12 | from dbbasic_sessions import create_session, get_session |
| 13 | |
| 14 | |
| 15 | class TestPerformance: |
| 16 | """Test performance characteristics""" |
| 17 | |
| 18 | def test_create_session_performance(self): |
| 19 | """Should create 10K sessions in < 1 second""" |
| 20 | start = time.time() |
| 21 | tokens = [create_session(str(i)) for i in range(10000)] |
| 22 | elapsed = time.time() - start |
| 23 | |
| 24 | print(f"\nCreated 10,000 sessions in {elapsed:.3f}s") |
| 25 | print(f"Average: {elapsed/10000*1000:.3f}ms per session") |
| 26 | |
| 27 | assert len(tokens) == 10000 |
| 28 | assert elapsed < 1.0, f"Too slow: {elapsed:.3f}s for 10K sessions" |
| 29 | |
| 30 | def test_get_session_performance(self): |
| 31 | """Should verify 10K sessions in < 1 second""" |
| 32 | # Create tokens first |
| 33 | tokens = [create_session(str(i)) for i in range(10000)] |
| 34 | |
| 35 | # Measure verification |
| 36 | start = time.time() |
| 37 | for token in tokens: |
| 38 | get_session(token) |
| 39 | elapsed = time.time() - start |
| 40 | |
| 41 | print(f"\nVerified 10,000 sessions in {elapsed:.3f}s") |
| 42 | print(f"Average: {elapsed/10000*1000:.3f}ms per verification") |
| 43 | |
| 44 | assert elapsed < 1.0, f"Too slow: {elapsed:.3f}s for 10K verifications" |
| 45 | |
| 46 | def test_single_operation_performance(self): |
| 47 | """Should perform single operations very quickly""" |
| 48 | # Warm up |
| 49 | for _ in range(100): |
| 50 | token = create_session('42') |
| 51 | get_session(token) |
| 52 | |
| 53 | # Measure create |
| 54 | start = time.perf_counter() |
| 55 | token = create_session('42') |
| 56 | create_time = time.perf_counter() - start |
| 57 | |
| 58 | # Measure verify |
| 59 | start = time.perf_counter() |
| 60 | get_session(token) |
| 61 | verify_time = time.perf_counter() - start |
| 62 | |
| 63 | print(f"\nSingle operation times:") |
| 64 | print(f"Create: {create_time*1000:.3f}ms") |
| 65 | print(f"Verify: {verify_time*1000:.3f}ms") |
| 66 | |
| 67 | # Should be very fast (< 1ms each) |
| 68 | assert create_time < 0.001, f"Create too slow: {create_time*1000:.3f}ms" |
| 69 | assert verify_time < 0.001, f"Verify too slow: {verify_time*1000:.3f}ms" |
| 70 | |
| 71 | def test_concurrent_session_performance(self): |
| 72 | """Should handle many concurrent sessions efficiently""" |
| 73 | num_sessions = 1000 |
| 74 | |
| 75 | start = time.time() |
| 76 | tokens = {str(i): create_session(str(i)) for i in range(num_sessions)} |
| 77 | create_time = time.time() - start |
| 78 | |
| 79 | start = time.time() |
| 80 | for user_id, token in tokens.items(): |
| 81 | verified_id = get_session(token) |
| 82 | assert verified_id == user_id |
| 83 | verify_time = time.time() - start |
| 84 | |
| 85 | print(f"\nConcurrent {num_sessions} sessions:") |
| 86 | print(f"Create all: {create_time:.3f}s") |
| 87 | print(f"Verify all: {verify_time:.3f}s") |
| 88 | print(f"Total: {create_time + verify_time:.3f}s") |
| 89 | |
| 90 | assert create_time < 0.2, f"Create too slow: {create_time:.3f}s" |
| 91 | assert verify_time < 0.2, f"Verify too slow: {verify_time:.3f}s" |
| 92 | |
| 93 | def test_memory_efficiency(self): |
| 94 | """Should use minimal memory (no server storage)""" |
| 95 | import sys |
| 96 | |
| 97 | # Create many sessions |
| 98 | tokens = [create_session(str(i)) for i in range(10000)] |
| 99 | |
| 100 | # Tokens are just strings, no server-side storage |
| 101 | # Memory usage should be minimal (just the token strings in the list) |
| 102 | token_memory = sum(sys.getsizeof(token) for token in tokens) |
| 103 | |
| 104 | print(f"\nMemory for 10K tokens: {token_memory / 1024:.2f} KB") |
| 105 | print(f"Average per token: {token_memory / 10000:.0f} bytes") |
| 106 | |
| 107 | # Should be reasonable (< 2MB for 10K tokens) |
| 108 | # Note: sys.getsizeof includes Python object overhead |
| 109 | assert token_memory < 2 * 1024 * 1024, f"Memory usage too high: {token_memory / 1024:.2f} KB" |
| 110 | |
| 111 | def test_scaling_characteristics(self): |
| 112 | """Should scale linearly (O(n))""" |
| 113 | sizes = [100, 500, 1000, 5000, 10000] |
| 114 | times = [] |
| 115 | |
| 116 | for size in sizes: |
| 117 | start = time.time() |
| 118 | tokens = [create_session(str(i)) for i in range(size)] |
| 119 | for token in tokens: |
| 120 | get_session(token) |
| 121 | elapsed = time.time() - start |
| 122 | times.append(elapsed) |
| 123 | |
| 124 | print(f"\n{size:5d} sessions: {elapsed:.3f}s ({elapsed/size*1000:.3f}ms avg)") |
| 125 | |
| 126 | # Time should scale roughly linearly |
| 127 | # Check that 10x size doesn't take more than 15x time |
| 128 | # (allowing some overhead) |
| 129 | ratio_100_to_1000 = times[2] / times[0] |
| 130 | ratio_1000_to_10000 = times[4] / times[2] |
| 131 | |
| 132 | print(f"\nScaling ratios:") |
| 133 | print(f"100 -> 1000 (10x): {ratio_100_to_1000:.2f}x time") |
| 134 | print(f"1000 -> 10000 (10x): {ratio_1000_to_10000:.2f}x time") |
| 135 | |
| 136 | assert ratio_100_to_1000 < 15, "Scaling worse than linear (100->1000)" |
| 137 | assert ratio_1000_to_10000 < 15, "Scaling worse than linear (1000->10000)" |
| 138 | |
| 139 | def test_invalid_token_performance(self): |
| 140 | """Should handle invalid tokens quickly (fail fast)""" |
| 141 | invalid_tokens = [ |
| 142 | 'invalid-token', |
| 143 | 'not.a.real.token', |
| 144 | '', |
| 145 | 'a' * 1000, # Very long invalid token |
| 146 | ] |
| 147 | |
| 148 | start = time.time() |
| 149 | for _ in range(1000): |
| 150 | for token in invalid_tokens: |
| 151 | get_session(token) |
| 152 | elapsed = time.time() - start |
| 153 | |
| 154 | print(f"\nVerified 4000 invalid tokens in {elapsed:.3f}s") |
| 155 | print(f"Average: {elapsed/4000*1000:.3f}ms per check") |
| 156 | |
| 157 | # Should fail fast |
| 158 | assert elapsed < 0.5, f"Invalid token checks too slow: {elapsed:.3f}s" |