-
Notifications
You must be signed in to change notification settings - Fork 9
/
generate_eval_config.py
executable file
·361 lines (290 loc) · 10.2 KB
/
generate_eval_config.py
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
361
#!/usr/bin/env python3
import argparse
import logging
import os
import sys
from collections.abc import Iterable
from typing import Any
from typing import Optional
from ktoolbox import common
import tftbase
from evalConfig import Config
from evalConfig import EvalIdentity
from tftbase import Bitrate
from tftbase import TftResults
logger = logging.getLogger("tft." + __name__)
def load_config(config: Optional[str]) -> Optional[Config]:
if not config:
return None
return Config.parse_from_file(config)
@common.iter_tuplify
def load_logs(
logs: Iterable[str],
*,
skip_invalid_logs: bool = False,
) -> Iterable[TftResults]:
for log in list(logs):
try:
tft_results = TftResults.parse_from_file(log)
except Exception as e:
if not skip_invalid_logs:
raise
# Failures are not fatal here. That is because the output format is
# not stable, so if we change the format, we may be unable to parse
# certain older logs. Skip.
logger.warning(f"Skip invalid file {repr(log)}: {e}")
continue
yield tft_results
def collect_all_bitrates(
config: Optional[Config],
all_tft_results: Iterable[TftResults],
) -> dict[EvalIdentity, list[Bitrate]]:
result: dict[EvalIdentity, list[Bitrate]]
if config is not None:
result = {ei: [] for ei in config.get_items()}
else:
result = {}
for tft_results in all_tft_results:
for tft_result in tft_results:
if not tft_result.eval_all_success:
# This result is not valid. We don't consider it for
# calculating the new thresholds.
#
# Note that this also includes eval_success fields, that is the
# result of a previous evaluation (with another eval-config).
# If you don't want that, run first ./evaluator.py on the
# input file with an empty eval-config, to clean out all
# previous evaluations.
continue
flow_test = tft_result.flow_test
ei = EvalIdentity.from_metadata(flow_test.tft_metadata)
lst = result.get(ei)
if lst is None:
if config is not None:
# we only collect the items that we have in config too. Don't create a new one.
continue
lst = []
result[ei] = lst
lst.append(flow_test.bitrate_gbps)
return result
def calc_mean_stddev(data: list[float]) -> tuple[float, float]:
mean = sum(data) / len(data)
variance = sum((x - mean) ** 2 for x in data) / (len(data))
stddev: float = variance**0.5
return mean, stddev
def accumulate_rate(
rate: Iterable[Optional[float]],
*,
quorum: int,
) -> Optional[float]:
data = [x for x in rate if x is not None]
if not data or len(data) < quorum:
return None
mean, stddev = calc_mean_stddev(data)
# Filter out outliers outside 3 stddev.
data2 = [x for x in data if x > mean - 3 * stddev and x < mean + 3 * stddev]
if not data2 or len(data2) < quorum:
return None
mean, stddev = calc_mean_stddev(data2)
return max(
mean - 2.0 * stddev,
mean * 0.8,
)
def accumulate_bitrates(
bitrates: list[Bitrate],
*,
quorum: int,
) -> Bitrate:
rx = accumulate_rate((bitrate.rx for bitrate in bitrates), quorum=quorum)
tx = accumulate_rate((bitrate.tx for bitrate in bitrates), quorum=quorum)
return Bitrate(rx=rx, tx=tx)
def _tighten_rate(
a: Optional[float], *, base: Optional[float], tighten_only: bool
) -> Optional[float]:
if base is None:
return None
if a is None:
return base
if tighten_only:
return max(a, base)
return a
@common.iter_dictify
def accumulate_all_bitrates(
config: Optional[Config],
all_bitrates: dict[EvalIdentity, list[Bitrate]],
*,
tighten_only: bool,
quorum: int,
) -> Iterable[tuple[EvalIdentity, Bitrate]]:
if config is not None:
assert list(all_bitrates) == list(config.get_items())
for ei, bitrates in all_bitrates.items():
bitrate = accumulate_bitrates(bitrates, quorum=quorum)
if config is not None:
item = config.get_item_for_id(ei)
assert item is not None
bitrate2 = item.bitrate
rx = _tighten_rate(bitrate.rx, base=bitrate2.rx, tighten_only=tighten_only)
tx = _tighten_rate(bitrate.tx, base=bitrate2.tx, tighten_only=tighten_only)
bitrate = Bitrate(rx=rx, tx=tx)
yield ei, bitrate
def parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Tool to generate eval-config.yaml TFT Flow test results"
)
parser.add_argument(
"logs",
nargs="*",
help="Result file(s) from a traffic flow test run.",
)
parser.add_argument(
"-o",
"--output",
default=None,
help="Output file to write new eval-config.yaml to.",
)
parser.add_argument(
"-S",
"--skip-invalid-logs",
action="store_true",
help='If set any invalid "--logs" files are ignored. This is useful because the output format is not stable, so your last logs might have been generated with an incompatible version and we want to skip those errors.',
)
parser.add_argument(
"-f",
"--force",
action="store_true",
help="For overwriting output file if it exists.",
)
parser.add_argument(
"-Q",
"--quorum",
default=0,
type=int,
help="If specified, require at least that many successful measurements for calculating a new threshold.",
)
parser.add_argument(
"-c",
"--config",
help="The base eval-config. If given, the result will contain all the entries from this input file. Values are updated with the measurementns from the logs.",
)
parser.add_argument(
"-T",
"--tighten-only",
action="store_true",
help="With '--config' the values are only updated if they tighten (increase) the thresholds.",
)
common.log_argparse_add_argument_verbose(parser)
args = parser.parse_args()
common.log_config_logger(args.verbose, "tft", "ktoolbox")
if args.tighten_only and not args.config:
logger.error(
'Option "--tighten-only" requires a "--config" base configuration.'
)
sys.exit(4)
return args
def log_data(
config: Optional[Config],
all_bitrates: dict[EvalIdentity, list[Bitrate]],
new_bitrates: dict[EvalIdentity, Bitrate],
) -> None:
for ei, bitrates in all_bitrates.items():
config_bitrate: Optional[Bitrate] = None
if config is not None:
item = common.unwrap(config.get_item_for_id(ei))
config_bitrate = item.bitrate
new_bitrate = new_bitrates.get(ei)
if config is None:
msg = f"new={Bitrate.get_pretty_str(new_bitrate)}"
else:
msg = f"config={Bitrate.get_pretty_str(config_bitrate)}"
if config_bitrate != new_bitrate:
msg += f" ; new={Bitrate.get_pretty_str(new_bitrate)}"
bitrates_msg = (
"[rx=["
+ ",".join(str(r.rx) for r in bitrates)
+ "],tx=["
+ ",".join(str(r.tx) for r in bitrates)
+ "]]"
)
logger.debug(f"{ei.pretty_str}: {msg} ; bitrates={bitrates_msg}")
def bitrate_to_yaml(bitrate: Bitrate) -> dict[str, Any]:
dd: dict[str, Any] = {}
common.dict_add_optional(dd, "threshold_rx", bitrate.rx)
common.dict_add_optional(dd, "threshold_tx", bitrate.tx)
return dd
def generate_result_config(
config: Optional[Config],
bitrates: dict[EvalIdentity, Bitrate],
) -> Config:
new_config: dict[str, list[dict[str, Any]]] = {}
handled: set[EvalIdentity] = set()
for ei in bitrates:
ei, ei_reverse = ei.both_directions()
if ei in handled:
continue
handled.add(ei)
if config is not None:
assert config.get_item_for_id(ei) or config.get_item_for_id(ei_reverse)
bitrate = bitrates.get(ei, Bitrate.NA)
bitrate_reverse = bitrates.get(ei_reverse, Bitrate.NA)
if config is None and bitrate.is_na and bitrate_reverse.is_na:
continue
lst = new_config.get(ei.test_type.name)
if lst is None:
lst = []
new_config[ei.test_type.name] = lst
list_entry: dict[str, Any] = {
"id": ei.test_case_id.name,
}
if not bitrate.is_na:
list_entry["Normal"] = bitrate_to_yaml(bitrate)
if not bitrate_reverse.is_na:
list_entry["Reverse"] = bitrate_to_yaml(bitrate_reverse)
lst.append(list_entry)
# Normalize the generated dictionary by sorting.
for lst in new_config.values():
lst.sort(key=lambda x: tftbase.TestCaseType[x["id"]].value)
keys = [
tftbase.TestType(n)
for n in sorted(tftbase.TestType[n].value for n in new_config)
]
new_config = {test_type.name: new_config[test_type.name] for test_type in keys}
return Config.parse(new_config)
def write_to_file(
config: Config,
*,
output: Optional[str],
force: bool,
) -> None:
if not output:
config.serialize_to_file(sys.stdout)
return
if not force and os.path.exists(output):
logger.error(
f"The output file {repr(output)} already exists. Run with '--force' to overwrite"
)
sys.exit(55)
config.serialize_to_file(output)
def main() -> None:
args = parse_args()
config = load_config(args.config)
all_tft_results = load_logs(
args.logs,
skip_invalid_logs=args.skip_invalid_logs,
)
all_bitrates = collect_all_bitrates(config, all_tft_results)
new_bitrates = accumulate_all_bitrates(
config,
all_bitrates,
tighten_only=args.tighten_only,
quorum=args.quorum,
)
log_data(config, all_bitrates, new_bitrates)
result_config = generate_result_config(config, new_bitrates)
write_to_file(
result_config,
output=args.output,
force=args.force or args.config == args.output,
)
if __name__ == "__main__":
main()