|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Benchmark cold start vs warm performance for both libraries.""" |
| 3 | + |
| 4 | +import os |
| 5 | +import statistics |
| 6 | +import subprocess |
| 7 | +import sys |
| 8 | +import time |
| 9 | + |
| 10 | +ENV_CONTENT = """APP_NAME=test |
| 11 | +DEBUG=true |
| 12 | +API_KEY=key123 |
| 13 | +MAX_CONNECTIONS=100 |
| 14 | +TIMEOUT=30.0 |
| 15 | +DATABASE__HOST=localhost |
| 16 | +DATABASE__PORT=5432 |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +def benchmark_msgspec_cold(): |
| 21 | + """Measure msgspec cold start.""" |
| 22 | + code = """ |
| 23 | +import time |
| 24 | +from msgspec_ext import BaseSettings, SettingsConfigDict |
| 25 | +
|
| 26 | +class TestSettings(BaseSettings): |
| 27 | + model_config = SettingsConfigDict(env_file=".env.test") |
| 28 | + app_name: str |
| 29 | + debug: bool = False |
| 30 | + api_key: str = "default" |
| 31 | + max_connections: int = 100 |
| 32 | + timeout: float = 30.0 |
| 33 | + database__host: str = "localhost" |
| 34 | + database__port: int = 5432 |
| 35 | +
|
| 36 | +start = time.perf_counter() |
| 37 | +TestSettings() |
| 38 | +end = time.perf_counter() |
| 39 | +print((end - start) * 1000) |
| 40 | +""" |
| 41 | + with open(".env.test", "w") as f: |
| 42 | + f.write(ENV_CONTENT) |
| 43 | + try: |
| 44 | + result = subprocess.run( |
| 45 | + ["uv", "run", "python", "-c", code], |
| 46 | + capture_output=True, |
| 47 | + text=True, |
| 48 | + check=True, |
| 49 | + ) |
| 50 | + return float(result.stdout.strip()) |
| 51 | + finally: |
| 52 | + if os.path.exists(".env.test"): |
| 53 | + os.unlink(".env.test") |
| 54 | + |
| 55 | + |
| 56 | +def benchmark_pydantic_cold(): |
| 57 | + """Measure pydantic cold start.""" |
| 58 | + code = """ |
| 59 | +import time |
| 60 | +from pydantic_settings import BaseSettings |
| 61 | +
|
| 62 | +class TestSettings(BaseSettings): |
| 63 | + app_name: str |
| 64 | + debug: bool = False |
| 65 | + api_key: str = "default" |
| 66 | + max_connections: int = 100 |
| 67 | + timeout: float = 30.0 |
| 68 | + database__host: str = "localhost" |
| 69 | + database__port: int = 5432 |
| 70 | +
|
| 71 | + class Config: |
| 72 | + env_file = ".env.test" |
| 73 | +
|
| 74 | +start = time.perf_counter() |
| 75 | +TestSettings() |
| 76 | +end = time.perf_counter() |
| 77 | +print((end - start) * 1000) |
| 78 | +""" |
| 79 | + with open(".env.test", "w") as f: |
| 80 | + f.write(ENV_CONTENT) |
| 81 | + try: |
| 82 | + result = subprocess.run( |
| 83 | + ["uv", "run", "--with", "pydantic-settings", "python", "-c", code], |
| 84 | + capture_output=True, |
| 85 | + text=True, |
| 86 | + check=True, |
| 87 | + ) |
| 88 | + return float(result.stdout.strip()) |
| 89 | + finally: |
| 90 | + if os.path.exists(".env.test"): |
| 91 | + os.unlink(".env.test") |
| 92 | + |
| 93 | + |
| 94 | +def benchmark_msgspec_warm(iterations=100): |
| 95 | + """Measure msgspec warm (cached).""" |
| 96 | + from msgspec_ext import BaseSettings, SettingsConfigDict |
| 97 | + |
| 98 | + class TestSettings(BaseSettings): |
| 99 | + model_config = SettingsConfigDict(env_file=".env.warm") |
| 100 | + app_name: str |
| 101 | + debug: bool = False |
| 102 | + api_key: str = "default" |
| 103 | + max_connections: int = 100 |
| 104 | + timeout: float = 30.0 |
| 105 | + database__host: str = "localhost" |
| 106 | + database__port: int = 5432 |
| 107 | + |
| 108 | + with open(".env.warm", "w") as f: |
| 109 | + f.write(ENV_CONTENT) |
| 110 | + |
| 111 | + try: |
| 112 | + TestSettings() # Warmup |
| 113 | + times = [] |
| 114 | + for _ in range(iterations): |
| 115 | + start = time.perf_counter() |
| 116 | + TestSettings() |
| 117 | + end = time.perf_counter() |
| 118 | + times.append((end - start) * 1000) |
| 119 | + return statistics.mean(times) |
| 120 | + finally: |
| 121 | + os.unlink(".env.warm") |
| 122 | + |
| 123 | + |
| 124 | +def benchmark_pydantic_warm(iterations=100): |
| 125 | + """Measure pydantic warm.""" |
| 126 | + code = f""" |
| 127 | +import time |
| 128 | +import statistics |
| 129 | +from pydantic_settings import BaseSettings |
| 130 | +
|
| 131 | +ENV = '''{ENV_CONTENT}''' |
| 132 | +
|
| 133 | +with open('.env.pwarm', 'w') as f: |
| 134 | + f.write(ENV) |
| 135 | +
|
| 136 | +class TestSettings(BaseSettings): |
| 137 | + app_name: str |
| 138 | + debug: bool = False |
| 139 | + api_key: str = "default" |
| 140 | + max_connections: int = 100 |
| 141 | + timeout: float = 30.0 |
| 142 | + database__host: str = "localhost" |
| 143 | + database__port: int = 5432 |
| 144 | +
|
| 145 | + class Config: |
| 146 | + env_file = ".env.pwarm" |
| 147 | +
|
| 148 | +TestSettings() # Warmup |
| 149 | +times = [] |
| 150 | +for _ in range({iterations}): |
| 151 | + start = time.perf_counter() |
| 152 | + TestSettings() |
| 153 | + end = time.perf_counter() |
| 154 | + times.append((end - start) * 1000) |
| 155 | +
|
| 156 | +print(statistics.mean(times)) |
| 157 | +""" |
| 158 | + try: |
| 159 | + result = subprocess.run( |
| 160 | + ["uv", "run", "--with", "pydantic-settings", "python", "-c", code], |
| 161 | + capture_output=True, |
| 162 | + text=True, |
| 163 | + check=True, |
| 164 | + ) |
| 165 | + return float(result.stdout.strip()) |
| 166 | + finally: |
| 167 | + if os.path.exists(".env.pwarm"): |
| 168 | + os.unlink(".env.pwarm") |
| 169 | + |
| 170 | + |
| 171 | +if __name__ == "__main__": |
| 172 | + print("=" * 70) |
| 173 | + print("Cold Start vs Warm Performance Comparison") |
| 174 | + print("=" * 70) |
| 175 | + print() |
| 176 | + |
| 177 | + print("Benchmarking msgspec-ext...") |
| 178 | + msgspec_cold_times = [benchmark_msgspec_cold() for _ in range(3)] |
| 179 | + msgspec_cold = statistics.mean(msgspec_cold_times) |
| 180 | + msgspec_warm = benchmark_msgspec_warm(100) |
| 181 | + |
| 182 | + print("Benchmarking pydantic-settings...") |
| 183 | + pydantic_cold_times = [benchmark_pydantic_cold() for _ in range(3)] |
| 184 | + pydantic_cold = statistics.mean(pydantic_cold_times) |
| 185 | + pydantic_warm = benchmark_pydantic_warm(100) |
| 186 | + |
| 187 | + print() |
| 188 | + print("=" * 70) |
| 189 | + print("RESULTS") |
| 190 | + print("=" * 70) |
| 191 | + print() |
| 192 | + print(f"{'Library':<20} {'Cold Start':<15} {'Warm (Cached)':<15} {'Speedup':<10}") |
| 193 | + print("-" * 70) |
| 194 | + print( |
| 195 | + f"{'msgspec-ext':<20} {msgspec_cold:>8.3f}ms {msgspec_warm:>8.3f}ms {msgspec_cold / msgspec_warm:>6.1f}x" |
| 196 | + ) |
| 197 | + print( |
| 198 | + f"{'pydantic-settings':<20} {pydantic_cold:>8.3f}ms {pydantic_warm:>8.3f}ms {pydantic_cold / pydantic_warm:>6.1f}x" |
| 199 | + ) |
| 200 | + print() |
| 201 | + print("-" * 70) |
| 202 | + print("msgspec vs pydantic:") |
| 203 | + print(f" Cold: {pydantic_cold / msgspec_cold:.1f}x faster") |
| 204 | + print(f" Warm: {pydantic_warm / msgspec_warm:.1f}x faster") |
| 205 | + print() |
0 commit comments