Package gofer :: Module metrics
[hide private]
[frames] | no frames]

Source Code for Module gofer.metrics

 1  # 
 2  # Copyright (c) 2011 Red Hat, Inc. 
 3  # 
 4  # This software is licensed to you under the GNU Lesser General Public 
 5  # License as published by the Free Software Foundation; either version 
 6  # 2 of the License (LGPLv2) or (at your option) any later version. 
 7  # There is NO WARRANTY for this software, express or implied, 
 8  # including the implied warranties of MERCHANTABILITY, 
 9  # NON-INFRINGEMENT, or FITNESS FOR A PARTICULAR PURPOSE. You should 
10  # have received a copy of LGPLv2 along with this software; if not, see 
11  # http://www.gnu.org/licenses/old-licenses/lgpl-2.0.txt. 
12  # 
13  # Jeff Ortel <jortel@redhat.com> 
14  # 
15   
16  """ 
17  The I{metrics} module defines classes and other resources 
18  designed for collecting and reporting performance metrics. 
19  """ 
20   
21  import time 
22  from math import modf 
23   
24 -class Timer:
25
26 - def __init__(self, started=0, stopped=0):
27 self.started = started 28 self.stopped = stopped
29
30 - def start(self):
31 self.started = time.time() 32 self.stopped = 0 33 return self
34
35 - def stop(self):
36 if self.started > 0: 37 self.stopped = time.time() 38 return self
39
40 - def duration(self):
41 return ( self.stopped - self.started )
42
43 - def __str__(self):
44 if self.started == 0: 45 return 'not-running' 46 if self.started > 0 and self.stopped == 0: 47 return 'started: %d (running)' % self.started 48 duration = self.duration() 49 jmod = ( lambda m : (m[1], m[0]*1000) ) 50 if duration < 1: 51 ms = (duration*1000) 52 return '%d (ms)' % ms 53 if duration < 60: 54 m = modf(duration) 55 return '%d.%.3d (seconds)' % jmod(m) 56 m = modf(duration/60) 57 return '%d.%.3d (minutes)' % jmod(m)
58