File size: 17,237 Bytes
079c32c |
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 433 |
import time
from functools import partial
from typing import Union
import pytest
from ding.utils.autolog import LoggedModel, LoggedValue, TickTime, NaturalTime, TimeMode
# noinspection DuplicatedCode
@pytest.mark.unittest
class TestAutologModel:
def __get_demo_class(self):
# noinspection DuplicatedCode
class _TickModel(LoggedModel):
in_time = LoggedValue(float)
out_time = LoggedValue(float)
__thruput_property_names = ['in_time', 'out_time']
def __init__(self, time_: 'BaseTime', expire: Union[int, float]): # noqa
LoggedModel.__init__(self, time_, expire)
self.__register()
def __register(self):
def __avg_func(prop_name: str) -> float:
records = self.range_values[prop_name]()
_sum = sum([_value for (_begin_time, _end_time), _value in records])
return _sum / self.expire
for _prop_name in self.__thruput_property_names:
self.register_attribute_value('thruput', _prop_name, partial(__avg_func, _prop_name))
self.register_attribute_value(
'reversed_name', _prop_name, partial(lambda name: name[::-1], _prop_name)
)
return _TickModel
def test_getter_and_setter(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor = _class(_time, expire=5)
with pytest.raises(ValueError):
_ = _tick_monitor.in_time
with pytest.raises(ValueError):
_ = _tick_monitor.out_time
_tick_monitor.in_time = 2.0
assert _tick_monitor.in_time == 2.0
with pytest.raises(TypeError):
_tick_monitor.in_time = None
assert _tick_monitor.in_time == 2.0
def test_property_getter(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor = _class(_time, expire=5)
assert _tick_monitor.reversed_name['in_time']() == 'emit_ni'
assert _tick_monitor.reversed_name['out_time']() == 'emit_tuo'
with pytest.raises(KeyError):
_tick_monitor.reversed_name['property_not_exist']()
with pytest.raises(KeyError):
_tick_monitor.reversed_nam['in_time']()
def test_time(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor = _class(_time, expire=5)
assert id(_tick_monitor.time) == id(_time)
assert _tick_monitor.fixed_time() == 0
assert _tick_monitor.current_time() == 0
_tick_monitor.freeze()
_time.step()
assert _tick_monitor.fixed_time() == 0
assert _tick_monitor.current_time() == 1
_tick_monitor.unfreeze()
assert _tick_monitor.fixed_time() == 1
assert _tick_monitor.current_time() == 1
def test_expire(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor = _class(_time, expire=5)
assert _tick_monitor.expire == 5
def test_with_tick_time(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor = _class(_time, expire=5)
_assert_results = [
(0.0, 0.0),
(0.2, 0.4),
(0.6, 1.2),
(1.2, 2.4),
(2.0, 4.0),
(3.0, 6.0),
(4.2, 8.4),
(5.4, 10.8),
(6.6, 13.2),
(7.8, 15.6),
]
for i in range(0, 10):
_tick_monitor.in_time = 1.0 * i
_tick_monitor.out_time = 2.0 * i
_time.step()
_thin, _thout = _tick_monitor.thruput['in_time'](), _tick_monitor.thruput['out_time']()
_exp_thin, _exp_thout = _assert_results[i]
assert _thin == _exp_thin
assert _thout == _exp_thout
def test_with_natural_time(self):
_class = self.__get_demo_class()
_time = NaturalTime()
_tick_monitor = _class(_time, expire=5)
_assert_results = [
(0.0, 0.0),
(0.2, 0.4),
(0.6, 1.2),
(1.2, 2.4),
(2.0, 4.0),
(3.0, 6.0),
(4.0, 8.0),
(5.0, 10.0),
(6.0, 12.0),
(7.0, 14.0),
]
for i in range(0, 10):
_tick_monitor.in_time = 1.0 * i
_tick_monitor.out_time = 2.0 * i
time.sleep(1.0)
_thin, _thout = _tick_monitor.thruput['in_time'](), _tick_monitor.thruput['out_time']()
_exp_thin, _exp_thout = _assert_results[i]
assert abs(_thin - _exp_thin) < 0.1
assert abs(_thout - _exp_thout) < 0.1
def test_double_model(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor_1 = _class(_time, expire=5)
_tick_monitor_2 = _class(_time, expire=5)
_assert_results_1 = [
(0.0, 0.0),
(0.2, 0.4),
(0.6, 1.2),
(1.2, 2.4),
(2.0, 4.0),
(3.0, 6.0),
(4.2, 8.4),
(5.4, 10.8),
(6.6, 13.2),
(7.8, 15.6),
]
_assert_results_2 = [
(0.0, 0.0), (0.4, 0.8), (1.2, 2.4), (2.4, 4.8), (4.0, 8.0), (6.0, 12.0), (8.4, 16.8), (10.8, 21.6),
(13.2, 26.4), (15.6, 31.2)
]
for i in range(0, 10):
_tick_monitor_1.in_time = 1.0 * i
_tick_monitor_1.out_time = 2.0 * i
_tick_monitor_2.in_time = 2.0 * i
_tick_monitor_2.out_time = 4.0 * i
_time.step()
_thin_1, _thout_1 = _tick_monitor_1.thruput['in_time'](), _tick_monitor_1.thruput['out_time']()
_exp_thin_1, _exp_thout_1 = _assert_results_1[i]
_thin_2, _thout_2 = _tick_monitor_2.thruput['in_time'](), _tick_monitor_2.thruput['out_time']()
_exp_thin_2, _exp_thout_2 = _assert_results_2[i]
assert (_thin_1, _thout_1) == (_exp_thin_1, _exp_thout_1)
assert (_thin_2, _thout_2) == (_exp_thin_2, _exp_thout_2)
def test_range_values_default(self):
_class = self.__get_demo_class()
_time = TickTime()
_tick_monitor = _class(_time, expire=5)
_assert_results = [
([((0, 1), 0.0)], [((0, 1), 0.0)]),
([((0, 1), 0.0), ((1, 2), 1.0)], [((0, 1), 0.0), ((1, 2), 2.0)]),
([((0, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0)], [((0, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0)]),
(
[((0, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0),
((3, 4), 3.0)], [((0, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0)]
),
(
[((0, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0), ((3, 4), 3.0),
((4, 5), 4.0)], [((0, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0)]
),
(
[((1, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0), ((3, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0)], [
((1, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0)
]
),
(
[((2, 2), 1.0), ((2, 3), 2.0), ((3, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0)], [
((2, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0)
]
),
(
[((3, 3), 2.0), ((3, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0), ((7, 8), 7.0)], [
((3, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0), ((7, 8), 14.0)
]
),
(
[((4, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0), ((7, 8), 7.0), ((8, 9), 8.0)], [
((4, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0), ((7, 8), 14.0), ((8, 9), 16.0)
]
),
(
[((5, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0), ((7, 8), 7.0), ((8, 9), 8.0), ((9, 10), 9.0)], [
((5, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0), ((7, 8), 14.0), ((8, 9), 16.0), ((9, 10), 18.0)
]
),
]
for i in range(0, 10):
_tick_monitor.in_time = 1.0 * i
_tick_monitor.out_time = 2.0 * i
_time.step()
_thin, _thout = _tick_monitor.range_values['in_time'](), _tick_monitor.range_values['out_time']()
_exp_thin, _exp_thout = _assert_results[i]
assert (_thin, _thout) == (_exp_thin, _exp_thout)
def test_range_values_absolute(self):
_class = self.__get_demo_class()
_time = TickTime(1)
_tick_monitor = _class(_time, expire=5)
_assert_results = [
([((1, 2), 0.0)], [((1, 2), 0.0)]),
([((1, 2), 0.0), ((2, 3), 1.0)], [((1, 2), 0.0), ((2, 3), 2.0)]),
([((1, 2), 0.0), ((2, 3), 1.0), ((3, 4), 2.0)], [((1, 2), 0.0), ((2, 3), 2.0), ((3, 4), 4.0)]),
(
[((1, 2), 0.0), ((2, 3), 1.0), ((3, 4), 2.0),
((4, 5), 3.0)], [((1, 2), 0.0), ((2, 3), 2.0), ((3, 4), 4.0), ((4, 5), 6.0)]
),
(
[((1, 2), 0.0), ((2, 3), 1.0), ((3, 4), 2.0), ((4, 5), 3.0),
((5, 6), 4.0)], [((1, 2), 0.0), ((2, 3), 2.0), ((3, 4), 4.0), ((4, 5), 6.0), ((5, 6), 8.0)]
),
(
[((2, 2), 0.0), ((2, 3), 1.0), ((3, 4), 2.0), ((4, 5), 3.0), ((5, 6), 4.0), ((6, 7), 5.0)], [
((2, 2), 0.0), ((2, 3), 2.0), ((3, 4), 4.0), ((4, 5), 6.0), ((5, 6), 8.0), ((6, 7), 10.0)
]
),
(
[((3, 3), 1.0), ((3, 4), 2.0), ((4, 5), 3.0), ((5, 6), 4.0), ((6, 7), 5.0), ((7, 8), 6.0)], [
((3, 3), 2.0), ((3, 4), 4.0), ((4, 5), 6.0), ((5, 6), 8.0), ((6, 7), 10.0), ((7, 8), 12.0)
]
),
(
[((4, 4), 2.0), ((4, 5), 3.0), ((5, 6), 4.0), ((6, 7), 5.0), ((7, 8), 6.0), ((8, 9), 7.0)], [
((4, 4), 4.0), ((4, 5), 6.0), ((5, 6), 8.0), ((6, 7), 10.0), ((7, 8), 12.0), ((8, 9), 14.0)
]
),
(
[((5, 5), 3.0), ((5, 6), 4.0), ((6, 7), 5.0), ((7, 8), 6.0), ((8, 9), 7.0), ((9, 10), 8.0)], [
((5, 5), 6.0), ((5, 6), 8.0), ((6, 7), 10.0), ((7, 8), 12.0), ((8, 9), 14.0), ((9, 10), 16.0)
]
),
(
[((6, 6), 4.0), ((6, 7), 5.0), ((7, 8), 6.0), ((8, 9), 7.0), ((9, 10), 8.0), ((10, 11), 9.0)], [
((6, 6), 8.0), ((6, 7), 10.0), ((7, 8), 12.0), ((8, 9), 14.0), ((9, 10), 16.0), ((10, 11), 18.0)
]
),
]
for i in range(0, 10):
_tick_monitor.in_time = 1.0 * i
_tick_monitor.out_time = 2.0 * i
_time.step()
_thin = _tick_monitor.range_values['in_time'](TimeMode.ABSOLUTE)
_thout = _tick_monitor.range_values['out_time'](TimeMode.ABSOLUTE)
_exp_thin, _exp_thout = _assert_results[i]
assert (_thin, _thout) == (_exp_thin, _exp_thout)
def test_range_values_lifecycle(self):
_class = self.__get_demo_class()
_time = TickTime(1)
_tick_monitor = _class(_time, expire=5)
_assert_results = [
([((0, 1), 0.0)], [((0, 1), 0.0)]),
([((0, 1), 0.0), ((1, 2), 1.0)], [((0, 1), 0.0), ((1, 2), 2.0)]),
([((0, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0)], [((0, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0)]),
(
[((0, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0),
((3, 4), 3.0)], [((0, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0)]
),
(
[((0, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0), ((3, 4), 3.0),
((4, 5), 4.0)], [((0, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0)]
),
(
[((1, 1), 0.0), ((1, 2), 1.0), ((2, 3), 2.0), ((3, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0)], [
((1, 1), 0.0), ((1, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0)
]
),
(
[((2, 2), 1.0), ((2, 3), 2.0), ((3, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0)], [
((2, 2), 2.0), ((2, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0)
]
),
(
[((3, 3), 2.0), ((3, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0), ((7, 8), 7.0)], [
((3, 3), 4.0), ((3, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0), ((7, 8), 14.0)
]
),
(
[((4, 4), 3.0), ((4, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0), ((7, 8), 7.0), ((8, 9), 8.0)], [
((4, 4), 6.0), ((4, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0), ((7, 8), 14.0), ((8, 9), 16.0)
]
),
(
[((5, 5), 4.0), ((5, 6), 5.0), ((6, 7), 6.0), ((7, 8), 7.0), ((8, 9), 8.0), ((9, 10), 9.0)], [
((5, 5), 8.0), ((5, 6), 10.0), ((6, 7), 12.0), ((7, 8), 14.0), ((8, 9), 16.0), ((9, 10), 18.0)
]
),
]
for i in range(0, 10):
_tick_monitor.in_time = 1.0 * i
_tick_monitor.out_time = 2.0 * i
_time.step()
# print('(', _tick_monitor.range_values['in_time'](TimeMode.RELATIVE_LIFECYCLE), ',',
# _tick_monitor.range_values['out_time'](TimeMode.RELATIVE_LIFECYCLE), '),')
_thin = _tick_monitor.range_values['in_time'](TimeMode.RELATIVE_LIFECYCLE)
_thout = _tick_monitor.range_values['out_time'](TimeMode.RELATIVE_LIFECYCLE)
_exp_thin, _exp_thout = _assert_results[i]
assert (_thin, _thout) == (_exp_thin, _exp_thout)
def test_range_values_current(self):
_class = self.__get_demo_class()
_time = TickTime(1)
_tick_monitor = _class(_time, expire=5)
_assert_results = [
([((-1, 0), 0.0)], [((-1, 0), 0.0)]),
([((-2, -1), 0.0), ((-1, 0), 1.0)], [((-2, -1), 0.0), ((-1, 0), 2.0)]),
([((-3, -2), 0.0), ((-2, -1), 1.0), ((-1, 0), 2.0)], [((-3, -2), 0.0), ((-2, -1), 2.0), ((-1, 0), 4.0)]),
(
[((-4, -3), 0.0), ((-3, -2), 1.0), ((-2, -1), 2.0),
((-1, 0), 3.0)], [((-4, -3), 0.0), ((-3, -2), 2.0), ((-2, -1), 4.0), ((-1, 0), 6.0)]
),
(
[((-5, -4), 0.0), ((-4, -3), 1.0), ((-3, -2), 2.0), ((-2, -1), 3.0),
((-1, 0), 4.0)], [((-5, -4), 0.0), ((-4, -3), 2.0), ((-3, -2), 4.0), ((-2, -1), 6.0), ((-1, 0), 8.0)]
),
(
[((-5, -5), 0.0), ((-5, -4), 1.0), ((-4, -3), 2.0), ((-3, -2), 3.0), ((-2, -1), 4.0), ((-1, 0), 5.0)], [
((-5, -5), 0.0), ((-5, -4), 2.0), ((-4, -3), 4.0), ((-3, -2), 6.0), ((-2, -1), 8.0),
((-1, 0), 10.0)
]
),
(
[((-5, -5), 1.0), ((-5, -4), 2.0), ((-4, -3), 3.0), ((-3, -2), 4.0), ((-2, -1), 5.0), ((-1, 0), 6.0)], [
((-5, -5), 2.0), ((-5, -4), 4.0), ((-4, -3), 6.0), ((-3, -2), 8.0), ((-2, -1), 10.0),
((-1, 0), 12.0)
]
),
(
[((-5, -5), 2.0), ((-5, -4), 3.0), ((-4, -3), 4.0), ((-3, -2), 5.0), ((-2, -1), 6.0), ((-1, 0), 7.0)], [
((-5, -5), 4.0), ((-5, -4), 6.0), ((-4, -3), 8.0), ((-3, -2), 10.0), ((-2, -1), 12.0),
((-1, 0), 14.0)
]
),
(
[((-5, -5), 3.0), ((-5, -4), 4.0), ((-4, -3), 5.0), ((-3, -2), 6.0), ((-2, -1), 7.0), ((-1, 0), 8.0)], [
((-5, -5), 6.0), ((-5, -4), 8.0), ((-4, -3), 10.0), ((-3, -2), 12.0), ((-2, -1), 14.0),
((-1, 0), 16.0)
]
),
(
[((-5, -5), 4.0), ((-5, -4), 5.0), ((-4, -3), 6.0), ((-3, -2), 7.0), ((-2, -1), 8.0), ((-1, 0), 9.0)], [
((-5, -5), 8.0), ((-5, -4), 10.0), ((-4, -3), 12.0), ((-3, -2), 14.0), ((-2, -1), 16.0),
((-1, 0), 18.0)
]
),
]
for i in range(0, 10):
_tick_monitor.in_time = 1.0 * i
_tick_monitor.out_time = 2.0 * i
_time.step()
# print('(', _tick_monitor.range_values['in_time'](TimeMode.RELATIVE_CURRENT_TIME), ',',
# _tick_monitor.range_values['out_time'](TimeMode.RELATIVE_CURRENT_TIME), '),')
_thin = _tick_monitor.range_values['in_time'](TimeMode.RELATIVE_CURRENT_TIME)
_thout = _tick_monitor.range_values['out_time'](TimeMode.RELATIVE_CURRENT_TIME)
_exp_thin, _exp_thout = _assert_results[i]
assert (_thin, _thout) == (_exp_thin, _exp_thout)
|