-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathcompile_profile.py
More file actions
203 lines (171 loc) · 6.08 KB
/
compile_profile.py
File metadata and controls
203 lines (171 loc) · 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
"""Compile profile benchmark runner for DataFusion.
Builds the `dfbench` benchmark binary with several Cargo profiles
(e.g. `--release` or `--profile ci`), runs the full TPC-H suite against
the Parquet data under `benchmarks/data/tpch_sf1`, and reports compile
time, execution time, and resulting binary size.
See `benchmarks/README.md` for usages.
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
import time
from pathlib import Path
from typing import Iterable, NamedTuple
REPO_ROOT = Path(__file__).resolve().parents[1]
DEFAULT_DATA_DIR = REPO_ROOT / "benchmarks" / "data" / "tpch_sf1"
DEFAULT_ITERATIONS = 1
DEFAULT_FORMAT = "parquet"
DEFAULT_PARTITIONS: int | None = None
BENCHMARK_PACKAGE = "datafusion-benchmarks"
BENCHMARK_BINARY = "dfbench.exe" if os.name == "nt" else "dfbench"
PROFILE_TARGET_DIR = {
"dev": "debug",
"release": "release",
"ci": "ci",
"ci-optimized": "ci-optimized",
"release-nonlto": "release-nonlto",
"profiling": "profiling",
}
class ProfileResult(NamedTuple):
profile: str
compile_seconds: float
run_seconds: float
binary_bytes: int
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--profiles",
nargs="+",
default=list(PROFILE_TARGET_DIR.keys()),
help=(
"Cargo profiles to test "
"(default: dev release ci ci-optimized release-nonlto profiling)"
),
)
parser.add_argument(
"--data",
type=Path,
default=DEFAULT_DATA_DIR,
help="Path to TPCH dataset (default: benchmarks/data/tpch_sf1)",
)
return parser.parse_args()
def timed_run(command: Iterable[str]) -> float:
start = time.perf_counter()
try:
subprocess.run(command, cwd=REPO_ROOT, check=True)
except subprocess.CalledProcessError as exc:
raise RuntimeError(f"command failed: {' '.join(command)}") from exc
return time.perf_counter() - start
def cargo_build(profile: str) -> float:
if profile == "dev":
command = [
"cargo",
"build",
"--package",
BENCHMARK_PACKAGE,
"--bin",
"dfbench",
]
else:
command = [
"cargo",
"build",
"--profile",
profile,
"--package",
BENCHMARK_PACKAGE,
"--bin",
"dfbench",
]
return timed_run(command)
def cargo_clean(profile: str) -> None:
command = ["cargo", "clean", "--profile", profile]
try:
subprocess.run(command, cwd=REPO_ROOT, check=True)
except subprocess.CalledProcessError as exc:
raise RuntimeError(f"failed to clean cargo artifacts for profile '{profile}'") from exc
def run_benchmark(profile: str, data_path: Path) -> float:
binary_dir = PROFILE_TARGET_DIR.get(profile)
if not binary_dir:
raise ValueError(f"unknown profile '{profile}'")
binary_path = REPO_ROOT / "target" / binary_dir / BENCHMARK_BINARY
if not binary_path.exists():
raise FileNotFoundError(f"compiled binary not found at {binary_path}")
command = [
str(binary_path),
"tpch",
"--iterations",
str(DEFAULT_ITERATIONS),
"--path",
str(data_path),
"--format",
DEFAULT_FORMAT,
]
if DEFAULT_PARTITIONS is not None:
command.extend(["--partitions", str(DEFAULT_PARTITIONS)])
env = os.environ.copy()
env.setdefault("RUST_LOG", "warn")
start = time.perf_counter()
try:
subprocess.run(command, cwd=REPO_ROOT, env=env, check=True)
except subprocess.CalledProcessError as exc:
raise RuntimeError(f"benchmark failed for profile '{profile}'") from exc
return time.perf_counter() - start
def binary_size(profile: str) -> int:
binary_dir = PROFILE_TARGET_DIR[profile]
binary_path = REPO_ROOT / "target" / binary_dir / BENCHMARK_BINARY
return binary_path.stat().st_size
def human_time(seconds: float) -> str:
return f"{seconds:6.2f}s"
def human_size(size: int) -> str:
value = float(size)
for unit in ("B", "KB", "MB", "GB", "TB"):
if value < 1024 or unit == "TB":
return f"{value:6.1f}{unit}"
value /= 1024
return f"{value:6.1f}TB"
def main() -> None:
args = parse_args()
data_path = args.data.resolve()
if not data_path.exists():
print(f"Data directory not found: {data_path}", file=sys.stderr)
sys.exit(1)
results: list[ProfileResult] = []
for profile in args.profiles:
print(f"\n=== Profile: {profile} ===")
print("Cleaning previous build artifacts...")
cargo_clean(profile)
compile_seconds = cargo_build(profile)
run_seconds = run_benchmark(profile, data_path)
size_bytes = binary_size(profile)
results.append(ProfileResult(profile, compile_seconds, run_seconds, size_bytes))
print("\nSummary")
header = f"{'Profile':<15}{'Compile':>12}{'Run':>12}{'Size':>12}"
print(header)
print("-" * len(header))
for result in results:
print(
f"{result.profile:<15}{human_time(result.compile_seconds):>12}"
f"{human_time(result.run_seconds):>12}{human_size(result.binary_bytes):>12}"
)
if __name__ == "__main__":
main()