feat(bench): add SQLite and PostgreSQL wrappers for benchmark queries

- implement SQLite and PostgreSQL wrappers to execute benchmark queries
- update README to reflect task 09 as implemented
- enhance storage size updates to handle (format, variant) replacements
- clarify benchmarking methodology for remote PostgreSQL setup
This commit is contained in:
administrator
2026-07-11 22:34:23 -04:00
parent 48c102d2a4
commit eda11aa63e
6 changed files with 686 additions and 6 deletions

View File

@@ -182,6 +182,55 @@ PY_FORMATS = [
("rdf", "rdf_queries", '"rdf" / "oxigraph_store"'),
]
SQLITE_WRAPPER = '''"""Benchmark query q{n} for the sqlite format (generated by task 08).
Executes the adjacent q{n}.sql against out/sqlite/tribo.db (read-only) and
prints the canonical result rows to stdout at full float precision; the
benchmark runner (task 09) captures and validates them against
out/bench/expected/q{n}.rows.csv.
"""
import sqlite3
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[4]
sys.path.insert(0, str(REPO_ROOT))
from common.results import print_rows
if __name__ == "__main__":
sql = Path(__file__).with_suffix(".sql").read_text(encoding="ascii")
db = REPO_ROOT / "out" / "sqlite" / "tribo.db"
conn = sqlite3.connect(f"file:{{db.as_posix()}}?mode=ro", uri=True)
print_rows([tuple(r) for r in conn.execute(sql).fetchall()])
'''
PG_WRAPPER = '''"""Benchmark query q{n} for the pg format (generated by task 08).
Executes the adjacent q{n}.sql on the PostgreSQL instance addressed by
TRIBO_PG_DSN and prints the canonical result rows to stdout at full float
precision; the benchmark runner (task 09) captures and validates them
against out/bench/expected/q{n}.rows.csv.
"""
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parents[4]
sys.path.insert(0, str(REPO_ROOT))
import psycopg
from common.pg import get_dsn
from common.results import print_rows
if __name__ == "__main__":
sql = Path(__file__).with_suffix(".sql").read_text(encoding="ascii")
with psycopg.connect(get_dsn()) as conn:
print_rows([tuple(r) for r in conn.execute(sql).fetchall()])
'''
SQL_WRAPPERS = {"sqlite": SQLITE_WRAPPER, "pg": PG_WRAPPER}
def write_query_files(bench_dir: Path) -> int:
queries_dir = bench_dir / "queries"
@@ -198,7 +247,8 @@ def write_query_files(bench_dir: Path) -> int:
fmt_dir.mkdir(parents=True, exist_ok=True)
for n, text in sql.items():
(fmt_dir / f"q{n}.sql").write_text(text, encoding="ascii", newline="\n")
written += 1
(fmt_dir / f"q{n}.py").write_text(SQL_WRAPPERS[fmt].format(n=n), encoding="ascii", newline="\n")
written += 2
rdf_dir = queries_dir / "rdf"
for n, keys in RDF_SPARQL_FILES.items():
text = "\n\n".join(f"# retrieval query: {key}\n{rdf_queries.SPARQL[key]}" for key in keys)