xref: /netbsd-src/external/gpl3/gdb.old/dist/gdb/testsuite/gdb.perf/lib/perftest/reporter.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
16# Text reports are written here.
17# This is the perftest counterpart to gdb.sum.
18SUM_FILE_NAME = "perftest.sum"
19
20# Raw data that went into the report is written here.
21# This is the perftest counterpart to gdb.log.
22LOG_FILE_NAME = "perftest.log"
23
24
25class Reporter(object):
26    """Base class of reporter to report test results in a certain format.
27
28    Subclass, which is specific to a report format, should overwrite
29    methods report, start and end.
30    """
31
32    def __init__(self, append):
33        """Constructor of Reporter.
34
35        attribute append is used to determine whether to append or
36        overwrite log file.
37        """
38        self.append = append
39
40    def report(self, *args):
41        raise NotImplementedError("Abstract Method:report.")
42
43    def start(self):
44        """Invoked when reporting is started."""
45        raise NotImplementedError("Abstract Method:start.")
46
47    def end(self):
48        """Invoked when reporting is done.
49
50        It must be overridden to do some cleanups, such as closing file
51        descriptors.
52        """
53        raise NotImplementedError("Abstract Method:end.")
54
55
56class TextReporter(Reporter):
57    """Report results in a plain text file 'perftest.log'."""
58
59    def __init__(self, append):
60        super(TextReporter, self).__init__(Reporter(append))
61        self.txt_sum = None
62        self.txt_log = None
63
64    def report(self, test_name, measurement_name, data_points):
65        if len(data_points) == 0:
66            self.txt_sum.write(
67                "%s %s *no data recorded*\n" % (test_name, measurement_name)
68            )
69            return
70        average = sum(data_points) / len(data_points)
71        data_min = min(data_points)
72        data_max = max(data_points)
73        self.txt_sum.write("%s %s %s\n" % (test_name, measurement_name, average))
74        self.txt_log.write(
75            "%s %s %s, min %s, max %s, data %s\n"
76            % (test_name, measurement_name, average, data_min, data_max, data_points)
77        )
78
79    def start(self):
80        mode = "a+" if self.append else "w"
81        self.txt_sum = open(SUM_FILE_NAME, mode)
82        self.txt_log = open(LOG_FILE_NAME, mode)
83
84    def end(self):
85        self.txt_sum.close()
86        self.txt_log.close()
87