forked from llvm/llvm-project
- Notifications
You must be signed in to change notification settings - Fork 339
/
Copy pathlldbbench.py
120 lines (97 loc) · 3.36 KB
/
lldbbench.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# System modules
importtime
# Third-party modules
# LLDB modules
from .lldbtestimport*
classStopwatch(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 measurement 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.
"""
returnself.start()
def__exit__(self, type, value, tb):
"""
Context management protocol on exit from the body of the with statement.
"""
self.stop()
defreset(self):
self.__laps__=0
self.__total_elapsed__=0.0
self.__start__=None
self.__stop__=None
self.__elapsed__=0.0
self.__nums__= []
def__init__(self):
self.reset()
defstart(self):
ifself.__start__isNone:
self.__start__=time.time()
else:
raiseException("start() already called, did you forget to stop() first?")
# Return self to facilitate the context manager __enter__ protocol.
returnself
defstop(self):
ifself.__start__isnotNone:
self.__stop__=time.time()
elapsed=self.__stop__-self.__start__
self.__total_elapsed__+=elapsed
self.__laps__+=1
self.__nums__.append(elapsed)
self.__start__=None# Reset __start__ to be None again.
else:
raiseException("stop() called without first start()?")
deflaps(self):
"""Gets the number of laps. One lap is equal to a start/stop action."""
returnself.__laps__
defavg(self):
"""Equal to total elapsed time divided by the number of laps."""
returnself.__total_elapsed__/self.__laps__
# def sigma(self):
# """Return the standard deviation of the available samples."""
# if self.__laps__ <= 0:
# return None
# return numpy.std(self.__nums__)
def__str__(self):
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__),
)
classBenchBase(TestBase):
"""
Abstract base class for benchmark tests.
"""
defsetUp(self):
"""Fixture for unittest test case setup."""
super(BenchBase, self).setUp()
# TestBase.setUp(self)
self.stopwatch=Stopwatch()
deftearDown(self):
"""Fixture for unittest test case teardown."""
super(BenchBase, self).tearDown()
# TestBase.tearDown(self)
delself.stopwatch