-
Notifications
You must be signed in to change notification settings - Fork 149
Expand file tree
/
Copy pathvisualize_benchmarks.py
More file actions
360 lines (286 loc) · 14 KB
/
visualize_benchmarks.py
File metadata and controls
360 lines (286 loc) · 14 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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#!/usr/bin/env python3
# Copyright DataStax, Inc.
#
# Licensed 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.
import argparse
import csv
import os
import sys
import re
import platform
import psutil
from collections import defaultdict
from typing import Dict, List, Any, Tuple, Optional
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import MaxNLocator
# Define metrics where higher values are better and lower values are better
HIGHER_IS_BETTER = ["QPS", "Recall@10"]
LOWER_IS_BETTER = ["Mean Latency", "Index Build Time", "Average Nodes Visited"]
class BenchmarkData:
def __init__(self):
# Structure: {release: {dataset: {metric: value}}}
self.data = defaultdict(lambda: defaultdict(dict))
self.releases = [] # This will maintain the order of releases as they are added
self.datasets = set()
self.metrics = set()
def add_file(self, file_path: str, release: str):
"""
Add benchmark data from a CSV file for a specific release
"""
if release not in self.releases:
self.releases.append(release)
try:
with open(file_path, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
dataset = row['dataset']
self.datasets.add(dataset)
for metric, value in row.items():
if metric != 'dataset':
try:
# Convert to float if possible
value = float(value)
self.metrics.add(metric)
self.data[release][dataset][metric] = value
except ValueError:
# Skip non-numeric values
pass
except Exception as e:
print(f"Error loading benchmark results from {file_path}: {e}")
return False
return True
def get_metric_data_for_dataset(self, metric: str, dataset: str) -> Tuple[List[str], List[float]]:
"""
Get the values of a specific metric for a dataset across all releases
"""
releases = []
values = []
for release in self.releases:
if dataset in self.data[release] and metric in self.data[release][dataset]:
releases.append(release)
values.append(self.data[release][dataset][metric])
return releases, values
def detect_changes(self, threshold_percent: float = 5.0) -> List[Dict[str, Any]]:
"""
Detect significant changes in metrics between consecutive releases
"""
changes = []
for dataset in self.datasets:
for metric in self.metrics:
releases, values = self.get_metric_data_for_dataset(metric, dataset)
for i in range(1, len(releases)):
current_value = values[i]
previous_value = values[i-1]
current_release = releases[i]
previous_release = releases[i-1]
if previous_value == 0:
continue # Avoid division by zero
change_pct = ((current_value - previous_value) / previous_value) * 100
# Determine if this is an improvement or regression
if metric in HIGHER_IS_BETTER:
status = "improvement" if change_pct > 0 else "regression"
elif metric in LOWER_IS_BETTER:
status = "improvement" if change_pct < 0 else "regression"
else:
status = "unknown"
# Only include significant changes
if abs(change_pct) >= threshold_percent:
changes.append({
"dataset": dataset,
"metric": metric,
"current_release": current_release,
"previous_release": previous_release,
"current_value": current_value,
"previous_value": previous_value,
"change_pct": change_pct,
"status": status
})
return changes
def extract_release_from_filename(filename: str) -> str:
"""
Extract release number from filename
"""
# Try to find a version pattern like v1.2.3 or 1.2.3
match = re.search(r'(?:v|release[-_])?(\d+\.\d+(?:\.\d+)?)', filename)
if match:
return match.group(1)
# If no version pattern, use the filename without extension
base = os.path.basename(filename)
return os.path.splitext(base)[0]
def generate_plots(benchmark_data: BenchmarkData, output_dir: str):
"""
Generate plots for each metric, with one line per dataset
"""
os.makedirs(output_dir, exist_ok=True)
# Create a plot for each metric
for metric in benchmark_data.metrics:
plt.figure(figsize=(10, 6))
# Plot one line per dataset
for dataset in benchmark_data.datasets:
releases, values = benchmark_data.get_metric_data_for_dataset(metric, dataset)
if releases and values:
# For QPS, try to add error bars using corresponding stddev column
if metric == "QPS":
std_releases, std_values = benchmark_data.get_metric_data_for_dataset("QPS StdDev", dataset)
if std_releases and std_values:
# Align stddev values to the QPS releases order
std_map = {r: v for r, v in zip(std_releases, std_values)}
yerr = [std_map.get(r, 0.0) for r in releases]
plt.errorbar(releases, values, yerr=yerr, marker='o', capsize=4, label=dataset)
else:
plt.plot(releases, values, marker='o', label=dataset)
else:
plt.plot(releases, values, marker='o', label=dataset)
plt.title(f"{metric} Over Time")
plt.xlabel("Release")
plt.ylabel(metric)
plt.xticks(rotation=45)
plt.grid(True, linestyle='--', alpha=0.7)
plt.legend()
plt.tight_layout()
# Save the plot
safe_metric_name = metric.replace('@', '_at_').replace(' ', '_')
plt.savefig(os.path.join(output_dir, f"{safe_metric_name}.png"))
plt.close()
# Create a combined plot with aggregated values for comparison
plt.figure(figsize=(12, 8))
# Get system information
import platform
import psutil
# Get CPU information
cpu_count = psutil.cpu_count(logical=False)
if cpu_count is None:
cpu_count = psutil.cpu_count(logical=True)
# Get memory information
memory_gb = round(psutil.virtual_memory().total / (1024**3), 2)
# Get processor information
processor = platform.processor()
if not processor:
processor = platform.machine()
system_info = f"CPU: {processor}, Cores: {cpu_count}, Memory: {memory_gb} GB"
for metric in benchmark_data.metrics:
plt.subplot(len(benchmark_data.metrics), 1, list(benchmark_data.metrics).index(metric) + 1)
for dataset in benchmark_data.datasets:
releases, values = benchmark_data.get_metric_data_for_dataset(metric, dataset)
if releases and values:
# Aggregate values to the first release
if values[0] != 0:
aggregated_values = [v / values[0] for v in values]
plt.plot(releases, aggregated_values, marker='o', label=f"{dataset}")
plt.title(f"Aggregated {metric}")
plt.grid(True, linestyle='--', alpha=0.7)
if metric == list(benchmark_data.metrics)[-1]:
plt.xlabel("Release")
plt.ylabel("Relative Change")
plt.xticks(rotation=45)
# Add system information as text annotation at the bottom of the plot
if metric == list(benchmark_data.metrics)[-1]:
plt.figtext(0.5, 0.01, system_info, ha='center', fontsize=8,
bbox={'facecolor': 'white', 'alpha': 0.8, 'pad': 5})
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout(rect=[0, 0.03, 1, 1]) # Adjust layout to make room for the annotation
plt.savefig(os.path.join(output_dir, "aggregated_comparison.png"))
plt.close()
def generate_report(benchmark_data: BenchmarkData, changes: List[Dict[str, Any]], output_file: str):
"""
Generate a markdown report summarizing the benchmark results and changes
"""
with open(output_file, 'w') as f:
f.write("# Benchmark Performance Report\n\n")
# Summary of datasets and metrics
f.write("## Summary\n\n")
f.write(f"- Total releases analyzed: {len(benchmark_data.releases)}\n")
f.write(f"- Datasets: {', '.join(benchmark_data.datasets)}\n")
f.write(f"- Metrics: {', '.join(benchmark_data.metrics)}\n\n")
# Significant changes
improvements = [c for c in changes if c['status'] == 'improvement']
regressions = [c for c in changes if c['status'] == 'regression']
f.write("## Significant Changes\n\n")
f.write(f"- Total improvements: {len(improvements)}\n")
f.write(f"- Total regressions: {len(regressions)}\n\n")
# List improvements
if improvements:
f.write("### Improvements\n\n")
f.write("| Dataset | Metric | Releases | Previous | Current | Change |\n")
f.write("|---------|--------|----------|----------|---------|-------|\n")
for change in improvements:
f.write(f"| {change['dataset']} | {change['metric']} | " +
f"{change['previous_release']} → {change['current_release']} | " +
f"{change['previous_value']:.4f} | {change['current_value']:.4f} | " +
f"{change['change_pct']:+.2f}% |\n")
f.write("\n")
# List regressions
if regressions:
f.write("### Regressions\n\n")
f.write("| Dataset | Metric | Releases | Previous | Current | Change |\n")
f.write("|---------|--------|----------|----------|---------|-------|\n")
for change in regressions:
f.write(f"| {change['dataset']} | {change['metric']} | " +
f"{change['previous_release']} → {change['current_release']} | " +
f"{change['previous_value']:.4f} | {change['current_value']:.4f} | " +
f"{change['change_pct']:+.2f}% |\n")
f.write("\n")
# Latest results
f.write("## Latest Results\n\n")
latest_release = benchmark_data.releases[-1] if benchmark_data.releases else "N/A"
f.write(f"### Release: {latest_release}\n\n")
f.write("| Dataset | QPS | Mean Latency | Recall@10 |\n")
f.write("|---------|-----|-------------|----------|\n")
for dataset in benchmark_data.datasets:
if latest_release in benchmark_data.data and dataset in benchmark_data.data[latest_release]:
qps = benchmark_data.data[latest_release][dataset].get("QPS", "N/A")
latency = benchmark_data.data[latest_release][dataset].get("Mean Latency", "N/A")
recall = benchmark_data.data[latest_release][dataset].get("Recall@10", "N/A")
if isinstance(qps, float):
qps = f"{qps:.2f}"
if isinstance(latency, float):
latency = f"{latency:.4f}"
if isinstance(recall, float):
recall = f"{recall:.4f}"
f.write(f"| {dataset} | {qps} | {latency} | {recall} |\n")
f.write("\n")
# Include links to the plots
f.write("## Performance Graphs\n\n")
for metric in benchmark_data.metrics:
safe_metric_name = metric.replace('@', '_at_').replace(' ', '_')
f.write(f"\n\n")
f.write("\n")
def main():
parser = argparse.ArgumentParser(description="Compare and visualize benchmark results across multiple releases")
parser.add_argument("files", nargs="+", help="Paths to CSV benchmark result files")
parser.add_argument("--output-dir", default="benchmark_reports", help="Directory to save plots and reports")
parser.add_argument("--threshold", type=float, default=5.0,
help="Threshold percentage for detecting significant changes (default: 5.0)")
args = parser.parse_args()
# Create output directory
os.makedirs(args.output_dir, exist_ok=True)
# Load benchmark data
benchmark_data = BenchmarkData()
for file_path in args.files:
release = extract_release_from_filename(file_path)
print(f"Loading {file_path} as release {release}...")
benchmark_data.add_file(file_path, release)
# We no longer sort the releases - we keep them in the order they were provided
# This comment replaces the previous sorting code
# Detect significant changes
changes = benchmark_data.detect_changes(args.threshold)
# Generate plots
generate_plots(benchmark_data, args.output_dir)
# Generate report
report_path = os.path.join(args.output_dir, "benchmark_report.md")
generate_report(benchmark_data, changes, report_path)
print(f"Benchmark analysis complete. Report saved to {report_path}")
print(f"Plots saved to {args.output_dir}")
if __name__ == "__main__":
main()