-
Notifications
You must be signed in to change notification settings - Fork 9
/
evalConfig.py
432 lines (371 loc) · 13 KB
/
evalConfig.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import os
import pathlib
import typing
import yaml
from collections.abc import Mapping
from collections.abc import Iterable
from dataclasses import dataclass
from typing import Any
from typing import Optional
from ktoolbox import common
from ktoolbox import kyaml
from ktoolbox.common import StructParseBase
from ktoolbox.common import StructParseParseContext
from ktoolbox.common import strict_dataclass
import testType
import tftbase
from tftbase import Bitrate
from tftbase import TestCaseType
from tftbase import TestType
@strict_dataclass
@dataclass(frozen=True, kw_only=True)
class EvalIdentity:
test_type: TestType
test_case_id: TestCaseType
is_reverse: bool
def clone(
self,
test_type: Optional[TestType] = None,
test_case_id: Optional[TestCaseType] = None,
is_reverse: Optional[bool] = None,
) -> "EvalIdentity":
if test_type is None:
test_type = self.test_type
if test_case_id is None:
test_case_id = self.test_case_id
if is_reverse is None:
is_reverse = self.is_reverse
return EvalIdentity(
test_type=test_type,
test_case_id=test_case_id,
is_reverse=is_reverse,
)
@staticmethod
def from_metadata(tft_metadata: tftbase.TestMetadata) -> "EvalIdentity":
return EvalIdentity(
test_type=tft_metadata.test_type,
test_case_id=tft_metadata.test_case_id,
is_reverse=tft_metadata.reverse,
)
def both_directions(self) -> tuple["EvalIdentity", "EvalIdentity"]:
reverse = self.clone(is_reverse=not self.is_reverse)
if self.is_reverse:
return reverse, self
else:
return self, reverse
@property
def pretty_str(self) -> str:
return f"[{self.test_type.name}, {self.test_case_id.name}, {'R' if self.is_reverse else 'N'}]"
@strict_dataclass
@dataclass(frozen=True, kw_only=True)
class TestItem(StructParseBase):
threshold_rx: Optional[float]
threshold_tx: Optional[float]
def _post_check(self) -> None:
object.__setattr__(
self,
"_bitrate",
Bitrate(rx=self.threshold_rx, tx=self.threshold_tx),
)
@property
def has_thresholds(self) -> bool:
return self.threshold_rx is not None or self.threshold_tx is not None
@property
def bitrate(self) -> Bitrate:
bitrate: Bitrate = getattr(self, "_bitrate")
return bitrate
def get_threshold(
self,
*,
rx: Optional[bool] = None,
tx: Optional[bool] = None,
) -> Optional[float]:
rx, tx = tftbase.eval_binary_opt_in(rx, tx)
if rx and tx:
# Either rx/tx is requested. We return the maximum for both.
if not self.has_thresholds:
return None
return max(
v for v in (self.threshold_rx, self.threshold_tx) if v is not None
)
elif rx:
return self.threshold_rx
elif tx:
return self.threshold_tx
else:
return None
@staticmethod
def parse(pctx: StructParseParseContext) -> "TestItem":
if pctx.arg is None:
return TestItem(
yamlidx=pctx.yamlidx,
yamlpath=pctx.yamlpath,
threshold_rx=None,
threshold_tx=None,
)
with pctx.with_strdict() as varg:
threshold_rx = common.structparse_pop_float(
varg.for_key("threshold_rx"),
default=None,
)
threshold_tx = common.structparse_pop_float(
varg.for_key("threshold_tx"),
default=None,
)
threshold = common.structparse_pop_float(
varg.for_key("threshold"),
default=None,
)
if threshold_rx is None and threshold_tx is None:
if threshold is not None:
threshold_rx = threshold
threshold_tx = threshold
else:
if threshold is not None:
raise pctx.value_error(
f"Cannot set together with '{'threshold_rx' if threshold_rx is not None else 'threshold_tx'}'",
key="threshold",
)
return TestItem(
yamlidx=pctx.yamlidx,
yamlpath=pctx.yamlpath,
threshold_rx=threshold_rx,
threshold_tx=threshold_tx,
)
def serialize(self) -> dict[str, Any]:
extra: dict[str, Any] = {}
if self.has_thresholds:
def _normalize(x: Optional[float]) -> Optional[int | float]:
if x is None:
return None
if x == int(x):
return int(x)
return x
if self.threshold_rx == self.threshold_tx:
common.dict_add_optional(
extra, "threshold", _normalize(self.threshold_rx)
)
else:
common.dict_add_optional(
extra, "threshold_rx", _normalize(self.threshold_rx)
)
common.dict_add_optional(
extra, "threshold_tx", _normalize(self.threshold_tx)
)
return extra
@strict_dataclass
@dataclass(frozen=True, kw_only=True)
class TestCaseData(StructParseBase):
test_case_type: TestCaseType
normal: TestItem
reverse: TestItem
def get_item(
self,
*,
is_reverse: bool,
) -> TestItem:
if is_reverse:
return self.reverse
return self.normal
@staticmethod
def parse(pctx: StructParseParseContext) -> "TestCaseData":
with pctx.with_strdict() as varg:
test_case_type = common.structparse_pop_enum(
varg.for_key("id"),
enum_type=TestCaseType,
)
normal = common.structparse_pop_obj(
varg.for_key("Normal"),
construct=TestItem.parse,
construct_default=True,
)
reverse = common.structparse_pop_obj(
varg.for_key("Reverse"),
construct=TestItem.parse,
construct_default=True,
)
return TestCaseData(
yamlidx=pctx.yamlidx,
yamlpath=pctx.yamlpath,
test_case_type=test_case_type,
normal=normal,
reverse=reverse,
)
def serialize(self) -> dict[str, Any]:
extra: dict[str, Any] = {}
if self.normal.has_thresholds:
common.dict_add_optional(extra, "Normal", self.normal.serialize())
if self.reverse.has_thresholds:
common.dict_add_optional(extra, "Reverse", self.reverse.serialize())
return {
"id": self.test_case_type.name,
**extra,
}
@strict_dataclass
@dataclass(frozen=True, kw_only=True)
class TestTypeData(StructParseBase):
test_type: TestType
test_cases: Mapping[TestCaseType, TestCaseData]
test_type_handler: testType.TestTypeHandler
@staticmethod
def parse(yamlidx: int, yamlpath_base: str, key: Any, arg: Any) -> "TestTypeData":
try:
test_type = common.enum_convert(TestType, key)
except Exception:
raise ValueError(
f'"{yamlpath_base}.[{yamlidx}]": expects a test type as key like iperf-tcp but got {key}'
)
yamlpath = f"{yamlpath_base}.{test_type.name}"
try:
test_type_handler = testType.TestTypeHandler.get(test_type)
except Exception:
raise ValueError(
f'"{yamlpath}": test type "{test_type}" has no handler implementation'
)
if not isinstance(arg, list):
raise ValueError(f'"{yamlpath}": expects a list of test cases')
test_cases = {}
for pctx2 in StructParseParseContext.enumerate_list(yamlpath, arg):
c = TestCaseData.parse(pctx2)
if c.test_case_type in test_cases:
raise ValueError(
f'"{pctx2.yamlpath}": duplicate key {c.test_case_type.name}'
)
test_cases[c.test_case_type] = c
return TestTypeData(
yamlidx=yamlidx,
yamlpath=yamlpath,
test_type=test_type,
test_type_handler=test_type_handler,
test_cases=test_cases,
)
def serialize(self) -> list[Any]:
return [v.serialize() for k, v in self.test_cases.items()]
@strict_dataclass
@dataclass(frozen=True, kw_only=True)
class Config(StructParseBase):
configs: Mapping[TestType, TestTypeData]
config_path: Optional[str]
configdir: str
cwddir: str
@staticmethod
def parse(
arg: Any,
*,
config_path: Optional[str] = None,
cwddir: str = ".",
) -> "Config":
if not config_path:
config_path = None
cwddir = common.path_norm(cwddir, make_absolute=True)
config_path = common.path_norm(config_path, cwd=cwddir)
if config_path is not None:
configdir = os.path.dirname(config_path)
else:
configdir = cwddir
yamlpath = ""
if arg is None:
# An empty file is valid too. That shows up here as None.
vdict = {}
else:
vdict = common.structparse_check_strdict(arg, yamlpath)
configs = {}
for yamlidx2, key in enumerate(vdict):
c = TestTypeData.parse(
yamlidx=yamlidx2,
yamlpath_base=yamlpath,
key=key,
arg=vdict[key],
)
if c.test_type in configs:
raise ValueError(
f'"{yamlpath}.[{yamlidx2}]": duplicate key {c.test_type.name}'
)
configs[c.test_type] = c
return Config(
yamlidx=0,
yamlpath=yamlpath,
configs=configs,
config_path=config_path,
configdir=configdir,
cwddir=cwddir,
)
@staticmethod
def parse_from_file(
config_path: Optional[str | pathlib.Path] = None,
*,
cwddir: str = ".",
) -> "Config":
yamldata: Any = None
errmsg_detail = ""
if not config_path:
config_path = None
cwddir = common.path_norm(cwddir, make_absolute=True)
config_path = common.path_norm(config_path, cwd=cwddir)
if config_path is not None:
config_path = str(config_path)
errmsg_detail = f" {repr(config_path)}"
try:
with open(config_path) as file:
yamldata = yaml.safe_load(file)
except Exception as e:
raise RuntimeError(f"Failure reading{errmsg_detail}: {e}")
try:
return Config.parse(yamldata, config_path=config_path, cwddir=cwddir)
except ValueError as e:
raise ValueError(f"Failure parsing{errmsg_detail}: {e}")
except Exception as e:
raise RuntimeError(f"Failure loading{errmsg_detail}: {e}")
def serialize(self) -> dict[str, Any]:
return {k.name: v.serialize() for k, v in self.configs.items()}
def serialize_to_file(
self,
filename: str | pathlib.Path | typing.IO[str],
) -> None:
kyaml.dump(
self.serialize(),
filename,
)
@common.iter_dictify
def get_items(self) -> Iterable[tuple[EvalIdentity, TestItem]]:
for test_type, test_type_data in self.configs.items():
for test_case_id, test_case_data in test_type_data.test_cases.items():
item = test_case_data.get_item(is_reverse=False)
if item is not None:
ei = EvalIdentity(
test_type=test_type,
test_case_id=test_case_id,
is_reverse=False,
)
yield ei, item
item = test_case_data.get_item(is_reverse=True)
if item is not None:
ei = EvalIdentity(
test_type=test_type,
test_case_id=test_case_id,
is_reverse=True,
)
yield ei, item
def get_item(
self,
*,
test_type: TestType,
test_case_id: TestCaseType,
is_reverse: bool,
) -> Optional[TestItem]:
test_type_data = self.configs.get(test_type)
if test_type_data is None:
return None
test_case_data = test_type_data.test_cases.get(test_case_id)
if test_case_data is None:
return None
return test_case_data.get_item(is_reverse=is_reverse)
def get_item_for_id(
self,
ei: EvalIdentity,
) -> Optional[TestItem]:
return self.get_item(
test_type=ei.test_type,
test_case_id=ei.test_case_id,
is_reverse=ei.is_reverse,
)