-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplot_benchmarks.py
More file actions
180 lines (150 loc) · 5.11 KB
/
plot_benchmarks.py
File metadata and controls
180 lines (150 loc) · 5.11 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
import os
import matplotlib.pyplot as plt
import polars as pl
import seaborn as sns
# Open the plots:
# ```shell
# open analysis/plots
# ```
def main():
# Create a directory to save the plots
output_dir = "analysis/plots"
os.makedirs(output_dir, exist_ok=True)
# Define the schema for the data
schema = {
"map_name": pl.Utf8,
"hash_name": pl.Utf8,
"benchmark": pl.Utf8,
"test_num": pl.Utf8,
"test_name": pl.Utf8,
"num_operations": pl.Utf8, # Read as string to handle errors
"nanoseconds_per_op": pl.Utf8, # Read as string to handle errors
"memory_usage_mb": pl.Utf8, # Read as string to handle errors
}
# Read the data file line by line to handle errors
with open("data/all_new.txt", "r") as f:
lines = f.readlines()
# Filter out error lines
lines = [line for line in lines if "ERROR" not in line and "TIMEOUT" not in line]
# Create a new cleaned data file
cleaned_data_path = "data/all_new_cleaned.txt"
with open(cleaned_data_path, "w") as f:
f.writelines(lines)
# Read and clean the data
try:
df = pl.read_csv(
cleaned_data_path,
separator=";",
has_header=False,
new_columns=list(schema.keys()),
schema_overrides=schema,
ignore_errors=True,
)
except Exception as e:
print(f"Error reading the data file: {e}")
return
# Convert columns to numeric, coercing errors to null
df = df.with_columns(
[
pl.col("num_operations").str.strip_chars().cast(pl.Int64, strict=False),
pl.col("nanoseconds_per_op")
.str.strip_chars()
.cast(pl.Float64, strict=False),
pl.col("memory_usage_mb").str.strip_chars().cast(pl.Float64, strict=False),
]
)
# Clean up map and hash names by removing quotes and stripping whitespace
df = df.with_columns(
[
pl.col("map_name")
.str.replace_all('"', "")
.str.strip_chars()
.alias("map_name"),
pl.col("hash_name")
.str.replace_all('"', "")
.str.strip_chars()
.alias("hash_name"),
pl.col("benchmark")
.str.replace_all('"', "")
.str.strip_chars()
.alias("benchmark"),
]
)
# Create a combined column for easier plotting
df = df.with_columns(
(pl.col("map_name") + " (" + pl.col("hash_name") + ")").alias("map_with_hash")
)
benchmarks = [
"Copy",
"CtorDtorEmptyMap",
"CtorDtorSingleEntryMap",
"InsertHugeInt",
"IterateIntegers",
"RandomDistinct2",
"RandomFind_200",
"RandomFind_2000",
"RandomFind_500000",
"RandomFindString",
"RandomFindString_1000000",
"RandomInsertErase",
"RandomInsertEraseStrings",
]
for bench in benchmarks:
print(f"Generating plot for {bench}...")
bench_df = df.filter(pl.col("benchmark") == bench)
if bench_df.height == 0:
print(f"No data found for benchmark: {bench}")
continue
plt.figure(figsize=(12, 8))
# --- Performance Plot ---
plt.figure(figsize=(12, 8))
agg_df_perf = (
bench_df.group_by("map_with_hash")
.agg(pl.mean("nanoseconds_per_op").alias("mean_nanoseconds_per_op"))
.sort("mean_nanoseconds_per_op")
)
sns.barplot(
x="mean_nanoseconds_per_op",
y="map_with_hash",
data=agg_df_perf.to_pandas(),
palette="viridis",
)
plt.title(f"Performance Comparison for {bench}")
plt.xlabel("Mean Nanoseconds per Operation (lower is better)")
plt.ylabel("Map Type and Hash Function")
plt.tight_layout()
ax = plt.gca()
for label in ax.get_yticklabels():
if "optimap::HashMap" in label.get_text():
label.set_weight("bold")
plot_path = os.path.join(output_dir, f"{bench}_performance.png")
plt.savefig(plot_path)
plt.close()
print(f"Saved plot to {plot_path}")
# --- Memory Usage Plot ---
plt.figure(figsize=(12, 8))
agg_df_mem = (
bench_df.group_by("map_with_hash")
.agg(pl.mean("memory_usage_mb").alias("mean_memory_usage_mb"))
.sort("mean_memory_usage_mb")
)
sns.barplot(
x="mean_memory_usage_mb",
y="map_with_hash",
data=agg_df_mem.to_pandas(),
palette="plasma",
)
plt.title(f"Memory Usage for {bench}")
plt.xlabel("Mean Memory Usage (MB)")
plt.ylabel("Map Type and Hash Function")
plt.tight_layout()
ax = plt.gca()
for label in ax.get_yticklabels():
if "optimap::HashMap" in label.get_text():
label.set_weight("bold")
plot_path = os.path.join(output_dir, f"{bench}_memory.png")
plt.savefig(plot_path)
plt.close()
print(f"Saved plot to {plot_path}")
if __name__ == "__main__":
main()