1# Copyright (C) 2013-2017 Free Software Foundation, Inc. 2 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. 7# 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. 12# 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/>. 15 16from __future__ import absolute_import 17 18import perftest.testresult as testresult 19import perftest.reporter as reporter 20from perftest.measure import Measure 21from perftest.measure import MeasurementCpuTime 22from perftest.measure import MeasurementWallTime 23from perftest.measure import MeasurementVmSize 24 25 26class TestCase(object): 27 """Base class of all performance testing cases. 28 29 Each sub-class should override methods execute_test, in which 30 several GDB operations are performed and measured by attribute 31 measure. Sub-class can also override method warm_up optionally 32 if the test case needs warm up. 33 """ 34 35 def __init__(self, name, measure): 36 """Constructor of TestCase. 37 38 Construct an instance of TestCase with a name and a measure 39 which is to measure the test by several different measurements. 40 """ 41 42 self.name = name 43 self.measure = measure 44 45 def execute_test(self): 46 """Abstract method to do the actual tests.""" 47 raise NotImplementedError("Abstract Method.") 48 49 def warm_up(self): 50 """Do some operations to warm up the environment.""" 51 pass 52 53 def run(self, warm_up=True, append=True): 54 """Run this test case. 55 56 It is a template method to invoke method warm_up, 57 execute_test, and finally report the measured results. 58 If parameter warm_up is True, run method warm_up. If parameter 59 append is True, the test result will be appended instead of 60 overwritten. 61 """ 62 if warm_up: 63 self.warm_up() 64 65 self.execute_test() 66 self.measure.report(reporter.TextReporter(append), self.name) 67 68class TestCaseWithBasicMeasurements(TestCase): 69 """Test case measuring CPU time, wall time and memory usage.""" 70 71 def __init__(self, name): 72 result_factory = testresult.SingleStatisticResultFactory() 73 measurements = [MeasurementCpuTime(result_factory.create_result()), 74 MeasurementWallTime(result_factory.create_result()), 75 MeasurementVmSize(result_factory.create_result())] 76 super (TestCaseWithBasicMeasurements, self).__init__ (name, Measure(measurements)) 77