blob: 6b568f874661da66a4535faf5ea60ed4634c1262 [file] [log] [blame]
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00001from __future__ import absolute_import
2
3# System modules
Johnny Chen2352af82011-08-02 00:43:09 +00004import time
Zachary Turnerc1b7cd72015-11-05 19:22:28 +00005
6# Third-party modules
7
8# LLDB modules
9from .lldbtest import *
Johnny Chen985e7402011-08-01 21:13:26 +000010
Johnny Chen2352af82011-08-02 00:43:09 +000011class 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
Johnny Chen09e87a62011-10-27 00:32:03 +000058 self.__nums__ = []
Johnny Chen2352af82011-08-02 00:43:09 +000059
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
Johnny Chen09e87a62011-10-27 00:32:03 +000077 self.__nums__.append(elapsed)
Johnny Chen2352af82011-08-02 00:43:09 +000078 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
Johnny Chen09e87a62011-10-27 00:32:03 +000090 #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
Johnny Chen2352af82011-08-02 00:43:09 +000096 def __str__(self):
Johnny Chen09e87a62011-10-27 00:32:03 +000097 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__))
Johnny Chen2352af82011-08-02 00:43:09 +0000102
Johnny Chen2a6eab02011-10-20 01:35:57 +0000103class BenchBase(TestBase):
Johnny Chen985e7402011-08-01 21:13:26 +0000104 """
105 Abstract base class for benchmark tests.
106 """
Johnny Chen2352af82011-08-02 00:43:09 +0000107 def setUp(self):
108 """Fixture for unittest test case setup."""
Johnny Chen2a6eab02011-10-20 01:35:57 +0000109 super(BenchBase, self).setUp()
110 #TestBase.setUp(self)
Johnny Chenda0384c2011-08-02 00:50:55 +0000111 self.stopwatch = Stopwatch()
Johnny Chen2352af82011-08-02 00:43:09 +0000112
113 def tearDown(self):
114 """Fixture for unittest test case teardown."""
Johnny Chen6acfb692012-04-19 23:50:00 +0000115 super(BenchBase, self).tearDown()
Johnny Chen2a6eab02011-10-20 01:35:57 +0000116 #TestBase.tearDown(self)
Johnny Chenda0384c2011-08-02 00:50:55 +0000117 del self.stopwatch
Johnny Chen985e7402011-08-01 21:13:26 +0000118