blob: c01aa762149cdb494a5e5d7c820bc9a5165ba389 [file] [log] [blame]
Joe Onorato88ede352023-12-19 02:56:38 +00001#!/usr/bin/env python3
2# Copyright (C) 2023 The Android Open Source Project
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import sys
17if __name__ == "__main__":
18 sys.dont_write_bytecode = True
19
20import argparse
21import dataclasses
22import datetime
23import json
24import os
25import pathlib
26import statistics
27import zoneinfo
28
29import pretty
30import utils
31
32# TODO:
33# - Flag if the last postroll build was more than 15 seconds or something. That's
34# an indicator that something is amiss.
35# - Add a mode to print all of the values for multi-iteration runs
36# - Add a flag to reorder the tags
37# - Add a flag to reorder the headers in order to show grouping more clearly.
38
39
40def FindSummaries(args):
41 def find_summaries(directory):
42 return [str(p.resolve()) for p in pathlib.Path(directory).glob("**/summary.json")]
43 if not args:
44 # If they didn't give an argument, use the default dir
45 root = utils.get_root()
46 if not root:
47 return []
48 return find_summaries(root.joinpath("..", utils.DEFAULT_REPORT_DIR))
49 results = list()
50 for arg in args:
51 if os.path.isfile(arg):
52 # If it's a file add that
53 results.append(arg)
54 elif os.path.isdir(arg):
55 # If it's a directory, find all of the files there
56 results += find_summaries(arg)
57 else:
58 sys.stderr.write(f"Invalid summary argument: {arg}\n")
59 sys.exit(1)
60 return sorted(list(results))
61
62
63def LoadSummary(filename):
64 with open(filename) as f:
65 return json.load(f)
66
67# Columns:
68# Date
69# Branch
70# Tag
71# --
72# Lunch
73# Rows:
74# Benchmark
75
76@dataclasses.dataclass(frozen=True)
77class Key():
78 pass
79
80class Column():
81 def __init__(self):
82 pass
83
84def lunch_str(d):
85 "Convert a lunch dict to a string"
86 return f"{d['TARGET_PRODUCT']}-{d['TARGET_RELEASE']}-{d['TARGET_BUILD_VARIANT']}"
87
88def group_by(l, key):
89 "Return a list of tuples, grouped by key, sorted by key"
90 result = {}
91 for item in l:
92 result.setdefault(key(item), []).append(item)
93 return [(k, v) for k, v in result.items()]
94
95
96class Table:
97 def __init__(self):
98 self._data = {}
99 self._rows = []
100 self._cols = []
101
102 def Set(self, column_key, row_key, data):
103 self._data[(column_key, row_key)] = data
104 if not column_key in self._cols:
105 self._cols.append(column_key)
106 if not row_key in self._rows:
107 self._rows.append(row_key)
108
109 def Write(self, out):
110 table = []
111 # Expand the column items
112 for row in zip(*self._cols):
113 if row.count(row[0]) == len(row):
114 continue
115 table.append([""] + [col for col in row])
116 if table:
117 table.append(pretty.SEPARATOR)
118 # Populate the data
119 for row in self._rows:
120 table.append([str(row)] + [str(self._data.get((col, row), "")) for col in self._cols])
121 out.write(pretty.FormatTable(table))
122
123
124def format_duration_sec(ns):
125 "Format a duration in ns to second precision"
126 sec = round(ns / 1000000000)
127 h, sec = divmod(sec, 60*60)
128 m, sec = divmod(sec, 60)
129 result = ""
130 if h > 0:
131 result += f"{h:2d}h "
132 if h > 0 or m > 0:
133 result += f"{m:2d}m "
134 return result + f"{sec:2d}s"
135
Joe Onorato67c944b2023-12-21 13:29:18 -0800136
Joe Onorato88ede352023-12-19 02:56:38 +0000137def main(argv):
138 parser = argparse.ArgumentParser(
139 prog="format_benchmarks",
140 allow_abbrev=False, # Don't let people write unsupportable scripts.
141 description="Print analysis tables for benchmarks")
142
Joe Onorato67c944b2023-12-21 13:29:18 -0800143 parser.add_argument("--tags", nargs="*",
144 help="The tags to print, in order.")
145
Joe Onorato88ede352023-12-19 02:56:38 +0000146 parser.add_argument("summaries", nargs="*",
147 help="A summary.json file or a directory in which to look for summaries.")
148
149 args = parser.parse_args()
150
151 # Load the summaries
152 summaries = [(s, LoadSummary(s)) for s in FindSummaries(args.summaries)]
153
154 # Convert to MTV time
155 for filename, s in summaries:
156 dt = datetime.datetime.fromisoformat(s["start_time"])
157 dt = dt.astimezone(zoneinfo.ZoneInfo("America/Los_Angeles"))
158 s["datetime"] = dt
159 s["date"] = datetime.date(dt.year, dt.month, dt.day)
160
Joe Onorato67c944b2023-12-21 13:29:18 -0800161 # Filter out tags we don't want
162 if args.tags:
163 summaries = [(f, s) for f, s in summaries if s.get("tag", "") in args.tags]
164
165 # If they supplied tags, sort in that order, otherwise sort by tag
166 if args.tags:
167 tagsort = lambda tag: args.tags.index(tag)
168 else:
169 tagsort = lambda tag: tag
170
Joe Onorato88ede352023-12-19 02:56:38 +0000171 # Sort the summaries
Joe Onorato67c944b2023-12-21 13:29:18 -0800172 summaries.sort(key=lambda s: (s[1]["date"], s[1]["branch"], tagsort(s[1]["tag"])))
Joe Onorato88ede352023-12-19 02:56:38 +0000173
174 # group the benchmarks by column and iteration
175 def bm_key(b):
176 return (
177 lunch_str(b["lunch"]),
178 )
179 for filename, summary in summaries:
180 summary["columns"] = [(key, group_by(bms, lambda b: b["id"])) for key, bms
181 in group_by(summary["benchmarks"], bm_key)]
182
183 # Build the table
184 table = Table()
185 for filename, summary in summaries:
186 for key, column in summary["columns"]:
187 for id, cell in column:
188 duration_ns = statistics.median([b["duration_ns"] for b in cell])
189 table.Set(tuple([summary["date"].strftime("YYYY-MM-DD"),
190 summary["branch"],
191 summary["tag"]]
192 + list(key)),
193 cell[0]["title"], format_duration_sec(duration_ns))
194
195 table.Write(sys.stdout)
196
197if __name__ == "__main__":
198 main(sys.argv)
199