Python 3 - Turn on absolute imports, and fix existing imports.
Absolute imports were introduced in Python 2.5 as a feature
(e.g. from __future__ import absolute_import), and made default
in Python 3.
When absolute imports are enabled, the import system changes in
a couple of ways:
1) The `import foo` syntax will *only* search sys.path. If `foo`
isn't in sys.path, it won't be found. Period. Without absolute
imports, the import system will also search the same directory
that the importing file resides in, so that you can easily
import from the same folder.
2) From inside a package, you can use a dot syntax to refer to higher
levels of the current package. For example, if you are in the
package lldbsuite.test.utility, then ..foo refers to
lldbsuite.test.foo. You can use this notation with the
`from X import Y` syntax to write intra-package references. For
example, using the previous locationa s a starting point, writing
`from ..support import seven` would import lldbsuite.support.seven
Since this is now the default behavior in Python 3, this means that
importing from the same directory with `import foo` *no longer works*.
As a result, the only way to have portable code is to force absolute
imports for all versions of Python.
See PEP 0328 [https://www.python.org/dev/peps/pep-0328/] for more
information about absolute and relative imports.
Differential Revision: http://reviews.llvm.org/D14342
Reviewed By: Todd Fiala
llvm-svn: 252191
2015-11-06 03:22:28 +08:00
|
|
|
from __future__ import absolute_import
|
|
|
|
|
|
|
|
# System modules
|
2011-08-02 08:43:09 +08:00
|
|
|
import time
|
Python 3 - Turn on absolute imports, and fix existing imports.
Absolute imports were introduced in Python 2.5 as a feature
(e.g. from __future__ import absolute_import), and made default
in Python 3.
When absolute imports are enabled, the import system changes in
a couple of ways:
1) The `import foo` syntax will *only* search sys.path. If `foo`
isn't in sys.path, it won't be found. Period. Without absolute
imports, the import system will also search the same directory
that the importing file resides in, so that you can easily
import from the same folder.
2) From inside a package, you can use a dot syntax to refer to higher
levels of the current package. For example, if you are in the
package lldbsuite.test.utility, then ..foo refers to
lldbsuite.test.foo. You can use this notation with the
`from X import Y` syntax to write intra-package references. For
example, using the previous locationa s a starting point, writing
`from ..support import seven` would import lldbsuite.support.seven
Since this is now the default behavior in Python 3, this means that
importing from the same directory with `import foo` *no longer works*.
As a result, the only way to have portable code is to force absolute
imports for all versions of Python.
See PEP 0328 [https://www.python.org/dev/peps/pep-0328/] for more
information about absolute and relative imports.
Differential Revision: http://reviews.llvm.org/D14342
Reviewed By: Todd Fiala
llvm-svn: 252191
2015-11-06 03:22:28 +08:00
|
|
|
|
|
|
|
# Third-party modules
|
|
|
|
|
|
|
|
# LLDB modules
|
|
|
|
from .lldbtest import *
|
2011-08-02 05:13:26 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-08-02 08:43:09 +08:00
|
|
|
class Stopwatch(object):
|
|
|
|
"""Stopwatch provides a simple utility to start/stop your stopwatch multiple
|
|
|
|
times. Each start/stop is equal to a lap, with its elapsed time accumulated
|
|
|
|
while measurment is in progress.
|
|
|
|
|
|
|
|
When you're ready to start from scratch for another round of measurements,
|
|
|
|
be sure to call the reset() method.
|
|
|
|
|
|
|
|
For example,
|
|
|
|
|
|
|
|
sw = Stopwatch()
|
|
|
|
for i in range(1000):
|
|
|
|
with sw:
|
|
|
|
# Do some length operations...
|
|
|
|
...
|
|
|
|
# Get the average time.
|
|
|
|
avg_time = sw.avg()
|
|
|
|
|
|
|
|
# Reset the stopwatch as we are about to perform other kind of operations.
|
|
|
|
sw.reset()
|
|
|
|
...
|
|
|
|
"""
|
|
|
|
|
|
|
|
#############################################################
|
|
|
|
#
|
|
|
|
# Context manager interfaces to support the 'with' statement.
|
|
|
|
#
|
|
|
|
#############################################################
|
|
|
|
|
|
|
|
def __enter__(self):
|
|
|
|
"""
|
|
|
|
Context management protocol on entry to the body of the with statement.
|
|
|
|
"""
|
|
|
|
return self.start()
|
|
|
|
|
|
|
|
def __exit__(self, type, value, tb):
|
|
|
|
"""
|
|
|
|
Context management protocol on exit from the body of the with statement.
|
|
|
|
"""
|
|
|
|
self.stop()
|
|
|
|
|
|
|
|
def reset(self):
|
|
|
|
self.__laps__ = 0
|
|
|
|
self.__total_elapsed__ = 0.0
|
|
|
|
self.__start__ = None
|
|
|
|
self.__stop__ = None
|
|
|
|
self.__elapsed__ = 0.0
|
2011-10-27 08:32:03 +08:00
|
|
|
self.__nums__ = []
|
2011-08-02 08:43:09 +08:00
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.reset()
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
if self.__start__ is None:
|
|
|
|
self.__start__ = time.time()
|
|
|
|
else:
|
2016-09-07 04:57:50 +08:00
|
|
|
raise Exception(
|
|
|
|
"start() already called, did you forget to stop() first?")
|
2011-08-02 08:43:09 +08:00
|
|
|
# Return self to facilitate the context manager __enter__ protocol.
|
|
|
|
return self
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
if self.__start__ is not None:
|
|
|
|
self.__stop__ = time.time()
|
|
|
|
elapsed = self.__stop__ - self.__start__
|
|
|
|
self.__total_elapsed__ += elapsed
|
|
|
|
self.__laps__ += 1
|
2011-10-27 08:32:03 +08:00
|
|
|
self.__nums__.append(elapsed)
|
2016-09-07 04:57:50 +08:00
|
|
|
self.__start__ = None # Reset __start__ to be None again.
|
2011-08-02 08:43:09 +08:00
|
|
|
else:
|
|
|
|
raise Exception("stop() called without first start()?")
|
|
|
|
|
|
|
|
def laps(self):
|
|
|
|
"""Gets the number of laps. One lap is equal to a start/stop action."""
|
|
|
|
return self.__laps__
|
|
|
|
|
|
|
|
def avg(self):
|
|
|
|
"""Equal to total elapsed time divided by the number of laps."""
|
|
|
|
return self.__total_elapsed__ / self.__laps__
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
# def sigma(self):
|
2011-10-27 08:32:03 +08:00
|
|
|
# """Return the standard deviation of the available samples."""
|
|
|
|
# if self.__laps__ <= 0:
|
|
|
|
# return None
|
|
|
|
# return numpy.std(self.__nums__)
|
|
|
|
|
2011-08-02 08:43:09 +08:00
|
|
|
def __str__(self):
|
2016-09-07 04:57:50 +08:00
|
|
|
return "Avg: %f (Laps: %d, Total Elapsed Time: %f, min=%f, max=%f)" % (self.avg(
|
|
|
|
), self.__laps__, self.__total_elapsed__, min(self.__nums__), max(self.__nums__))
|
|
|
|
|
2011-08-02 08:43:09 +08:00
|
|
|
|
2011-10-20 09:35:57 +08:00
|
|
|
class BenchBase(TestBase):
|
2011-08-02 05:13:26 +08:00
|
|
|
"""
|
|
|
|
Abstract base class for benchmark tests.
|
|
|
|
"""
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-08-02 08:43:09 +08:00
|
|
|
def setUp(self):
|
|
|
|
"""Fixture for unittest test case setup."""
|
2011-10-20 09:35:57 +08:00
|
|
|
super(BenchBase, self).setUp()
|
2016-09-07 04:57:50 +08:00
|
|
|
# TestBase.setUp(self)
|
2011-08-02 08:50:55 +08:00
|
|
|
self.stopwatch = Stopwatch()
|
2011-08-02 08:43:09 +08:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Fixture for unittest test case teardown."""
|
2012-04-20 07:50:00 +08:00
|
|
|
super(BenchBase, self).tearDown()
|
2016-09-07 04:57:50 +08:00
|
|
|
# TestBase.tearDown(self)
|
2011-08-02 08:50:55 +08:00
|
|
|
del self.stopwatch
|