blob: b00817de61a5c966c1dc197fd2bbedf2d5d549f0 [file] [log] [blame]
Johnny Chencbfd0932011-08-02 00:43:09 +00001import time
Johnny Chen289fd0e2011-10-20 01:35:57 +00002from lldbtest import *
Johnny Chen71cb7972011-08-01 21:13:26 +00003
Johnny Chencbfd0932011-08-02 00:43:09 +00004class Stopwatch(object):
5 """Stopwatch provides a simple utility to start/stop your stopwatch multiple
6 times. Each start/stop is equal to a lap, with its elapsed time accumulated
7 while measurment is in progress.
8
9 When you're ready to start from scratch for another round of measurements,
10 be sure to call the reset() method.
11
12 For example,
13
14 sw = Stopwatch()
15 for i in range(1000):
16 with sw:
17 # Do some length operations...
18 ...
19 # Get the average time.
20 avg_time = sw.avg()
21
22 # Reset the stopwatch as we are about to perform other kind of operations.
23 sw.reset()
24 ...
25 """
26
27 #############################################################
28 #
29 # Context manager interfaces to support the 'with' statement.
30 #
31 #############################################################
32
33 def __enter__(self):
34 """
35 Context management protocol on entry to the body of the with statement.
36 """
37 return self.start()
38
39 def __exit__(self, type, value, tb):
40 """
41 Context management protocol on exit from the body of the with statement.
42 """
43 self.stop()
44
45 def reset(self):
46 self.__laps__ = 0
47 self.__total_elapsed__ = 0.0
48 self.__start__ = None
49 self.__stop__ = None
50 self.__elapsed__ = 0.0
51
52 def __init__(self):
53 self.reset()
54
55 def start(self):
56 if self.__start__ is None:
57 self.__start__ = time.time()
58 else:
59 raise Exception("start() already called, did you forget to stop() first?")
60 # Return self to facilitate the context manager __enter__ protocol.
61 return self
62
63 def stop(self):
64 if self.__start__ is not None:
65 self.__stop__ = time.time()
66 elapsed = self.__stop__ - self.__start__
67 self.__total_elapsed__ += elapsed
68 self.__laps__ += 1
69 self.__start__ = None # Reset __start__ to be None again.
70 else:
71 raise Exception("stop() called without first start()?")
72
73 def laps(self):
74 """Gets the number of laps. One lap is equal to a start/stop action."""
75 return self.__laps__
76
77 def avg(self):
78 """Equal to total elapsed time divided by the number of laps."""
79 return self.__total_elapsed__ / self.__laps__
80
81 def __str__(self):
Johnny Chen113388f2011-08-02 22:54:37 +000082 return "Avg: %f (Laps: %d, Total Elapsed Time: %f)" % (self.avg(),
Johnny Chencbfd0932011-08-02 00:43:09 +000083 self.__laps__,
84 self.__total_elapsed__)
85
Johnny Chen289fd0e2011-10-20 01:35:57 +000086class BenchBase(TestBase):
Johnny Chen71cb7972011-08-01 21:13:26 +000087 """
88 Abstract base class for benchmark tests.
89 """
Johnny Chencbfd0932011-08-02 00:43:09 +000090 def setUp(self):
91 """Fixture for unittest test case setup."""
Johnny Chen289fd0e2011-10-20 01:35:57 +000092 super(BenchBase, self).setUp()
93 #TestBase.setUp(self)
Johnny Chenbd434532011-08-02 00:50:55 +000094 self.stopwatch = Stopwatch()
Johnny Chencbfd0932011-08-02 00:43:09 +000095
96 def tearDown(self):
97 """Fixture for unittest test case teardown."""
Johnny Chen289fd0e2011-10-20 01:35:57 +000098 super(BenchBase, self).setUp()
99 #TestBase.tearDown(self)
Johnny Chenbd434532011-08-02 00:50:55 +0000100 del self.stopwatch
Johnny Chen71cb7972011-08-01 21:13:26 +0000101