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
|
===========
timeutils
===========
Using a stopwatch (as a context manager)
----------------------------------------
::
>>> from oslo_utils import timeutils
>>> import time
>>>
>>> def slow_routine(delay):
... def i_am_slow():
... time.sleep(delay)
... return i_am_slow
...
>>>
>>> half_sec_func = slow_routine(0.5)
>>> with timeutils.StopWatch() as w:
... half_sec_func()
...
>>> print(w.elapsed())
0.500243999995
Manually using a stopwatch
--------------------------
::
>>> from oslo_utils import timeutils
>>> import time
>>> w = timeutils.StopWatch()
>>> w.start()
<oslo_utils.timeutils.StopWatch object at 0x2b85a0ab7590>
>>> time.sleep(0.1)
>>> time.sleep(0.1)
>>> time.sleep(0.1)
>>> time.sleep(0.1)
>>> w.stop()
<oslo_utils.timeutils.StopWatch object at 0x2b85a0ab7590>
>>> w.elapsed()
13.96467600017786
Tracking durations with a stopwatch
-----------------------------------
::
>>> from oslo_utils import timeutils
>>> w = timeutils.StopWatch(duration=10)
>>> w.start()
<oslo_utils.timeutils.StopWatch object at 0x2b85a7940a10>
>>> w.elapsed()
2.023942000232637
>>> w.leftover()
4.648160999640822
>>> w.leftover()
3.5522090001031756
>>> w.leftover()
3.0481000002473593
>>> w.leftover()
2.1918740002438426
>>> w.leftover()
1.6966530000790954
>>> w.leftover()
1.1202940000221133
>>> w.leftover()
0.0
>>> w.expired()
True
Tracking and splitting with a stopwatch
---------------------------------------
::
>>> from oslo_utils import timeutils
>>> w = timeutils.StopWatch()
>>> w.start()
<oslo_utils.timeutils.StopWatch object at 0x2ba75c12b050>
>>> w.split()
Split(elapsed=3.02423300035, length=3.02423300035)
>>> w.split()
Split(elapsed=6.44820600003, length=3.42397299968)
>>> w.split()
Split(elapsed=7.9678720003, length=1.51966600027)
>>> w.splits
(Split(elapsed=3.02423300035, length=3.02423300035), Split(elapsed=6.44820600003, length=3.42397299968), Split(elapsed=7.9678720003, length=1.51966600027))
>>> w.stop()
<oslo_utils.timeutils.StopWatch object at 0x2ba75c12b050>
>>> w.elapsed()
16.799759999848902
>>> w.splits
(Split(elapsed=3.02423300035, length=3.02423300035), Split(elapsed=6.44820600003, length=3.42397299968), Split(elapsed=7.9678720003, length=1.51966600027))
|