md.python.measure
md.python.measure is a component that provides measure API.
Architecture overview
Install
Usage example
Time measure
md.python.measure.Time
component designed to measure operation time, for example:
import time
import md.python.measure
if __name__ == '__main__':
with md.python.measure.Time() as measure:
time.sleep(0.42) # your operation, for example ...
print(
f'Operation started at `{measure.start!s}`, '
f'finished at `{measure.end!s}` '
f'and took `{measure.time()}` seconds'
)
will print:
typically, it may be required for statistic purposes:
import psr.log
import md.python.measure
if __name__ == '__main__':
logger: psr.log.LoggerInterface = ...
with md.python.measure.Time() as measure:
pass # your operation
logger.notice('Important blocking operation done successful', {'duration': measure.time()})
Set now function
By default, monotonic clock (time.monotonic
function) is used to get current time,
if different driver is required now
argument should be passed in constructor,
for example:
import time
import md.python.measure
if __name__ == '__main__':
with md.python.measure.Time(now=time.time) as measure:
time.sleep(0.42)
print(
f'Operation started at `{measure.start!s}`, '
f'finished at `{measure.end!s}` '
f'and took `{measure.time()}` seconds'
)
Operation started at `1675068910.211057`, finished at `1675068910.632562` and took `0.4215049743652344` seconds
Offline measure
md.python.measure.Time
component could be used to measure duration with preset timings,
for example: