Skip to content

Commit cc83572

Browse files
committed
refactor: move format_with_reduced_unit into ab_test.py
This function was only used in ab_test.py. Signed-off-by: Egor Lazarchuk <yegorlz@amazon.co.uk>
1 parent ab32316 commit cc83572

File tree

2 files changed

+88
-91
lines changed

2 files changed

+88
-91
lines changed

tests/host_tools/metrics.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -133,89 +133,3 @@ def emit_raw_emf(emf_msg: dict):
133133
(json.dumps(emf_msg) + "\n").encode("utf-8"),
134134
(emf_endpoint.hostname, emf_endpoint.port),
135135
)
136-
137-
138-
UNIT_REDUCTIONS = {
139-
"Microseconds": "Milliseconds",
140-
"Milliseconds": "Seconds",
141-
"Bytes": "Kilobytes",
142-
"Kilobytes": "Megabytes",
143-
"Megabytes": "Gigabytes",
144-
"Gigabytes": "Terabytes",
145-
"Bits": "Kilobits",
146-
"Kilobits": "Megabits",
147-
"Megabits": "Gigabits",
148-
"Gigabits": "Terabit",
149-
"Bytes/Second": "Kilobytes/Second",
150-
"Kilobytes/Second": "Megabytes/Second",
151-
"Megabytes/Second": "Gigabytes/Second",
152-
"Gigabytes/Second": "Terabytes/Second",
153-
"Bits/Second": "Kilobits/Second",
154-
"Kilobits/Second": "Megabits/Second",
155-
"Megabits/Second": "Gigabits/Second",
156-
"Gigabits/Second": "Terabits/Second",
157-
}
158-
INV_UNIT_REDUCTIONS = {v: k for k, v in UNIT_REDUCTIONS.items()}
159-
160-
161-
UNIT_SHORTHANDS = {
162-
"Seconds": "s",
163-
"Microseconds": "μs",
164-
"Milliseconds": "ms",
165-
"Bytes": "B",
166-
"Kilobytes": "KB",
167-
"Megabytes": "MB",
168-
"Gigabytes": "GB",
169-
"Terabytes": "TB",
170-
"Bits": "Bit",
171-
"Kilobits": "KBit",
172-
"Megabits": "MBit",
173-
"Gigabits": "GBit",
174-
"Terabits": "TBit",
175-
"Percent": "%",
176-
"Count": "",
177-
"Bytes/Second": "B/s",
178-
"Kilobytes/Second": "KB/s",
179-
"Megabytes/Second": "MB/s",
180-
"Gigabytes/Second": "GB/s",
181-
"Terabytes/Second": "TB/s",
182-
"Bits/Second": "Bit/s",
183-
"Kilobits/Second": "KBit/s",
184-
"Megabits/Second": "MBit/s",
185-
"Gigabits/Second": "GBit/s",
186-
"Terabits/Second": "TBit/s",
187-
"Count/Second": "Hz",
188-
"None": "",
189-
}
190-
191-
192-
def reduce_value(value, unit):
193-
"""
194-
Utility function for expressing a value in the largest possible unit in which it would still be >= 1
195-
196-
For example, `reduce_value(1_000_000, Bytes)` would return (1, Megabytes)
197-
"""
198-
# Could do this recursively, but I am worried about infinite recursion
199-
# due to precision problems (e.g. infinite loop of dividing/multiplying by 1000, alternating
200-
# between values < 1 and >= 1000).
201-
while abs(value) < 1 and unit in INV_UNIT_REDUCTIONS:
202-
value *= 1000
203-
unit = INV_UNIT_REDUCTIONS[unit]
204-
while abs(value) >= 1000 and unit in UNIT_REDUCTIONS:
205-
value /= 1000
206-
unit = UNIT_REDUCTIONS[unit]
207-
208-
return value, unit
209-
210-
211-
def format_with_reduced_unit(value, unit):
212-
"""
213-
Utility function for pretty printing a given value by choosing a unit as large as possible,
214-
and then outputting its shorthand.
215-
216-
For example, `format_with_reduced_unit(1_000_000, Bytes)` would return "1MB".
217-
"""
218-
reduced_value, reduced_unit = reduce_value(value, unit)
219-
formatted_unit = UNIT_SHORTHANDS.get(reduced_unit, reduced_unit)
220-
221-
return f"{reduced_value:.2f}{formatted_unit}"

