[PowerPC] Collect some CallLowering arguments into a struct. [NFC]
[llvm-project.git] / lldb / packages / Python / lldbsuite / test / lldbbench.py
blob26ee6c21bd9a863923efe583af82cf5df17beb1c
1 from __future__ import absolute_import
3 # System modules
4 import time
6 # Third-party modules
8 # LLDB modules
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.
20 For example,
22 sw = Stopwatch()
23 for i in range(1000):
24 with sw:
25 # Do some length operations...
26 ...
27 # Get the average time.
28 avg_time = sw.avg()
30 # Reset the stopwatch as we are about to perform other kind of operations.
31 sw.reset()
32 ...
33 """
35 #############################################################
37 # Context manager interfaces to support the 'with' statement.
39 #############################################################
41 def __enter__(self):
42 """
43 Context management protocol on entry to the body of the with statement.
44 """
45 return self.start()
47 def __exit__(self, type, value, tb):
48 """
49 Context management protocol on exit from the body of the with statement.
50 """
51 self.stop()
53 def reset(self):
54 self.__laps__ = 0
55 self.__total_elapsed__ = 0.0
56 self.__start__ = None
57 self.__stop__ = None
58 self.__elapsed__ = 0.0
59 self.__nums__ = []
61 def __init__(self):
62 self.reset()
64 def start(self):
65 if self.__start__ is None:
66 self.__start__ = time.time()
67 else:
68 raise Exception(
69 "start() already called, did you forget to stop() first?")
70 # Return self to facilitate the context manager __enter__ protocol.
71 return self
73 def stop(self):
74 if self.__start__ is not None:
75 self.__stop__ = time.time()
76 elapsed = self.__stop__ - self.__start__
77 self.__total_elapsed__ += elapsed
78 self.__laps__ += 1
79 self.__nums__.append(elapsed)
80 self.__start__ = None # Reset __start__ to be None again.
81 else:
82 raise Exception("stop() called without first start()?")
84 def laps(self):
85 """Gets the number of laps. One lap is equal to a start/stop action."""
86 return self.__laps__
88 def avg(self):
89 """Equal to total elapsed time divided by the number of laps."""
90 return self.__total_elapsed__ / self.__laps__
92 # def sigma(self):
93 # """Return the standard deviation of the available samples."""
94 # if self.__laps__ <= 0:
95 # return None
96 # return numpy.std(self.__nums__)
98 def __str__(self):
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.
108 def setUp(self):
109 """Fixture for unittest test case setup."""
110 super(BenchBase, self).setUp()
111 # TestBase.setUp(self)
112 self.stopwatch = Stopwatch()
114 def tearDown(self):
115 """Fixture for unittest test case teardown."""
116 super(BenchBase, self).tearDown()
117 # TestBase.tearDown(self)
118 del self.stopwatch