|
| 1 | +"""Standalone CodSpeed benchmark module — NOT integrated (not in pyproject, not in CI, not committed). |
| 2 | +
|
| 3 | +Purpose: A/B the binding-layer perf between the two builds (pybind11 `main` vs nanobind cutover), in particular |
| 4 | +the narrow-column `fetchall` regression. Run the SAME file under each build's interpreter and compare: |
| 5 | +
|
| 6 | + M=/Users/evert/projects/duckdb-python/main/.venv-release/bin/python |
| 7 | + C=/Users/evert/projects/duckdb-python/wt-cutover/.venv-release/bin/python |
| 8 | + cd /Users/evert/projects/duckdb-python/wt-cutover |
| 9 | + $M -m pytest benchmarks/test_fetch_perf.py --codspeed --codspeed-mode=walltime -o addopts= |
| 10 | + $C -m pytest benchmarks/test_fetch_perf.py --codspeed --codspeed-mode=walltime -o addopts= |
| 11 | +
|
| 12 | +NOTE: macOS arm64 has no Valgrind, so only `--codspeed-mode=walltime` works locally (wall-clock stats). The |
| 13 | +deterministic instruction-count mode (`--codspeed-mode=simulation`) needs Linux + the CodSpeed instrument |
| 14 | +(CI, or `codspeed run` in a Linux container). In CI/cloud, CodSpeed compares each run against a git baseline; |
| 15 | +locally we get the same benchmark workflow but A/B by running the file under the two interpreters by hand. |
| 16 | +""" |
| 17 | + |
| 18 | +import duckdb |
| 19 | +import pytest |
| 20 | + |
| 21 | + |
| 22 | +@pytest.fixture |
| 23 | +def con(): |
| 24 | + c = duckdb.connect() |
| 25 | + yield c |
| 26 | + c.close() |
| 27 | + |
| 28 | + |
| 29 | +def _bench_fetchall(benchmark, con, query): |
| 30 | + con.execute(query).fetchall() # warm the engine before measuring |
| 31 | + benchmark(lambda: con.execute(query).fetchall()) |
| 32 | + |
| 33 | + |
| 34 | +def test_fetchall_int(benchmark, con): |
| 35 | + _bench_fetchall(benchmark, con, "SELECT i::BIGINT AS a FROM range(200000) t(i)") |
| 36 | + |
| 37 | + |
| 38 | +def test_fetchall_smallint(benchmark, con): |
| 39 | + _bench_fetchall(benchmark, con, "SELECT (i % 100)::INTEGER AS a FROM range(200000) t(i)") |
| 40 | + |
| 41 | + |
| 42 | +def test_fetchall_double(benchmark, con): |
| 43 | + _bench_fetchall(benchmark, con, "SELECT (i * 1.5)::DOUBLE AS a FROM range(200000) t(i)") |
| 44 | + |
| 45 | + |
| 46 | +def test_fetchall_2int(benchmark, con): |
| 47 | + _bench_fetchall(benchmark, con, "SELECT i::BIGINT AS a, (i + 1)::BIGINT AS b FROM range(200000) t(i)") |
| 48 | + |
| 49 | + |
| 50 | +def test_fetchall_str(benchmark, con): |
| 51 | + _bench_fetchall(benchmark, con, "SELECT ('str_value_' || i) AS s FROM range(100000) t(i)") |
| 52 | + |
| 53 | + |
| 54 | +def test_fetchall_mixed(benchmark, con): |
| 55 | + query = ( |
| 56 | + "SELECT i::BIGINT AS bi, ('str_' || i) AS s, [i, i + 1, i + 2] AS lst, " |
| 57 | + "{'a': i, 'b': i + 1} AS st FROM range(50000) t(i)" |
| 58 | + ) |
| 59 | + _bench_fetchall(benchmark, con, query) |
| 60 | + |
| 61 | + |
| 62 | +def test_fetchone_iter(benchmark, con): |
| 63 | + query = "SELECT i::BIGINT AS a, (i * 1.5)::DOUBLE AS b FROM range(100000) t(i)" |
| 64 | + |
| 65 | + def run(): |
| 66 | + rel = con.execute(query) |
| 67 | + while rel.fetchone() is not None: |
| 68 | + pass |
| 69 | + |
| 70 | + benchmark(run) |
| 71 | + |
| 72 | + |
| 73 | +# --------------------------------------------------------------------------- # |
| 74 | +# ADDED: small-N instruction-count-gate variants (the narrow-numeric fixed-cost path, noise-free at range(2048) |
| 75 | +# under simulation mode in CI), expensive scalar OUT-row types (timestamptz pytz-per-row, blob, null-heavy), a |
| 76 | +# heterogeneous per-cell-dispatch row (hugeint+uuid+decimal128+varchar, distinct from homogeneous columns), and |
| 77 | +# the batched fetchmany loop. |
| 78 | +# --------------------------------------------------------------------------- # |
| 79 | + |
| 80 | + |
| 81 | +def test_fetchall_int_gate(benchmark, con): |
| 82 | + _bench_fetchall(benchmark, con, "SELECT i::BIGINT AS a FROM range(2048) t(i)") |
| 83 | + |
| 84 | + |
| 85 | +def test_fetchall_2int_gate(benchmark, con): |
| 86 | + _bench_fetchall(benchmark, con, "SELECT i::BIGINT AS a, (i + 1)::BIGINT AS b FROM range(2048) t(i)") |
| 87 | + |
| 88 | + |
| 89 | +def test_fetchall_null_heavy(benchmark, con): |
| 90 | + _bench_fetchall( |
| 91 | + benchmark, con, "SELECT CASE WHEN i % 2 = 0 THEN NULL ELSE i::BIGINT END FROM range(200000) t(i)" |
| 92 | + ) |
| 93 | + |
| 94 | + |
| 95 | +def test_fetchall_timestamptz(benchmark, con): |
| 96 | + _bench_fetchall( |
| 97 | + benchmark, con, "SELECT (TIMESTAMPTZ '2020-01-01' + (i * INTERVAL 1 SECOND)) FROM range(100000) t(i)" |
| 98 | + ) |
| 99 | + |
| 100 | + |
| 101 | +def test_fetchall_decimal128(benchmark, con): |
| 102 | + _bench_fetchall(benchmark, con, "SELECT ((i * 1.5)::DECIMAL(28, 6)) FROM range(200000) t(i)") |
| 103 | + |
| 104 | + |
| 105 | +def test_fetchall_blob(benchmark, con): |
| 106 | + _bench_fetchall(benchmark, con, "SELECT ('blob_value_' || i)::BLOB FROM range(100000) t(i)") |
| 107 | + |
| 108 | + |
| 109 | +def test_fetchall_mixed_wide(benchmark, con): |
| 110 | + # heterogeneous row -> per-cell type dispatch in the Fetchone column loop (distinct branch/cache profile |
| 111 | + # from the homogeneous single-type columns above) |
| 112 | + query = ( |
| 113 | + "SELECT (i::HUGEINT * 1000000000000) AS h, gen_random_uuid() AS u, " |
| 114 | + "((i * 1.5)::DECIMAL(28, 6)) AS d, ('string_' || i) AS s FROM range(100000) t(i)" |
| 115 | + ) |
| 116 | + _bench_fetchall(benchmark, con, query) |
| 117 | + |
| 118 | + |
| 119 | +def test_fetchmany_batched(benchmark, con): |
| 120 | + query = "SELECT i::BIGINT AS a, (i * 1.5)::DOUBLE AS b FROM range(100000) t(i)" |
| 121 | + |
| 122 | + def run(): |
| 123 | + rel = con.execute(query) |
| 124 | + while True: |
| 125 | + rows = rel.fetchmany(10_000) |
| 126 | + if not rows: |
| 127 | + break |
| 128 | + |
| 129 | + benchmark(run) |
| 130 | + |
| 131 | + |
| 132 | +def test_expr_many(benchmark): |
| 133 | + def run(): |
| 134 | + out = [] |
| 135 | + for i in range(2000): |
| 136 | + col = duckdb.ColumnExpression(f"col_{i}") |
| 137 | + const = duckdb.ConstantExpression(i) |
| 138 | + out.append(((col + const) * duckdb.ConstantExpression(2)).alias(f"a{i}")) |
| 139 | + return len(out) |
| 140 | + |
| 141 | + benchmark(run) |
0 commit comments