xref: /openbsd-src/gnu/llvm/lldb/packages/Python/lldbsuite/test/lldbbench.py (revision f6aab3d83b51b91c24247ad2c2573574de475a82)
1061da546Spatrickfrom __future__ import absolute_import
2061da546Spatrick
3061da546Spatrick# System modules
4061da546Spatrickimport time
5061da546Spatrick
6061da546Spatrick# Third-party modules
7061da546Spatrick
8061da546Spatrick# LLDB modules
9061da546Spatrickfrom .lldbtest import *
10061da546Spatrick
11061da546Spatrick
12061da546Spatrickclass Stopwatch(object):
13061da546Spatrick    """Stopwatch provides a simple utility to start/stop your stopwatch multiple
14061da546Spatrick    times.  Each start/stop is equal to a lap, with its elapsed time accumulated
15*f6aab3d8Srobert    while measurement is in progress.
16061da546Spatrick
17061da546Spatrick    When you're ready to start from scratch for another round of measurements,
18061da546Spatrick    be sure to call the reset() method.
19061da546Spatrick
20061da546Spatrick    For example,
21061da546Spatrick
22061da546Spatrick    sw = Stopwatch()
23061da546Spatrick    for i in range(1000):
24061da546Spatrick        with sw:
25061da546Spatrick            # Do some length operations...
26061da546Spatrick            ...
27061da546Spatrick    # Get the average time.
28061da546Spatrick    avg_time = sw.avg()
29061da546Spatrick
30061da546Spatrick    # Reset the stopwatch as we are about to perform other kind of operations.
31061da546Spatrick    sw.reset()
32061da546Spatrick    ...
33061da546Spatrick    """
34061da546Spatrick
35061da546Spatrick    #############################################################
36061da546Spatrick    #
37061da546Spatrick    # Context manager interfaces to support the 'with' statement.
38061da546Spatrick    #
39061da546Spatrick    #############################################################
40061da546Spatrick
41061da546Spatrick    def __enter__(self):
42061da546Spatrick        """
43061da546Spatrick        Context management protocol on entry to the body of the with statement.
44061da546Spatrick        """
45061da546Spatrick        return self.start()
46061da546Spatrick
47061da546Spatrick    def __exit__(self, type, value, tb):
48061da546Spatrick        """
49061da546Spatrick        Context management protocol on exit from the body of the with statement.
50061da546Spatrick        """
51061da546Spatrick        self.stop()
52061da546Spatrick
53061da546Spatrick    def reset(self):
54061da546Spatrick        self.__laps__ = 0
55061da546Spatrick        self.__total_elapsed__ = 0.0
56061da546Spatrick        self.__start__ = None
57061da546Spatrick        self.__stop__ = None
58061da546Spatrick        self.__elapsed__ = 0.0
59061da546Spatrick        self.__nums__ = []
60061da546Spatrick
61061da546Spatrick    def __init__(self):
62061da546Spatrick        self.reset()
63061da546Spatrick
64061da546Spatrick    def start(self):
65061da546Spatrick        if self.__start__ is None:
66061da546Spatrick            self.__start__ = time.time()
67061da546Spatrick        else:
68061da546Spatrick            raise Exception(
69061da546Spatrick                "start() already called, did you forget to stop() first?")
70061da546Spatrick        # Return self to facilitate the context manager __enter__ protocol.
71061da546Spatrick        return self
72061da546Spatrick
73061da546Spatrick    def stop(self):
74061da546Spatrick        if self.__start__ is not None:
75061da546Spatrick            self.__stop__ = time.time()
76061da546Spatrick            elapsed = self.__stop__ - self.__start__
77061da546Spatrick            self.__total_elapsed__ += elapsed
78061da546Spatrick            self.__laps__ += 1
79061da546Spatrick            self.__nums__.append(elapsed)
80061da546Spatrick            self.__start__ = None  # Reset __start__ to be None again.
81061da546Spatrick        else:
82061da546Spatrick            raise Exception("stop() called without first start()?")
83061da546Spatrick
84061da546Spatrick    def laps(self):
85061da546Spatrick        """Gets the number of laps. One lap is equal to a start/stop action."""
86061da546Spatrick        return self.__laps__
87061da546Spatrick
88061da546Spatrick    def avg(self):
89061da546Spatrick        """Equal to total elapsed time divided by the number of laps."""
90061da546Spatrick        return self.__total_elapsed__ / self.__laps__
91061da546Spatrick
92061da546Spatrick    # def sigma(self):
93061da546Spatrick    #    """Return the standard deviation of the available samples."""
94061da546Spatrick    #    if self.__laps__ <= 0:
95061da546Spatrick    #        return None
96061da546Spatrick    #    return numpy.std(self.__nums__)
97061da546Spatrick
98061da546Spatrick    def __str__(self):
99061da546Spatrick        return "Avg: %f (Laps: %d, Total Elapsed Time: %f, min=%f, max=%f)" % (self.avg(
100061da546Spatrick        ), self.__laps__, self.__total_elapsed__, min(self.__nums__), max(self.__nums__))
101061da546Spatrick
102061da546Spatrick
103061da546Spatrickclass BenchBase(TestBase):
104061da546Spatrick    """
105061da546Spatrick    Abstract base class for benchmark tests.
106061da546Spatrick    """
107061da546Spatrick
108061da546Spatrick    def setUp(self):
109061da546Spatrick        """Fixture for unittest test case setup."""
110061da546Spatrick        super(BenchBase, self).setUp()
111061da546Spatrick        # TestBase.setUp(self)
112061da546Spatrick        self.stopwatch = Stopwatch()
113061da546Spatrick
114061da546Spatrick    def tearDown(self):
115061da546Spatrick        """Fixture for unittest test case teardown."""
116061da546Spatrick        super(BenchBase, self).tearDown()
117061da546Spatrick        # TestBase.tearDown(self)
118061da546Spatrick        del self.stopwatch
119