Repository navigation
perf: tune SQLite snapshot imports #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """Compare default SQLite writes with the SnapshotDb import configuration. | ||
|
|
||
| Run with ``PYTHONPATH=src python benchmarks/sqlite_write_throughput.py``. | ||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import argparse | ||
| import tempfile | ||
| import time | ||
| from contextlib import ExitStack | ||
| from pathlib import Path | ||
|
|
||
| from pt_snap_cli.vendor.memsnapdump.tools.adaptors.database.snapshot_db import SnapshotDb | ||
| from pt_snap_cli.vendor.memsnapdump.util.sqlite_meta import SqliteDB | ||
|
|
||
|
|
||
| def measure(db: SqliteDB, rows: int, batch_size: int) -> float: | ||
| db.conn.execute("CREATE TABLE benchmark (id INTEGER, payload TEXT)") | ||
| started = time.perf_counter() | ||
| for offset in range(0, rows, batch_size): | ||
| batch_end = min(offset + batch_size, rows) | ||
| db.conn.executemany( | ||
| "INSERT INTO benchmark VALUES (?, ?)", | ||
| ((index, "x" * 256) for index in range(offset, batch_end)), | ||
| ) | ||
| db.conn.commit() | ||
| return time.perf_counter() - started | ||
|
|
||
|
|
||
| def main() -> None: | ||
| parser = argparse.ArgumentParser() | ||
| parser.add_argument("--rows", type=int, default=100_000) | ||
| parser.add_argument("--batch-size", type=int, default=1_000) | ||
| args = parser.parse_args() | ||
|
|
||
| with tempfile.TemporaryDirectory() as tmp_dir, ExitStack() as stack: | ||
| root = Path(tmp_dir) | ||
| baseline = SqliteDB(str(root / "baseline.db")) | ||
| stack.callback(baseline.conn.close) | ||
| optimized = SnapshotDb(str(root / "optimized.db")) | ||
| stack.callback(optimized.conn.close) | ||
| baseline_seconds = measure(baseline, args.rows, args.batch_size) | ||
| optimized_seconds = measure(optimized, args.rows, args.batch_size) | ||
|
|
||
| print(f"default: {baseline_seconds:.3f}s") | ||
| print(f"optimized: {optimized_seconds:.3f}s") | ||
| print(f"speedup: {baseline_seconds / optimized_seconds:.2f}x") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import sqlite3 | ||
| from pathlib import Path | ||
|
|
||
| from pt_snap_cli.vendor.memsnapdump.tools.adaptors.database.snapshot_db import SnapshotDb | ||
| from pt_snap_cli.vendor.memsnapdump.util.sqlite_meta import SqliteDB | ||
|
|
||
|
|
||
| def _pragma(conn: sqlite3.Connection, name: str) -> object: | ||
| return conn.execute(f"PRAGMA {name}").fetchone()[0] | ||
|
|
||
|
|
||
| def test_snapshot_db_optimizes_import_writes(tmp_path: Path) -> None: | ||
| db = SnapshotDb(str(tmp_path / "snapshot.db")) | ||
| try: | ||
| assert _pragma(db.conn, "journal_mode") == "memory" | ||
| assert _pragma(db.conn, "synchronous") == 0 | ||
| assert _pragma(db.conn, "cache_size") == -65536 | ||
| finally: | ||
| db.conn.close() | ||
|
|
||
|
|
||
| def test_generic_sqlite_db_keeps_default_write_settings(tmp_path: Path) -> None: | ||
| baseline = sqlite3.connect(tmp_path / "baseline.db") | ||
| db = SqliteDB(str(tmp_path / "generic.db")) | ||
| try: | ||
| for name in ("journal_mode", "synchronous", "cache_size"): | ||
| assert _pragma(db.conn, name) == _pragma(baseline, name) | ||
| finally: | ||
| db.conn.close() | ||
| baseline.close() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.