-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathplot-stats
More file actions
executable file
·247 lines (202 loc) · 6.87 KB
/
Copy pathplot-stats
File metadata and controls
executable file
·247 lines (202 loc) · 6.87 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
#!/usr/bin/env python3
# SPDX-FileCopyrightText: Contributors to the vmnet-helper project
# SPDX-License-Identifier: Apache-2.0
"""
Generate benchmark comparison plots from vmnet-helper log files.
Usage:
plot-stats [--title TITLE] [-o output-prefix] before.log after.log
Output prefix defaults to the before log basename without extension.
Generates two plots:
{output-prefix}-throughput.png - bitrate and drops over time
{output-prefix}-efficiency.png - fast and slow calls/sec over time
Example:
% ./plot-stats --title "krunkit: iperf3 -c vm.local -P 4 -b 7G" \
krunkit-before/tx-p4-b7.log krunkit-after/tx-p4-b7.log
Saved: tx-p4-b7-throughput.png
Saved: tx-p4-b7-efficiency.png
"""
import argparse
import os
import sys
from datetime import datetime
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import testing.stats
def main():
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--title", help="Plot title (default: output prefix)")
parser.add_argument(
"-o",
"--output",
help="Output file prefix (default: before log basename without extension)",
)
parser.add_argument(
"--threshold",
type=float,
default=1.0,
help="Steady state threshold in Gbps (default: 1.0)",
)
parser.add_argument("before", help="Before log file")
parser.add_argument("after", help="After log file")
args = parser.parse_args()
output_prefix = args.output or os.path.splitext(os.path.basename(args.before))[0]
title = args.title or output_prefix
before = steady_state(compute_rates(args.before), args.threshold)
after = steady_state(compute_rates(args.after), args.threshold)
if not before:
print(f"No steady state data in {args.before}", file=sys.stderr)
sys.exit(1)
if not after:
print(f"No steady state data in {args.after}", file=sys.stderr)
sys.exit(1)
plot_throughput(before, after, title, f"{output_prefix}-throughput.png")
plot_efficiency(before, after, title, f"{output_prefix}-efficiency.png")
def compute_rates(logfile):
"""Compute per-second rates from a log file."""
records = []
prev = None
for curr in testing.stats.parse(logfile):
if prev is not None:
t0 = datetime.fromisoformat(prev["time"])
t1 = datetime.fromisoformat(curr["time"])
duration = (t1 - t0).total_seconds()
if duration <= 0:
prev = curr
continue
delta = testing.stats.compute_delta(prev, curr)
record = {}
for ep in ("host", "vm"):
d = delta[ep]
record[ep] = {
"bitrate_gbps": d["bytes"] * 8 / duration / 1e9,
"packets": int(d["packets"] / duration),
"drops": d["drops"],
"fast": int(d["fast"] / duration),
"slow": int(d["slow"] / duration),
}
records.append(record)
prev = curr
return records
def steady_state(records, threshold=5.0):
return [r for r in records if r["host"]["bitrate_gbps"] > threshold]
def plot_throughput(before, after, title, output_path):
n = min(len(before), len(after))
before = before[:n]
after = after[:n]
t = list(range(1, n + 1))
before_bitrate = [r["host"]["bitrate_gbps"] for r in before]
after_bitrate = [r["host"]["bitrate_gbps"] for r in after]
before_drops = [r["host"]["drops"] for r in before]
after_drops = [r["host"]["drops"] for r in after]
fig, ax1 = plt.subplots(figsize=(12, 5))
ax1.set_xlabel("Time (seconds)", fontsize=12)
ax1.set_ylabel("Host\u2192VM Bitrate (Gbits/sec)", fontsize=12)
ax1.plot(
t,
before_bitrate,
color="#2196F3",
linewidth=1.5,
label="Before - bitrate",
alpha=0.9,
)
ax1.plot(
t,
after_bitrate,
color="#FF9800",
linewidth=1.5,
label="After - bitrate",
alpha=0.9,
)
max_br = max(max(before_bitrate), max(after_bitrate))
ax1.set_ylim(0, max_br * 1.1)
ax2 = ax1.twinx()
ax2.set_ylabel("Host\u2192VM Drops", fontsize=12)
ax2.bar(
[x - 0.15 for x in t],
before_drops,
width=0.3,
color="#90CAF9",
alpha=0.7,
label="Before - drops",
)
ax2.bar(
[x + 0.15 for x in t],
after_drops,
width=0.3,
color="#FFCC80",
alpha=0.7,
label="After - drops",
)
max_drops = max(max(before_drops), max(after_drops))
ax2.set_ylim(0, max(max_drops * 1.5, 1))
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc="lower left", fontsize=10)
ax1.set_title(title + " (throughput)", fontsize=14, fontweight="bold")
ax1.grid(True, alpha=0.3)
fig.tight_layout()
fig.savefig(output_path, dpi=150)
plt.close(fig)
print(f"Saved: {output_path}")
def plot_efficiency(before, after, title, output_path):
n = min(len(before), len(after))
before = before[:n]
after = after[:n]
t = list(range(1, n + 1))
before_fast = [r["host"]["fast"] for r in before]
after_fast = [r["host"]["fast"] for r in after]
before_slow = [r["host"]["slow"] for r in before]
after_slow = [r["host"]["slow"] for r in after]
fig, ax1 = plt.subplots(figsize=(12, 5))
ax1.set_xlabel("Time (seconds)", fontsize=12)
ax1.set_ylabel("Fast calls/sec", fontsize=12)
ax1.plot(
t,
before_fast,
color="#2196F3",
linewidth=1.5,
label="Before - fast calls",
alpha=0.9,
)
ax1.plot(
t,
after_fast,
color="#FF9800",
linewidth=1.5,
label="After - fast calls",
alpha=0.9,
)
max_val = max(max(before_fast), max(after_fast))
ax1.set_ylim(0, max_val * 1.1)
ax2 = ax1.twinx()
ax2.set_ylabel("Slow calls/sec", fontsize=12)
ax2.bar(
[x - 0.15 for x in t],
before_slow,
width=0.3,
color="#90CAF9",
alpha=0.7,
label="Before - slow calls",
)
ax2.bar(
[x + 0.15 for x in t],
after_slow,
width=0.3,
color="#FFCC80",
alpha=0.7,
label="After - slow calls",
)
max_slow = max(max(before_slow), max(after_slow))
ax2.set_ylim(0, max(max_slow * 1.5, 1))
lines1, labels1 = ax1.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()
ax1.legend(lines1 + lines2, labels1 + labels2, loc="upper left", fontsize=10)
ax1.set_title(title + " (efficiency)", fontsize=14, fontweight="bold")
ax1.grid(True, alpha=0.3)
fig.tight_layout()
fig.savefig(output_path, dpi=150)
plt.close(fig)
print(f"Saved: {output_path}")
if __name__ == "__main__":
main()