Skip to content

Commit d5a99e5

Browse files
sumedhsakdeoclaude
andcommitted
feat: add read throughput micro-benchmark for ArrowScan configurations
Measures records/sec and peak memory across streaming, concurrent_files, and batch_size configurations to validate performance characteristics of the new scan modes introduced for apache#3036. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 988d3f2 commit d5a99e5

1 file changed

Lines changed: 147 additions & 0 deletions

File tree

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
"""Read throughput micro-benchmark for ArrowScan configurations.
18+
19+
Measures records/sec and peak memory across streaming, concurrent_files,
20+
and batch_size configurations introduced for issue #3036.
21+
22+
Run with: uv run pytest tests/benchmark/test_read_benchmark.py -v -s -m benchmark
23+
"""
24+
25+
import statistics
26+
import timeit
27+
import tracemalloc
28+
from datetime import datetime, timezone
29+
30+
import pyarrow as pa
31+
import pyarrow.parquet as pq
32+
import pytest
33+
34+
from pyiceberg.catalog.sql import SqlCatalog
35+
36+
NUM_FILES = 8
37+
ROWS_PER_FILE = 500_000
38+
TOTAL_ROWS = NUM_FILES * ROWS_PER_FILE
39+
NUM_RUNS = 3
40+
41+
42+
def _generate_parquet_file(path: str, num_rows: int, seed: int) -> pa.Schema:
43+
"""Write a synthetic Parquet file and return its schema."""
44+
table = pa.table(
45+
{
46+
"id": pa.array(range(seed, seed + num_rows), type=pa.int64()),
47+
"value": pa.array([float(i) * 0.1 for i in range(num_rows)], type=pa.float64()),
48+
"label": pa.array([f"row_{i}" for i in range(num_rows)], type=pa.string()),
49+
"flag": pa.array([i % 2 == 0 for i in range(num_rows)], type=pa.bool_()),
50+
"ts": pa.array([datetime.now(timezone.utc)] * num_rows, type=pa.timestamp("us", tz="UTC")),
51+
}
52+
)
53+
pq.write_table(table, path)
54+
return table.schema
55+
56+
57+
@pytest.fixture(scope="session")
58+
def benchmark_table(tmp_path_factory: pytest.TempPathFactory) -> "pyiceberg.table.Table": # noqa: F821
59+
"""Create a catalog and table with synthetic Parquet files for benchmarking."""
60+
warehouse_path = str(tmp_path_factory.mktemp("benchmark_warehouse"))
61+
catalog = SqlCatalog(
62+
"benchmark",
63+
uri=f"sqlite:///{warehouse_path}/pyiceberg_catalog.db",
64+
warehouse=f"file://{warehouse_path}",
65+
)
66+
catalog.create_namespace("default")
67+
68+
# Generate files and append to table
69+
first_schema = None
70+
table = None
71+
for i in range(NUM_FILES):
72+
file_path = f"{warehouse_path}/data_{i}.parquet"
73+
schema = _generate_parquet_file(file_path, ROWS_PER_FILE, seed=i * ROWS_PER_FILE)
74+
if first_schema is None:
75+
first_schema = schema
76+
77+
file_table = pq.read_table(file_path)
78+
if table is None:
79+
table = catalog.create_table("default.benchmark_read", schema=file_table.schema)
80+
table.append(file_table)
81+
82+
return table
83+
84+
85+
@pytest.mark.benchmark
86+
@pytest.mark.parametrize(
87+
"streaming,concurrent_files,batch_size",
88+
[
89+
pytest.param(False, 1, None, id="default"),
90+
pytest.param(False, 1, 1000, id="default-batch1k"),
91+
pytest.param(False, 1, 10000, id="default-batch10k"),
92+
pytest.param(True, 1, None, id="streaming"),
93+
pytest.param(True, 1, 1000, id="streaming-batch1k"),
94+
pytest.param(True, 1, 10000, id="streaming-batch10k"),
95+
pytest.param(True, 2, None, id="concurrent2"),
96+
pytest.param(True, 2, 10000, id="concurrent2-batch10k"),
97+
pytest.param(True, 4, None, id="concurrent4"),
98+
pytest.param(True, 4, 10000, id="concurrent4-batch10k"),
99+
],
100+
)
101+
def test_read_throughput(
102+
benchmark_table: "pyiceberg.table.Table", # noqa: F821
103+
streaming: bool,
104+
concurrent_files: int,
105+
batch_size: int | None,
106+
) -> None:
107+
"""Measure records/sec and peak memory for a scan configuration."""
108+
config_str = f"streaming={streaming}, concurrent_files={concurrent_files}, batch_size={batch_size}"
109+
print(f"\n--- ArrowScan Read Throughput Benchmark ---")
110+
print(f"Config: {config_str}")
111+
print(f" Files: {NUM_FILES}, Rows per file: {ROWS_PER_FILE}, Total rows: {TOTAL_ROWS}")
112+
113+
elapsed_times: list[float] = []
114+
throughputs: list[float] = []
115+
peak_memories: list[int] = []
116+
117+
for run in range(NUM_RUNS):
118+
tracemalloc.start()
119+
120+
start = timeit.default_timer()
121+
total_rows = 0
122+
for batch in benchmark_table.scan().to_arrow_batch_reader(
123+
batch_size=batch_size,
124+
streaming=streaming,
125+
concurrent_files=concurrent_files,
126+
):
127+
total_rows += len(batch)
128+
elapsed = timeit.default_timer() - start
129+
130+
_, peak_mem = tracemalloc.get_traced_memory()
131+
tracemalloc.stop()
132+
133+
rows_per_sec = total_rows / elapsed if elapsed > 0 else 0
134+
elapsed_times.append(elapsed)
135+
throughputs.append(rows_per_sec)
136+
peak_memories.append(peak_mem)
137+
138+
print(f" Run {run + 1}: {elapsed:.2f}s, {rows_per_sec:,.0f} rows/s, peak mem: {peak_mem / (1024 * 1024):.1f} MB")
139+
140+
assert total_rows == TOTAL_ROWS, f"Expected {TOTAL_ROWS} rows, got {total_rows}"
141+
142+
mean_elapsed = statistics.mean(elapsed_times)
143+
stdev_elapsed = statistics.stdev(elapsed_times) if len(elapsed_times) > 1 else 0.0
144+
mean_throughput = statistics.mean(throughputs)
145+
mean_peak_mem = statistics.mean(peak_memories)
146+
147+
print(f" Mean: {mean_elapsed:.2f}s ± {stdev_elapsed:.2f}s, {mean_throughput:,.0f} rows/s, peak mem: {mean_peak_mem / (1024 * 1024):.1f} MB")

0 commit comments

Comments
 (0)