-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
295 lines (241 loc) · 9.53 KB
/
benchmark.py
File metadata and controls
295 lines (241 loc) · 9.53 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
from __future__ import annotations
import csv
import json
import random
from collections import Counter, defaultdict
from dataclasses import asdict, dataclass
from pathlib import Path
from time import perf_counter
from typing import Iterable
from adaptive_selector import recommend_search_algorithm, recommend_sort_algorithm
from algorithms.searching import SEARCHING_ALGORITHMS
from algorithms.sorting import SORTING_ALGORITHMS
DEFAULT_SIZES = (100, 1000, 5000, 10000)
DEFAULT_TRIALS = 5
SORTING_DATASET_TYPES = ("random", "sorted", "reverse", "nearly_sorted", "few_unique")
SEARCH_TARGET_MODES = ("front", "middle", "end", "missing")
DEFAULT_DASHBOARD_DATA_PATH = Path("docs/results-data.js")
@dataclass(frozen=True)
class BenchmarkRow:
kind: str
dataset_type: str
size: int
scenario: str
algorithm: str
average_time_seconds: float
recommended_algorithm: str
@dataclass(frozen=True)
class BenchmarkSummary:
fastest_counts: dict[str, Counter]
recommendation_hits: dict[str, int]
scenario_totals: dict[str, int]
def generate_sorting_dataset(
size: int, dataset_type: str, rng: random.Random
) -> list[int]:
values = [rng.randint(1, 10_000) for _ in range(size)]
if dataset_type == "random":
return values
if dataset_type == "sorted":
return sorted(values)
if dataset_type == "reverse":
return sorted(values, reverse=True)
if dataset_type == "nearly_sorted":
values = sorted(values)
swap_count = max(1, size // 10)
for _ in range(swap_count):
left = rng.randrange(size)
right = rng.randrange(size)
values[left], values[right] = values[right], values[left]
return values
if dataset_type == "few_unique":
return [rng.randint(1, 10) for _ in range(size)]
raise ValueError(f"Unsupported dataset type: {dataset_type}")
def build_search_dataset(size: int, rng: random.Random) -> list[int]:
return sorted(generate_sorting_dataset(size, "random", rng))
def choose_target(dataset: list[int], mode: str) -> int:
if mode == "front":
return dataset[0]
if mode == "middle":
return dataset[len(dataset) // 2]
if mode == "end":
return dataset[-1]
if mode == "missing":
return max(dataset) + 1 if dataset else 1
raise ValueError(f"Unsupported search scenario: {mode}")
def run_sorting_benchmarks(
*,
sizes: Iterable[int] = DEFAULT_SIZES,
dataset_types: Iterable[str] = SORTING_DATASET_TYPES,
trials: int = DEFAULT_TRIALS,
seed: int = 42,
bubble_limit: int = 2_000,
) -> list[BenchmarkRow]:
rng = random.Random(seed)
rows: list[BenchmarkRow] = []
for dataset_type in dataset_types:
for size in sizes:
dataset = generate_sorting_dataset(size, dataset_type, rng)
expected = sorted(dataset)
recommendation = recommend_sort_algorithm(dataset)
for key, algorithm in SORTING_ALGORITHMS.items():
if key == "bubble" and size > bubble_limit:
continue
total_time = 0.0
for _ in range(trials):
trial_input = list(dataset)
start = perf_counter()
result = algorithm(trial_input)
total_time += perf_counter() - start
if result != expected:
raise ValueError(f"{key} produced an incorrect sorting result.")
rows.append(
BenchmarkRow(
kind="sorting",
dataset_type=dataset_type,
size=size,
scenario="-",
algorithm=key,
average_time_seconds=total_time / trials,
recommended_algorithm=recommendation.algorithm_key,
)
)
return rows
def run_searching_benchmarks(
*,
sizes: Iterable[int] = DEFAULT_SIZES,
target_modes: Iterable[str] = SEARCH_TARGET_MODES,
trials: int = DEFAULT_TRIALS,
seed: int = 42,
repeat_count: int = 1_000,
) -> list[BenchmarkRow]:
rng = random.Random(seed + 1)
rows: list[BenchmarkRow] = []
for size in sizes:
dataset = build_search_dataset(size, rng)
for target_mode in target_modes:
target = choose_target(dataset, target_mode)
recommendation = recommend_search_algorithm(
dataset,
assume_sorted=True,
target_position_hint=target_mode,
)
for key, algorithm in SEARCHING_ALGORITHMS.items():
total_time = 0.0
for _ in range(trials):
start = perf_counter()
result = -1
for _ in range(repeat_count):
result = algorithm(dataset, target)
total_time += perf_counter() - start
if target_mode == "missing":
if result != -1:
raise ValueError(f"{key} should not find a missing target.")
elif result == -1 or dataset[result] != target:
raise ValueError(f"{key} returned an invalid index.")
rows.append(
BenchmarkRow(
kind="searching",
dataset_type="sorted",
size=size,
scenario=target_mode,
algorithm=key,
average_time_seconds=(total_time / trials) / repeat_count,
recommended_algorithm=recommendation.algorithm_key,
)
)
return rows
def run_benchmarks(
*,
kind: str = "all",
sizes: Iterable[int] = DEFAULT_SIZES,
trials: int = DEFAULT_TRIALS,
seed: int = 42,
) -> list[BenchmarkRow]:
rows: list[BenchmarkRow] = []
if kind in {"sorting", "all"}:
rows.extend(run_sorting_benchmarks(sizes=sizes, trials=trials, seed=seed))
if kind in {"searching", "all"}:
rows.extend(run_searching_benchmarks(sizes=sizes, trials=trials, seed=seed))
return rows
def write_results_csv(rows: Iterable[BenchmarkRow], output_path: str | Path) -> None:
path = Path(output_path)
path.parent.mkdir(parents=True, exist_ok=True)
with path.open("w", newline="", encoding="utf-8") as handle:
writer = csv.DictWriter(
handle,
fieldnames=[
"kind",
"dataset_type",
"size",
"scenario",
"algorithm",
"average_time_seconds",
"recommended_algorithm",
],
)
writer.writeheader()
for row in rows:
writer.writerow(asdict(row))
def write_dashboard_data_js(
rows: Iterable[BenchmarkRow], output_path: str | Path = DEFAULT_DASHBOARD_DATA_PATH
) -> None:
row_list = list(rows)
summary = summarize_benchmarks(row_list)
path = Path(output_path)
path.parent.mkdir(parents=True, exist_ok=True)
payload = {
"rows": [asdict(row) for row in row_list],
"summary": {
"fastest_counts": {
kind: dict(counter) for kind, counter in summary.fastest_counts.items()
},
"recommendation_hits": dict(summary.recommendation_hits),
"scenario_totals": dict(summary.scenario_totals),
},
"cases": build_case_summaries(row_list),
}
with path.open("w", encoding="utf-8") as handle:
handle.write("window.BENCHMARK_DASHBOARD_DATA = ")
json.dump(payload, handle, indent=2)
handle.write(";\n")
def build_case_summaries(rows: Iterable[BenchmarkRow]) -> list[dict[str, object]]:
grouped: dict[tuple[str, str, int, str], list[BenchmarkRow]] = defaultdict(list)
for row in rows:
grouped[(row.kind, row.dataset_type, row.size, row.scenario)].append(row)
cases: list[dict[str, object]] = []
for key, group in sorted(grouped.items()):
fastest = min(group, key=lambda item: item.average_time_seconds)
recommendation = group[0].recommended_algorithm
cases.append(
{
"kind": key[0],
"dataset_type": key[1],
"size": key[2],
"scenario": key[3],
"fastest_algorithm": fastest.algorithm,
"fastest_time_seconds": fastest.average_time_seconds,
"recommended_algorithm": recommendation,
"recommendation_hit": recommendation == fastest.algorithm,
}
)
return cases
def summarize_benchmarks(rows: Iterable[BenchmarkRow]) -> BenchmarkSummary:
grouped: dict[tuple[str, str, int, str], list[BenchmarkRow]] = defaultdict(list)
for row in rows:
key = (row.kind, row.dataset_type, row.size, row.scenario)
grouped[key].append(row)
fastest_counts: dict[str, Counter] = {"sorting": Counter(), "searching": Counter()}
recommendation_hits = {"sorting": 0, "searching": 0}
scenario_totals = {"sorting": 0, "searching": 0}
for key, group in grouped.items():
kind = key[0]
fastest = min(group, key=lambda item: item.average_time_seconds)
fastest_counts[kind][fastest.algorithm] += 1
scenario_totals[kind] += 1
if group[0].recommended_algorithm == fastest.algorithm:
recommendation_hits[kind] += 1
return BenchmarkSummary(
fastest_counts=fastest_counts,
recommendation_hits=recommendation_hits,
scenario_totals=scenario_totals,
)