1 # Copyright (C) 2013-2020 Free Software Foundation, Inc.
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 3 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 class Measure(object):
21 """A class that measure and collect the interesting data for a given testcase.
23 An instance of Measure has a collection of measurements, and each
24 of them is to measure a given aspect, such as time and memory.
27 def __init__(self, measurements):
28 """Constructor of measure.
30 measurements is a collection of Measurement objects.
33 self.measurements = measurements
35 def measure(self, func, id):
36 """Measure the operations done by func with a collection of measurements."""
37 # Enable GC, force GC and disable GC before running test in order to reduce
38 # the interference from GC.
43 for m in self.measurements:
48 for m in self.measurements:
53 def report(self, reporter, name):
54 """Report the measured results."""
55 for m in self.measurements:
56 m.report(reporter, name)
58 class Measurement(object):
59 """A measurement for a certain aspect."""
61 def __init__(self, name, result):
62 """Constructor of Measurement.
64 Attribute result is the TestResult associated with measurement.
70 """Abstract method to start the measurement."""
71 raise NotImplementedError("Abstract Method:start")
74 """Abstract method to stop the measurement.
76 When the measurement is stopped, we've got something, and
77 record them in result.
79 raise NotImplementedError("Abstract Method:stop.")
81 def report(self, reporter, name):
82 """Report the measured data by argument reporter."""
83 self.result.report(reporter, name + " " + self.name)
85 class MeasurementCpuTime(Measurement):
86 """Measurement on CPU time."""
87 # On UNIX, time.clock() measures the amount of CPU time that has
88 # been used by the current process. On Windows it will measure
89 # wall-clock seconds elapsed since the first call to the function.
90 # Something other than time.clock() should be used to measure CPU
93 def __init__(self, result):
94 super(MeasurementCpuTime, self).__init__("cpu_time", result)
98 self.start_time = time.clock()
104 cpu_time = time.clock() - self.start_time
105 self.result.record (id, cpu_time)
107 class MeasurementWallTime(Measurement):
108 """Measurement on Wall time."""
110 def __init__(self, result):
111 super(MeasurementWallTime, self).__init__("wall_time", result)
115 self.start_time = time.time()
118 wall_time = time.time() - self.start_time
119 self.result.record (id, wall_time)
121 class MeasurementVmSize(Measurement):
122 """Measurement on memory usage represented by VmSize."""
124 def __init__(self, result):
125 super(MeasurementVmSize, self).__init__("vmsize", result)
127 def _compute_process_memory_usage(self, key):
128 file_path = "/proc/%d/status" % os.getpid()
136 v = v[i:].split(None, 3)
145 memory_used = self._compute_process_memory_usage("VmSize:")
146 self.result.record (id, memory_used)