-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathbenchmark.py
More file actions
344 lines (286 loc) · 8.98 KB
/
benchmark.py
File metadata and controls
344 lines (286 loc) · 8.98 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
"""Benchmark speedcopy against shutil.copyfile on a mounted share path."""
from __future__ import annotations
import argparse
import os
import shutil
import statistics
import tempfile
import time
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, List, Sequence, Tuple
import speedcopy
MB_BYTES = 1024 * 1024
CHUNK_MB = 4
DEFAULT_SIZES_MB = (8, 64, 256)
DEFAULT_REPEATS = 3
DEFAULT_COPIES_PER_WORKER = 3
@dataclass
class CaseResult:
"""Store benchmark data for one file-size case."""
size_mb: int
baseline_seconds: float
speedcopy_seconds: float
baseline_mb_s: float
speedcopy_mb_s: float
speedup: float
@dataclass
class RunConfig:
"""Store run configuration reused by each benchmarked method."""
repeats: int
workers: int
copies_per_worker: int
def parse_sizes(value: str) -> Tuple[int, ...]:
"""Parse comma-separated file sizes in MB into a validated tuple.
Returns:
Tuple of positive integer file sizes in MB.
Raises:
argparse.ArgumentTypeError: Sizes are missing or non-positive.
"""
parsed: List[int] = []
for raw in value.split(","):
item = raw.strip()
if not item:
continue
parsed.append(int(item))
if not parsed:
msg = "Provide at least one file size via --sizes-mb."
raise argparse.ArgumentTypeError(msg)
if any(size <= 0 for size in parsed):
msg = "All file sizes must be positive integers."
raise argparse.ArgumentTypeError(msg)
return tuple(parsed)
def parse_args() -> argparse.Namespace:
"""Create CLI parser and return parsed arguments.
Returns:
Parsed benchmark CLI arguments.
"""
parser = argparse.ArgumentParser(
description=(
"Compare shutil.copyfile and speedcopy.copyfile "
"on a mounted share."
),
)
parser.add_argument(
"share_path",
help="Mounted share path used for source and destination files.",
)
parser.add_argument(
"--sizes-mb",
type=parse_sizes,
default=DEFAULT_SIZES_MB,
help=(
"Comma-separated source file sizes in MB "
f"(default: {','.join(str(size) for size in DEFAULT_SIZES_MB)})."
),
)
parser.add_argument(
"--repeats",
type=int,
default=DEFAULT_REPEATS,
help=(
"How many timed runs to execute per method "
f"(default: {DEFAULT_REPEATS})."
),
)
parser.add_argument(
"--copies-per-worker",
type=int,
default=DEFAULT_COPIES_PER_WORKER,
help=(
"Copies each worker performs in one timed run "
f"(default: {DEFAULT_COPIES_PER_WORKER})."
),
)
parser.add_argument(
"--workers",
type=int,
default=1,
help="Set >1 to run copies concurrently in multiple threads.",
)
args = parser.parse_args()
if args.repeats < 1:
parser.error("--repeats must be at least 1")
if args.copies_per_worker < 1:
parser.error("--copies-per-worker must be at least 1")
if args.workers < 1:
parser.error("--workers must be at least 1")
return args
def generate_file(filepath: Path, size_mb: int) -> None:
"""Create a file with random contents and a target size in MB."""
chunk_bytes = CHUNK_MB * MB_BYTES
full_chunks, remainder = divmod(size_mb * MB_BYTES, chunk_bytes)
with filepath.open("wb") as stream:
for _ in range(full_chunks):
stream.write(os.urandom(chunk_bytes))
if remainder:
stream.write(os.urandom(remainder))
def copy_worker(
copy_fn: Callable[[str, str], str],
src: str,
run_dir: Path,
worker_id: int,
copies_per_worker: int,
) -> None:
"""Run copy operations for one worker and remove each destination file."""
for index in range(copies_per_worker):
dst = run_dir / f"w{worker_id:02d}_copy{index:03d}.bin"
copy_fn(src, str(dst))
dst.unlink()
def run_once(
copy_fn: Callable[[str, str], str],
src: str,
run_dir: Path,
workers: int,
copies_per_worker: int,
) -> float:
"""Run one timed benchmark pass and return elapsed seconds.
Returns:
Elapsed wall-clock time in seconds.
"""
started = time.perf_counter()
if workers == 1:
copy_worker(copy_fn, src, run_dir, 0, copies_per_worker)
return time.perf_counter() - started
with ThreadPoolExecutor(max_workers=workers) as executor:
futures = [
executor.submit(
copy_worker,
copy_fn,
src,
run_dir,
worker,
copies_per_worker,
)
for worker in range(workers)
]
for future in futures:
future.result()
return time.perf_counter() - started
def time_method(
method_name: str,
copy_fn: Callable[[str, str], str],
src: str,
bench_dir: Path,
config: RunConfig,
) -> float:
"""Time one copy method and return the median run time.
Returns:
Median elapsed wall-clock seconds for the method.
"""
timings: List[float] = []
for repeat_index in range(config.repeats):
run_dir = bench_dir / f"{method_name}_run{repeat_index:02d}"
run_dir.mkdir()
elapsed = run_once(
copy_fn,
src,
run_dir,
config.workers,
config.copies_per_worker,
)
timings.append(elapsed)
run_dir.rmdir()
return statistics.median(timings)
def benchmark_case(
size_mb: int,
bench_dir: Path,
config: RunConfig,
) -> CaseResult:
"""Benchmark one source file size and return timing/speedup metrics.
Returns:
One completed case result with timings, throughput, and speedup.
"""
src = bench_dir / f"src_{size_mb}mb.bin"
generate_file(src, size_mb)
source = str(src)
baseline_seconds = time_method(
"shutil",
shutil.copyfile,
source,
bench_dir,
config,
)
speedcopy_seconds = time_method(
"speedcopy",
speedcopy.copyfile,
source,
bench_dir,
config,
)
src.unlink()
total_mb = float(size_mb * config.workers * config.copies_per_worker)
baseline_mb_s = total_mb / baseline_seconds
speedcopy_mb_s = total_mb / speedcopy_seconds
return CaseResult(
size_mb=size_mb,
baseline_seconds=baseline_seconds,
speedcopy_seconds=speedcopy_seconds,
baseline_mb_s=baseline_mb_s,
speedcopy_mb_s=speedcopy_mb_s,
speedup=baseline_seconds / speedcopy_seconds,
)
def print_results(results: Sequence[CaseResult], workers: int) -> None:
"""Print concise benchmark output and an aggregate summary."""
mode = "multithreaded" if workers > 1 else "single-threaded"
print(f"mode={mode}, workers={workers}")
print(
"size(MB) | shutil(s) speedcopy(s) | shutil(MB/s) speedcopy(MB/s) "
"| gain"
)
total_shutil = 0.0
total_speedcopy = 0.0
for result in results:
total_shutil += result.baseline_seconds
total_speedcopy += result.speedcopy_seconds
print(
f"{result.size_mb:>8} | "
f"{result.baseline_seconds:>8.3f} "
f"{result.speedcopy_seconds:>11.3f} | "
f"{result.baseline_mb_s:>11.1f} {result.speedcopy_mb_s:>14.1f} | "
f"{result.speedup:>5.2f}x"
)
overall_gain = total_shutil / total_speedcopy
print("-" * 76)
print(
f"overall: shutil={total_shutil:.3f}s "
f"speedcopy={total_speedcopy:.3f}s "
f"gain={overall_gain:.2f}x"
)
def main() -> None:
"""Run benchmark suite for requested sizes and execution mode.
Raises:
NotADirectoryError: `share_path` is missing or not a directory.
"""
args = parse_args()
print("-=> Speedcopy benchmark script <=-")
if args.workers > 1:
print(f"Running in multithreaded mode with {args.workers} workers.")
else:
print("Running in single-threaded mode.")
print(f"Running with file sizes {args.sizes_mb} MB.")
print(f"Using path: {args.share_path}")
share_path = Path(args.share_path).expanduser().resolve()
if not share_path.exists() or not share_path.is_dir():
msg = f"Path is not an existing directory: {share_path}"
raise NotADirectoryError(msg)
config = RunConfig(
repeats=args.repeats,
workers=args.workers,
copies_per_worker=args.copies_per_worker,
)
sizes = args.sizes_mb
with tempfile.TemporaryDirectory(dir=str(share_path)) as temp_dir:
bench_dir = Path(temp_dir)
results = [
benchmark_case(
size_mb=size_mb,
bench_dir=bench_dir,
config=config,
)
for size_mb in sizes
]
print_results(results, args.workers)
if __name__ == "__main__":
main()