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