-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperformance_comparison.py
More file actions
427 lines (345 loc) · 13.1 KB
/
Copy pathperformance_comparison.py
File metadata and controls
427 lines (345 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
#!/usr/bin/env python3
"""
Performance comparison between psqlpy-sqlalchemy and asyncpg dialect.
This script benchmarks various database operations to measure performance improvements.
"""
import asyncio
import time
from statistics import mean, median, stdev
from sqlalchemy import (
Integer,
String,
Text,
text,
)
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
# Database connection strings
PSQLPY_URL = "postgresql+psqlpy://postgres:password@localhost:5432/test_db"
ASYNCPG_URL = "postgresql+asyncpg://postgres:password@localhost:5432/test_db"
class Base(DeclarativeBase):
"""Declarative base class for ORM models."""
class TestModel(Base):
"""Test model for benchmarking."""
__tablename__ = "benchmark_test"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
name: Mapped[str] = mapped_column(String(100), nullable=False)
description: Mapped[str | None] = mapped_column(Text)
value: Mapped[int] = mapped_column(Integer, default=0)
class BenchmarkResult:
"""Store and format benchmark results."""
def __init__(self, name: str):
self.name = name
self.times: list[float] = []
def add_time(self, duration: float) -> None:
"""Add a timing measurement."""
self.times.append(duration)
def get_stats(self) -> dict[str, float]:
"""Calculate statistics for the benchmark."""
if not self.times:
return {"mean": 0, "median": 0, "stdev": 0, "min": 0, "max": 0}
return {
"mean": mean(self.times),
"median": median(self.times),
"stdev": stdev(self.times) if len(self.times) > 1 else 0,
"min": min(self.times),
"max": max(self.times),
}
def __str__(self) -> str:
"""Format results as string."""
stats = self.get_stats()
return (
f"{self.name}:\n"
f" Mean: {stats['mean'] * 1000:.2f}ms\n"
f" Median: {stats['median'] * 1000:.2f}ms\n"
f" StdDev: {stats['stdev'] * 1000:.2f}ms\n"
f" Min: {stats['min'] * 1000:.2f}ms\n"
f" Max: {stats['max'] * 1000:.2f}ms"
)
async def setup_database(url: str) -> None:
"""Set up the test database."""
engine = create_async_engine(url, echo=False)
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.drop_all)
await conn.run_sync(Base.metadata.create_all)
await engine.dispose()
async def benchmark_simple_select(
url: str, iterations: int = 100
) -> BenchmarkResult:
"""Benchmark simple SELECT queries."""
result = BenchmarkResult("Simple SELECT")
engine = create_async_engine(
url, echo=False, pool_size=10, max_overflow=20
)
try:
async with engine.begin() as conn:
# Insert test data
for i in range(100):
await conn.execute(
text(
"INSERT INTO benchmark_test (name, description, value) VALUES (:name, :desc, :val)"
),
{
"name": f"test_{i}",
"desc": f"Description {i}",
"val": i,
},
)
for _ in range(iterations):
start = time.perf_counter()
async with engine.connect() as conn:
result_proxy = await conn.execute(
text("SELECT * FROM benchmark_test LIMIT 10")
)
result_proxy.fetchall()
end = time.perf_counter()
result.add_time(end - start)
finally:
await engine.dispose()
return result
async def benchmark_bulk_insert(
url: str, batch_size: int = 1000, iterations: int = 10
) -> BenchmarkResult:
"""Benchmark bulk INSERT operations."""
result = BenchmarkResult(f"Bulk INSERT ({batch_size} rows)")
engine = create_async_engine(
url, echo=False, pool_size=10, max_overflow=20
)
try:
for _iteration in range(iterations):
# Clear table before each iteration
async with engine.begin() as conn:
await conn.execute(text("TRUNCATE benchmark_test"))
start = time.perf_counter()
async with engine.begin() as conn:
for i in range(batch_size):
await conn.execute(
text(
"INSERT INTO benchmark_test (name, description, value) VALUES (:name, :desc, :val)"
),
{
"name": f"test_{i}",
"desc": f"Description {i}",
"val": i,
},
)
end = time.perf_counter()
result.add_time(end - start)
finally:
await engine.dispose()
return result
async def benchmark_executemany(
url: str, batch_size: int = 1000, iterations: int = 10
) -> BenchmarkResult:
"""Benchmark executemany operations."""
result = BenchmarkResult(f"executemany ({batch_size} rows)")
engine = create_async_engine(
url, echo=False, pool_size=10, max_overflow=20
)
try:
for _iteration in range(iterations):
# Clear table before each iteration
async with engine.begin() as conn:
await conn.execute(text("TRUNCATE benchmark_test"))
# Prepare batch data
batch_data = [
{"name": f"test_{i}", "desc": f"Description {i}", "val": i}
for i in range(batch_size)
]
start = time.perf_counter()
async with engine.begin() as conn:
await conn.execute(
text(
"INSERT INTO benchmark_test (name, description, value) VALUES (:name, :desc, :val)"
),
batch_data,
)
end = time.perf_counter()
result.add_time(end - start)
finally:
await engine.dispose()
return result
async def benchmark_complex_query(
url: str, iterations: int = 100
) -> BenchmarkResult:
"""Benchmark complex queries with aggregations."""
result = BenchmarkResult("Complex query with aggregation")
engine = create_async_engine(
url, echo=False, pool_size=10, max_overflow=20
)
try:
# Populate with test data
async with engine.begin() as conn:
await conn.execute(text("TRUNCATE benchmark_test"))
for i in range(1000):
await conn.execute(
text(
"INSERT INTO benchmark_test (name, description, value) VALUES (:name, :desc, :val)"
),
{
"name": f"test_{i % 10}",
"desc": f"Description {i}",
"val": i,
},
)
complex_query = text("""
SELECT
name,
COUNT(*) as count,
AVG(value) as avg_value,
MAX(value) as max_value,
MIN(value) as min_value
FROM benchmark_test
GROUP BY name
HAVING COUNT(*) > 10
ORDER BY count DESC
""")
for _ in range(iterations):
start = time.perf_counter()
async with engine.connect() as conn:
result_proxy = await conn.execute(complex_query)
result_proxy.fetchall()
end = time.perf_counter()
result.add_time(end - start)
finally:
await engine.dispose()
return result
async def benchmark_transaction(
url: str, iterations: int = 50
) -> BenchmarkResult:
"""Benchmark transaction performance."""
result = BenchmarkResult("Transaction with multiple operations")
engine = create_async_engine(
url, echo=False, pool_size=10, max_overflow=20
)
try:
for _ in range(iterations):
start = time.perf_counter()
async with engine.begin() as conn:
# Multiple operations within transaction
await conn.execute(
text(
"INSERT INTO benchmark_test (name, description, value) VALUES (:name, :desc, :val)"
),
{"name": "tx_test", "desc": "Transaction test", "val": 1},
)
await conn.execute(
text(
"UPDATE benchmark_test SET value = value + 1 WHERE name = :name"
),
{"name": "tx_test"},
)
await conn.execute(
text("SELECT * FROM benchmark_test WHERE name = :name"),
{"name": "tx_test"},
)
await conn.execute(
text("DELETE FROM benchmark_test WHERE name = :name"),
{"name": "tx_test"},
)
end = time.perf_counter()
result.add_time(end - start)
finally:
await engine.dispose()
return result
async def run_benchmarks(
url: str, dialect_name: str
) -> dict[str, BenchmarkResult]:
"""Run all benchmarks for a specific dialect."""
print(f"\n{'=' * 60}")
print(f"Running benchmarks for {dialect_name}")
print(f"{'=' * 60}\n")
results = {}
# Setup database
print("Setting up database...")
await setup_database(url)
# Run benchmarks
print("Running simple SELECT benchmark...")
results["simple_select"] = await benchmark_simple_select(
url, iterations=100
)
print("Running bulk INSERT benchmark...")
results["bulk_insert"] = await benchmark_bulk_insert(
url, batch_size=1000, iterations=10
)
print("Running executemany benchmark...")
results["executemany"] = await benchmark_executemany(
url, batch_size=1000, iterations=10
)
print("Running complex query benchmark...")
results["complex_query"] = await benchmark_complex_query(
url, iterations=100
)
print("Running transaction benchmark...")
results["transaction"] = await benchmark_transaction(url, iterations=50)
return results
def print_comparison(
psqlpy_results: dict[str, BenchmarkResult],
asyncpg_results: dict[str, BenchmarkResult],
) -> None:
"""Print comparison of results."""
print(f"\n{'=' * 60}")
print("PERFORMANCE COMPARISON RESULTS")
print(f"{'=' * 60}\n")
for benchmark_name in psqlpy_results:
psqlpy_stats = psqlpy_results[benchmark_name].get_stats()
asyncpg_stats = asyncpg_results[benchmark_name].get_stats()
psqlpy_mean = psqlpy_stats["mean"] * 1000 # Convert to ms
asyncpg_mean = asyncpg_stats["mean"] * 1000 # Convert to ms
improvement = (
((asyncpg_mean - psqlpy_mean) / asyncpg_mean) * 100
if asyncpg_mean > 0
else 0
)
print(f"\n{benchmark_name.upper().replace('_', ' ')}:")
print(f" psqlpy-sqlalchemy: {psqlpy_mean:.2f}ms (mean)")
print(f" asyncpg: {asyncpg_mean:.2f}ms (mean)")
if improvement > 0:
print(f" ✓ psqlpy is {improvement:.1f}% FASTER")
elif improvement < 0:
print(f" ✗ psqlpy is {abs(improvement):.1f}% SLOWER")
else:
print(" = Performance is EQUAL")
print(f"\n{'=' * 60}")
async def main() -> int:
"""Main benchmark runner."""
print("PostgreSQL SQLAlchemy Dialect Performance Comparison")
print("Comparing psqlpy-sqlalchemy vs asyncpg")
print("\nThis may take several minutes...\n")
try:
# Warmup both connections first
print("Warming up connections...")
for url in [PSQLPY_URL, ASYNCPG_URL]:
engine = create_async_engine(url, echo=False)
async with engine.connect() as conn:
for _ in range(10):
await conn.execute(text("SELECT 1"))
await engine.dispose()
# Run asyncpg FIRST to give psqlpy the "second run" advantage
# This makes the comparison more fair
print("\nRunning asyncpg benchmarks (first)...")
asyncpg_results = await run_benchmarks(ASYNCPG_URL, "asyncpg")
# Run psqlpy benchmarks second
print("Running psqlpy-sqlalchemy benchmarks (second)...")
psqlpy_results = await run_benchmarks(PSQLPY_URL, "psqlpy-sqlalchemy")
# Print detailed results
print("\n" + "=" * 60)
print("DETAILED RESULTS")
print("=" * 60)
print("\n--- psqlpy-sqlalchemy ---")
for result in psqlpy_results.values():
print(f"\n{result}")
print("\n--- asyncpg ---")
for result in asyncpg_results.values():
print(f"\n{result}")
# Print comparison
print_comparison(psqlpy_results, asyncpg_results)
except Exception as e:
print(f"\n❌ Error running benchmarks: {e}")
import traceback
traceback.print_exc()
return 1
return 0
if __name__ == "__main__":
exit_code = asyncio.run(main())
exit(exit_code)