diff options
author | Folker Bernitt <fbernitt@thoughtworks.com> | 2016-02-23 10:01:39 +0100 |
---|---|---|
committer | Folker Bernitt <fbernitt@thoughtworks.com> | 2016-02-23 10:01:39 +0100 |
commit | 9e79c3b4e948e5d8ae1adab4fad7b49b50fe77c3 (patch) | |
tree | c36fa65092131c54054a9d982fbab3f663330f08 /service/pixelated/support | |
parent | 1b1476a0ebb07a5e22b9ee10b2cbc65226c3a12e (diff) |
Improve stopwatch output
- measures time and cpu time and displays percentage
Diffstat (limited to 'service/pixelated/support')
-rw-r--r-- | service/pixelated/support/__init__.py | 30 |
1 files changed, 26 insertions, 4 deletions
diff --git a/service/pixelated/support/__init__.py b/service/pixelated/support/__init__.py index 80ecaa2e..9ac52fe0 100644 --- a/service/pixelated/support/__init__.py +++ b/service/pixelated/support/__init__.py @@ -22,13 +22,34 @@ from twisted.internet import defer log = logging.getLogger(__name__) +def _start_stopwatch(): + return (time.time(), time.clock()) + + +def _stop_stopwatch(start): + start_time, start_clock = start + end_clock = time.clock() + end_time = time.time() + clock_duration = end_clock - start_clock + time_duration = end_time - start_time + if time_duration < 0.00000001: # avoid division by zero + time_duration = 0.00000001 + + estimate_percent_io = ((time_duration - clock_duration) / time_duration) * 100.0 + + return time_duration, clock_duration, estimate_percent_io + + def log_time(f): @wraps(f) def wrapper(*args, **kwds): - start = time.clock() + start = _start_stopwatch() + result = f(*args, **kwds) - log.info('Needed %f ms to execute %s' % ((time.clock() - start), f)) + + time_duration, clock_duration, estimate_percent_io = _stop_stopwatch(start) + log.info('Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % (time_duration, clock_duration, estimate_percent_io, f)) return result @@ -38,12 +59,13 @@ def log_time(f): def log_time_deferred(f): def log_time(result, start): - log.info('after callback: Needed %f ms to execute %s' % ((time.clock() - start), f)) + time_duration, clock_duration, estimate_percent_io = _stop_stopwatch(start) + log.info('after callback: Needed %fs (%fs cpu time, %.2f%% spent outside process) to execute %s' % (time_duration, clock_duration, estimate_percent_io, f)) return result @wraps(f) def wrapper(*args, **kwds): - start = time.clock() + start = _start_stopwatch() result = f(*args, **kwds) if isinstance(result, defer.Deferred): result.addCallback(log_time, start=start) |