Revert bfd_use_reserved_id patch
[binutils-gdb.git] / gdb / testsuite / gdb.perf / lib / perftest / reporter.py
blobe04b2f61c1f8d2211009675c314169b7986a59ff
1 # Copyright (C) 2013-2024 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/>.
16 # Text reports are written here.
17 # This is the perftest counterpart to gdb.sum.
18 SUM_FILE_NAME = "perftest.sum"
20 # Raw data that went into the report is written here.
21 # This is the perftest counterpart to gdb.log.
22 LOG_FILE_NAME = "perftest.log"
25 class Reporter(object):
26 """Base class of reporter to report test results in a certain format.
28 Subclass, which is specific to a report format, should overwrite
29 methods report, start and end.
30 """
32 def __init__(self, append):
33 """Constructor of Reporter.
35 attribute append is used to determine whether to append or
36 overwrite log file.
37 """
38 self.append = append
40 def report(self, *args):
41 raise NotImplementedError("Abstract Method:report.")
43 def start(self):
44 """Invoked when reporting is started."""
45 raise NotImplementedError("Abstract Method:start.")
47 def end(self):
48 """Invoked when reporting is done.
50 It must be overridden to do some cleanups, such as closing file
51 descriptors.
52 """
53 raise NotImplementedError("Abstract Method:end.")
56 class TextReporter(Reporter):
57 """Report results in a plain text file 'perftest.log'."""
59 def __init__(self, append):
60 super(TextReporter, self).__init__(Reporter(append))
61 self.txt_sum = None
62 self.txt_log = None
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)
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)
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)
84 def end(self):
85 self.txt_sum.close()
86 self.txt_log.close()