We use Python 3.6

go back Level: Advanced (img: AV / score: 4) level Bite 88. Write a performance monitoring context manager

7 out of 8 users completed this Bite ...
Will you be Pythonista #8 to crack this Bite?
Average Bite difficulty 1-10 rating: 9.0
» Up for a challenge? 💪

It's time for Context Managers part II. In Bite 20 you used it to roll back a transaction implementing the __enter__ and __exit__ dunder methods.

In this Bite you will write a context manager using contextlib.contextmanager that measures performance of operations executed in its context:

with timeit():
  ... measure time in seconds of the the stuff done in this block ...

Complete the timeit context manager implementing mentioned timing in seconds (duration).

Keep track of performance violations which we define as duration >= 2.2 (OPERATION_THRESHOLD_IN_SECONDS).

If there are >= 3 (ALERT_THRESHOLD) violations the same day, print ALERT: suffering performance hit today (ALERT_MSG).

Good luck and keep calm and code in Python!

Github login button