-
Notifications
You must be signed in to change notification settings - Fork 32
Description
Based on the tests reported in #7826, equivalent database queries show a significant performance difference between asyncpg and aiopg. Specifically, asyncpg—the library we aim to use—performs worse than aiopg in this scenario.
This issue should explore why this happened since most reports indicate that the new library is actualy better. The conclusions should also suggest some guidelines on how to use asyncpg
✅ Benchmarking Checklist for asyncpg vs. aiopg
1. Connection Management
- Pooling Strategy: Ensure both libraries use comparable connection pooling configurations. For
asyncpg, utilizeasyncpg.create_pool(), and foraiopg, useaiopg.create_pool(). - Pool Size: Set identical
minsizeandmaxsizeparameters to maintain consistency.
2. Query Execution
- Query Complexity: Benchmark both simple (e.g.,
SELECT 1) and complex queries to assess performance across different workloads. - Parameter Binding: Use parameterized queries to prevent SQL injection and ensure fair comparison.
3. Data Volume
- Result Set Size: Test with varying result set sizes (e.g., 10, 1000, 10000 rows) to evaluate how each library handles different data volumes.
4. Concurrency
- Concurrent Connections: Simulate multiple concurrent connections to assess how each library scales under load.
- Async Context: Ensure that the event loop is properly managed and that asynchronous contexts are correctly implemented.
5. Performance Metrics
- Latency: Measure the time taken to execute queries.
- Throughput: Assess the number of queries handled per second.
- Resource Utilization: Monitor CPU and memory usage during benchmarking.
6. Environment Consistency
- Hardware: Run benchmarks on the same hardware to eliminate discrepancies.
- Database Configuration: Ensure the PostgreSQL server settings are identical for both tests.
7. Instrumentation and Monitoring
- Logging: Enable detailed logging to trace query execution paths.
- Profiling Tools: Use tools like
cProfile,py-spy, orasync-profilerto identify bottlenecks.([varrazzo.com]1)
🔍 Tracing and Analysis Tools
EXPLAIN ANALYZE: Use this PostgreSQL command to understand query execution plans and identify inefficiencies.pg_stat_statements: Enable this extension to track execution statistics of all SQL statements.pgbench: Utilize this benchmarking tool to simulate client load and measure performance.asyncpg's Built-in Logging: Leverageasyncpg's logging capabilities to monitor query execution and pool behavior.
📚 Additional Resources
For a deeper understanding and practical examples, consider reviewing the following resources:
-
MagicStack's Benchmark Report: Provides an in-depth performance comparison between
asyncpgand other PostgreSQL drivers. ([magic.io]2) -
Fernando Arteaga's Blog Post: Offers a comprehensive benchmark analysis of
psycopg3andasyncpgwithin a FastAPI application context. ([fernandoarteaga.dev]3) -
GitHub Gist on
asyncpgsavs.aiopg.sa: Presents a practical benchmark comparison between these two libraries. ([gist.github.com]4)
By meticulously following this checklist and utilizing the suggested tools, you can achieve a fair and thorough comparison between asyncpg and aiopg, leading to informed decisions tailored to your application's specific needs.