Skip to content

Commit

Permalink
Merge pull request #16 from harlowja/stats
Browse files Browse the repository at this point in the history
Add basic stats
  • Loading branch information
jd authored Aug 20, 2016
2 parents 5b4fc5f + 5efb378 commit 9ea6df0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions tenacity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,32 @@ def __init__(self,
self.retry = retry
self._before_attempts = before_attempts
self._after_attempts = after_attempts
self._statistics = {}

@property
def statistics(self):
"""A dictionary of runtime statistics this controller has gathered.
This dictionary will be empty when the controller has never been
ran. When it is running or has ran previously it should have (but
may not) have useful and/or informational keys and values when
running is underway and/or completed.
.. warning:: The keys in this dictionary **should** be some what
stable (not changing), but there existence **may**
change between major releases as new statistics are
gathered or removed so before accessing keys ensure that
they actually exist and handle when they do not.
"""
return self._statistics

def call(self, fn, *args, **kwargs):
self._statistics.clear()
start_time = int(round(now() * 1000))
self._statistics['start_time'] = start_time
attempt_number = 1
self._statistics['attempt_number'] = attempt_number
self._statistics['idle_for'] = 0
while True:
if self._before_attempts:
self._before_attempts(attempt_number)
Expand Down Expand Up @@ -127,16 +149,20 @@ def call(self, fn, *args, **kwargs):
delay_since_first_attempt_ms = int(
round(now() * 1000)
) - start_time
self._statistics['delay_since_first_attempt'] = \
delay_since_first_attempt_ms
if self.stop(attempt_number, delay_since_first_attempt_ms):
six.raise_from(RetryError(fut), fut.exception())

if self.wait:
sleep = self.wait(attempt_number, delay_since_first_attempt_ms)
else:
sleep = 0
self._statistics['idle_for'] += sleep
self.sleep(sleep / 1000.0)

attempt_number += 1
self._statistics['attempt_number'] = attempt_number


class Future(futures.Future):
Expand Down

0 comments on commit 9ea6df0

Please sign in to comment.