tools/ab_test.py

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
statistical regression test across all the list-valued properties collected.
1717
"""
1818

19-
import glob
2019
import argparse
20+
import glob
2121
import json
2222
import os
2323
import statistics
@@ -32,10 +32,93 @@
3232
# pylint:disable=wrong-import-position
3333
from framework.ab_test import binary_ab_test, check_regression
3434
from framework.properties import global_props
35-
from host_tools.metrics import (
36-
format_with_reduced_unit,
37-
get_metrics_logger,
38-
)
35+
from host_tools.metrics import get_metrics_logger
36+
37+
UNIT_REDUCTIONS = {
38+
"Microseconds": "Milliseconds",
39+
"Milliseconds": "Seconds",
40+
"Bytes": "Kilobytes",
41+
"Kilobytes": "Megabytes",
42+
"Megabytes": "Gigabytes",
43+
"Gigabytes": "Terabytes",
44+
"Bits": "Kilobits",
45+
"Kilobits": "Megabits",
46+
"Megabits": "Gigabits",
47+
"Gigabits": "Terabit",
48+
"Bytes/Second": "Kilobytes/Second",
49+
"Kilobytes/Second": "Megabytes/Second",
50+
"Megabytes/Second": "Gigabytes/Second",
51+
"Gigabytes/Second": "Terabytes/Second",
52+
"Bits/Second": "Kilobits/Second",
53+
"Kilobits/Second": "Megabits/Second",
54+
"Megabits/Second": "Gigabits/Second",
55+
"Gigabits/Second": "Terabits/Second",
56+
}
57+
INV_UNIT_REDUCTIONS = {v: k for k, v in UNIT_REDUCTIONS.items()}
58+
59+
60+
UNIT_SHORTHANDS = {
61+
"Seconds": "s",
62+
"Microseconds": "μs",
63+
"Milliseconds": "ms",
64+
"Bytes": "B",
65+
"Kilobytes": "KB",
66+
"Megabytes": "MB",
67+
"Gigabytes": "GB",
68+
"Terabytes": "TB",
69+
"Bits": "Bit",
70+
"Kilobits": "KBit",
71+
"Megabits": "MBit",
72+
"Gigabits": "GBit",
73+
"Terabits": "TBit",
74+
"Percent": "%",
75+
"Count": "",
76+
"Bytes/Second": "B/s",
77+
"Kilobytes/Second": "KB/s",
78+
"Megabytes/Second": "MB/s",
79+
"Gigabytes/Second": "GB/s",
80+
"Terabytes/Second": "TB/s",
81+
"Bits/Second": "Bit/s",
82+
"Kilobits/Second": "KBit/s",
83+
"Megabits/Second": "MBit/s",
84+
"Gigabits/Second": "GBit/s",
85+
"Terabits/Second": "TBit/s",
86+
"Count/Second": "Hz",
87+
"None": "",
88+
}
89+
90+
91+
def reduce_value(value, unit):
92+
"""
93+
Utility function for expressing a value in the largest possible unit in which it would still be >= 1
94+
95+
For example, `reduce_value(1_000_000, Bytes)` would return (1, Megabytes)
96+
"""
97+
# Could do this recursively, but I am worried about infinite recursion
98+
# due to precision problems (e.g. infinite loop of dividing/multiplying by 1000, alternating
99+
# between values < 1 and >= 1000).
100+
while abs(value) < 1 and unit in INV_UNIT_REDUCTIONS:
101+
value *= 1000
102+
unit = INV_UNIT_REDUCTIONS[unit]
103+
while abs(value) >= 1000 and unit in UNIT_REDUCTIONS:
104+
value /= 1000
105+
unit = UNIT_REDUCTIONS[unit]
106+
107+
return value, unit
108+
109+
110+
def format_with_reduced_unit(value, unit):
111+
"""
112+
Utility function for pretty printing a given value by choosing a unit as large as possible,
113+
and then outputting its shorthand.
114+
115+
For example, `format_with_reduced_unit(1_000_000, Bytes)` would return "1MB".
116+
"""
117+
reduced_value, reduced_unit = reduce_value(value, unit)
118+
formatted_unit = UNIT_SHORTHANDS.get(reduced_unit, reduced_unit)
119+
120+
return f"{reduced_value:.2f}{formatted_unit}"
121+
39122

40123
# Performance tests that are known to be unstable and exhibit variances of up to 60% of the mean
41124
IGNORED = [

0 commit comments

Comments
 (0)