Skip to content
This repository has been archived by the owner on Mar 28, 2021. It is now read-only.

Commit

Permalink
Merge 5faa539 into 02331c5
Browse files Browse the repository at this point in the history
  • Loading branch information
samgiagtzoglou committed Jul 17, 2014
2 parents 02331c5 + 5faa539 commit 327b4dd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 6 additions & 0 deletions appmetrics/metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ def new_meter(name, tick_interval=5):

return new_metric(name, meter.Meter, tick_interval)

def new_timer(name):
"""
Build a new "timer" metric
"""

return new_metric(name, simple_metrics.Timer)

def new_histogram_with_implicit_reservoir(name, reservoir_type='uniform', *reservoir_args, **reservoir_kwargs):
"""
Expand Down
37 changes: 36 additions & 1 deletion appmetrics/simple_metrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"""

import threading

import time

class Counter(object):
"""
Expand Down Expand Up @@ -82,3 +82,38 @@ def get(self):

def raw_data(self):
return self.value

class Timer(object):
"""
A simple timer, with function timing support
"""

def __init__(self):
self.value = 0
self.lock = threading.Lock()

def start(self):
self.startTime = time.time();

def stop(self):
self.endTime = time.time()
self.value = (self.endTime - self.startTime)

def time(self, function, *args):
"""
Times a function. Format should be timer.time(function, arguments))
"""
self.startTime = time.time()
x = function(*args)
self.value = (time.time() - self.startTime)
return x

def get(self):
"""
Return the timer's current value
"""

return dict(kind="timer", value=self.value)

def raw_data(self):
return self.value

0 comments on commit 327b4dd

Please sign in to comment.