1 from __future__
import absolute_import
9 from .lldbtest
import *
12 class Stopwatch(object):
13 """Stopwatch provides a simple utility to start/stop your stopwatch multiple
14 times. Each start/stop is equal to a lap, with its elapsed time accumulated
15 while measurment is in progress.
17 When you're ready to start from scratch for another round of measurements,
18 be sure to call the reset() method.
25 # Do some length operations...
27 # Get the average time.
30 # Reset the stopwatch as we are about to perform other kind of operations.
35 #############################################################
37 # Context manager interfaces to support the 'with' statement.
39 #############################################################
43 Context management protocol on entry to the body of the with statement.
47 def __exit__(self
, type, value
, tb
):
49 Context management protocol on exit from the body of the with statement.
55 self
.__total
_elapsed
__ = 0.0
58 self
.__elapsed
__ = 0.0
65 if self
.__start
__ is None:
66 self
.__start
__ = time
.time()
69 "start() already called, did you forget to stop() first?")
70 # Return self to facilitate the context manager __enter__ protocol.
74 if self
.__start
__ is not None:
75 self
.__stop
__ = time
.time()
76 elapsed
= self
.__stop
__ - self
.__start
__
77 self
.__total
_elapsed
__ += elapsed
79 self
.__nums
__.append(elapsed
)
80 self
.__start
__ = None # Reset __start__ to be None again.
82 raise Exception("stop() called without first start()?")
85 """Gets the number of laps. One lap is equal to a start/stop action."""
89 """Equal to total elapsed time divided by the number of laps."""
90 return self
.__total
_elapsed
__ / self
.__laps
__
93 # """Return the standard deviation of the available samples."""
94 # if self.__laps__ <= 0:
96 # return numpy.std(self.__nums__)
99 return "Avg: %f (Laps: %d, Total Elapsed Time: %f, min=%f, max=%f)" % (self
.avg(
100 ), self
.__laps
__, self
.__total
_elapsed
__, min(self
.__nums
__), max(self
.__nums
__))
103 class BenchBase(TestBase
):
105 Abstract base class for benchmark tests.
109 """Fixture for unittest test case setup."""
110 super(BenchBase
, self
).setUp()
111 # TestBase.setUp(self)
112 self
.stopwatch
= Stopwatch()
115 """Fixture for unittest test case teardown."""
116 super(BenchBase
, self
).tearDown()
117 # TestBase.tearDown(self)