|
| 1 | +import json |
| 2 | +import sys |
| 3 | +from typing import Any, Optional |
| 4 | + |
| 5 | +WINDOWS = "win32" |
| 6 | +MACOS = "darwin" |
| 7 | +LINUX = "linux" |
| 8 | +NAME = "name" |
| 9 | +FILE_PATH = "file_path" |
| 10 | +KEY_LOCATION = "key_location" |
| 11 | +EXPECTED = "expected" |
| 12 | +ERROR_MARGIN_FRAC = "error_margin_frac" |
| 13 | + |
| 14 | +RUST_BENCHMARKS = { |
| 15 | + WINDOWS: [ |
| 16 | + { |
| 17 | + NAME: "2020-09-04-BayLoop/M8L_BDS_ADR431_nativeODO_Swiftlets_OSR_prod_10Hz_AmotechL2/04-155800", |
| 18 | + FILE_PATH: "target/criterion/proc_messages/RPM/base/estimates.json", |
| 19 | + KEY_LOCATION: "mean.point_estimate", |
| 20 | + EXPECTED: 27500000, |
| 21 | + ERROR_MARGIN_FRAC: 0.05, |
| 22 | + }, |
| 23 | + ], |
| 24 | + MACOS: [ |
| 25 | + { |
| 26 | + NAME: "2020-09-04-BayLoop/M8L_BDS_ADR431_nativeODO_Swiftlets_OSR_prod_10Hz_AmotechL2/04-155800", |
| 27 | + FILE_PATH: "target/criterion/proc_messages/RPM/base/estimates.json", |
| 28 | + KEY_LOCATION: "mean.point_estimate", |
| 29 | + EXPECTED: 27500000, |
| 30 | + ERROR_MARGIN_FRAC: 0.05, |
| 31 | + }, |
| 32 | + ], |
| 33 | + LINUX: [ |
| 34 | + { |
| 35 | + NAME: "2020-09-04-BayLoop/M8L_BDS_ADR431_nativeODO_Swiftlets_OSR_prod_10Hz_AmotechL2/04-155800", |
| 36 | + FILE_PATH: "target/criterion/proc_messages/RPM/base/estimates.json", |
| 37 | + KEY_LOCATION: "mean.point_estimate", |
| 38 | + EXPECTED: 27500000, |
| 39 | + ERROR_MARGIN_FRAC: 0.05, |
| 40 | + }, |
| 41 | + ], |
| 42 | +} |
| 43 | + |
| 44 | + |
| 45 | +def get_nested_key(nested_dict: dict, key_path: str) -> Optional[Any]: |
| 46 | + """Extract a key in nested dict/json assuming stringified key_path. |
| 47 | +
|
| 48 | + Assuming `key_path` format: <key1>.<key2>.<key3> |
| 49 | +
|
| 50 | + Args: |
| 51 | + nested_dict (dict): The nested dictionary containing the desired key. |
| 52 | + key_path (str): The stringified nested dictionary key location. |
| 53 | +
|
| 54 | + Returns: |
| 55 | + Optional[Any]: A value corresponding to the key_path in the nested dictionary. |
| 56 | + Otherwise, None if not found. |
| 57 | + """ |
| 58 | + current_key, *next_keys = key_path.split(".", 1) |
| 59 | + value = nested_dict.get(current_key, None) |
| 60 | + return value if not isinstance(value, dict) and len(next_keys) != 1 else get_nested_key(value, next_keys[0]) |
| 61 | + |
| 62 | + |
| 63 | +def run_validate_benchmarks(): |
| 64 | + """Runner for a suite of benchmark validations. |
| 65 | + """ |
| 66 | + for os_, benchmarks in RUST_BENCHMARKS.items(): |
| 67 | + if os_ != sys.platform: |
| 68 | + continue |
| 69 | + for bench in benchmarks: |
| 70 | + with open(bench[FILE_PATH]) as fileo: |
| 71 | + bench_result = json.load(fileo) |
| 72 | + bench_value = get_nested_key(bench_result, bench[KEY_LOCATION]) |
| 73 | + assert bench_value is not None, f"Test:{bench[NAME]} retrieved bench value None." |
| 74 | + assert bench_value - bench[EXPECTED] <= bench[ERROR_MARGIN_FRAC] * bench[EXPECTED], ( |
| 75 | + f"Test:{bench[NAME]} Bench Value:{bench_value} not within " |
| 76 | + f"{bench[ERROR_MARGIN_FRAC]} of {bench[EXPECTED]}." |
| 77 | + ) |
| 78 | + print(f"PASS - {os_}:{bench[NAME]} MARGIN={bench_value - bench[EXPECTED]}") |
| 79 | + |
| 80 | + |
| 81 | +if __name__ == "__main__": |
| 82 | + run_validate_benchmarks() |
0 commit comments