1from __future__ import absolute_import 2 3# System modules 4import time 5 6# Third-party modules 7 8# LLDB modules 9from .lldbtest import * 10 11class Stopwatch(object): 12 """Stopwatch provides a simple utility to start/stop your stopwatch multiple 13 times. Each start/stop is equal to a lap, with its elapsed time accumulated 14 while measurment is in progress. 15 16 When you're ready to start from scratch for another round of measurements, 17 be sure to call the reset() method. 18 19 For example, 20 21 sw = Stopwatch() 22 for i in range(1000): 23 with sw: 24 # Do some length operations... 25 ... 26 # Get the average time. 27 avg_time = sw.avg() 28 29 # Reset the stopwatch as we are about to perform other kind of operations. 30 sw.reset() 31 ... 32 """ 33 34 ############################################################# 35 # 36 # Context manager interfaces to support the 'with' statement. 37 # 38 ############################################################# 39 40 def __enter__(self): 41 """ 42 Context management protocol on entry to the body of the with statement. 43 """ 44 return self.start() 45 46 def __exit__(self, type, value, tb): 47 """ 48 Context management protocol on exit from the body of the with statement. 49 """ 50 self.stop() 51 52 def reset(self): 53 self.__laps__ = 0 54 self.__total_elapsed__ = 0.0 55 self.__start__ = None 56 self.__stop__ = None 57 self.__elapsed__ = 0.0 58 self.__nums__ = [] 59 60 def __init__(self): 61 self.reset() 62 63 def start(self): 64 if self.__start__ is None: 65 self.__start__ = time.time() 66 else: 67 raise Exception("start() already called, did you forget to stop() first?") 68 # Return self to facilitate the context manager __enter__ protocol. 69 return self 70 71 def stop(self): 72 if self.__start__ is not None: 73 self.__stop__ = time.time() 74 elapsed = self.__stop__ - self.__start__ 75 self.__total_elapsed__ += elapsed 76 self.__laps__ += 1 77 self.__nums__.append(elapsed) 78 self.__start__ = None # Reset __start__ to be None again. 79 else: 80 raise Exception("stop() called without first start()?") 81 82 def laps(self): 83 """Gets the number of laps. One lap is equal to a start/stop action.""" 84 return self.__laps__ 85 86 def avg(self): 87 """Equal to total elapsed time divided by the number of laps.""" 88 return self.__total_elapsed__ / self.__laps__ 89 90 #def sigma(self): 91 # """Return the standard deviation of the available samples.""" 92 # if self.__laps__ <= 0: 93 # return None 94 # return numpy.std(self.__nums__) 95 96 def __str__(self): 97 return "Avg: %f (Laps: %d, Total Elapsed Time: %f, min=%f, max=%f)" % (self.avg(), 98 self.__laps__, 99 self.__total_elapsed__, 100 min(self.__nums__), 101 max(self.__nums__)) 102 103class BenchBase(TestBase): 104 """ 105 Abstract base class for benchmark tests. 106 """ 107 def setUp(self): 108 """Fixture for unittest test case setup.""" 109 super(BenchBase, self).setUp() 110 #TestBase.setUp(self) 111 self.stopwatch = Stopwatch() 112 113 def tearDown(self): 114 """Fixture for unittest test case teardown.""" 115 super(BenchBase, self).tearDown() 116 #TestBase.tearDown(self) 117 del self.stopwatch 118 119