forked from mozilla/mozperftest-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_test_report.py
executable file
·327 lines (288 loc) · 9.23 KB
/
generate_test_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/python3
"""
Used to check what tests are running where. Primarily for Raptor and Browsertime.
"""
import argparse
import os
import json
try:
from urllib.parse import urlencode
from urllib.request import urlopen, urlretrieve
except ImportError:
from urllib import urlencode, urlretrieve
from urllib2 import urlopen
DEFAULT_TASK = "Gz9K6jGjQd6MvI2v6_02xg"
LINK = "https://firefoxci.taskcluster-artifacts.net/{}/0/public/full-task-graph.json"
def reporter_parser():
parser = argparse.ArgumentParser(
"This tool can be used to generate a report of where eveyrthing is "
+ "currently running."
)
parser.add_argument(
"--decision-task-id",
type=str,
default="",
help="The decision task to get the full-task-graph.json file from.",
)
parser.add_argument(
"--full-task-graph-path",
type=str,
default="",
help="A path to a full-task-graph.json artifact to use instead of "
"obtaining it from a decision task.",
)
parser.add_argument(
"--tests",
type=str,
nargs="+",
default=["raptor", "browsertime"],
help="The tests to build a report for (pattern matched). "
+ "Defaults to raptor and browsertime.",
)
parser.add_argument(
"--platforms",
type=str,
nargs="+",
default=[],
help="Platforms to return results for. Defaults to all.",
)
parser.add_argument(
"--output",
type=str,
nargs=1,
default=os.getcwd(),
help="This is where the data will be saved. Defaults to CWD.",
)
parser.add_argument(
"--platform-breakdown",
action="store_true",
default=False,
help="Get a platform breakdown instead of a test breakdown.",
)
parser.add_argument(
"--branch-breakdown",
action="store_true",
default=False,
help="Get a branch breakdown instead of a test breakdown.",
)
parser.add_argument(
"--match-all-tests",
action="store_true",
default=False,
help="Only tests which match all --tests entries will be selected.",
)
parser.add_argument(
"--ignore-no-projects",
action="store_true",
default=False,
help="Prevents displaying tests with no projects.",
)
parser.add_argument(
"--field",
type=str,
default="attributes.run_on_projects",
help="The field to search for (defaults to `attributes.run_on_projects`).",
)
parser.add_argument(
"--show-all-fields",
action="store_true",
default=False,
help="Show all available fields in the given FTG.",
)
return parser
def get_json(url, params=None):
print("Requesting full-task-graph.json from: %s" % url)
if params is not None:
url += "?" + urlencode(params)
r = urlopen(url).read().decode("utf-8")
return json.loads(r)
def pattern_match(name, patterns):
if not patterns:
return True
found = False
for pattern in patterns:
if pattern in name:
found = True
break
return found
def pattern_match_all(name, patterns):
if not patterns:
return True
found = True
for pattern in patterns:
if pattern not in name:
found = False
return found
def _get_all_fields(info, parent=""):
fields = []
keys = list(info.keys())
for key in keys:
if parent != "":
newparent = "{}.{}".format(parent, key)
else:
newparent = key
if isinstance(info[key], dict):
fields.extend(_get_all_fields(info[key], parent=newparent))
else:
fields.append(newparent)
return fields
def print_fields(ftg):
allfields = set()
for test, info in ftg.items():
allfields = set(_get_all_fields(info)) | allfields
for field in sorted(allfields):
print(field)
def generate_report(
decision_task,
tests,
platforms,
platform_breakdown=False,
branch_breakdown=False,
match_all_tests=False,
field="attributes.run_on_projects",
show_all_fields=False,
ftg_path="",
):
# Get the graph
if not ftg_path:
dt = decision_task or DEFAULT_TASK
cached_data = "%s.json" % dt
if not os.path.exists(cached_data):
ftg = get_json(LINK.format(dt))
with open(cached_data, "w") as f:
json.dump(ftg, f)
else:
with open(cached_data, "r") as f:
ftg = json.load(f)
else:
with open(ftg_path, "r") as f:
ftg = json.load(f)
## FILTER
# Filter out all tests that are not wanted
filt_test_ftg = {}
for test in ftg:
if match_all_tests:
if pattern_match_all(test, tests):
filt_test_ftg[test] = ftg[test]
else:
if pattern_match(test, tests):
filt_test_ftg[test] = ftg[test]
# Filter out all platforms that are not wanted
filt_ftg = {}
for test in filt_test_ftg:
if pattern_match(test, platforms):
filt_ftg[test] = filt_test_ftg[test]
if len(filt_ftg) == 0:
print("Could not find any matching test+platform combinations.")
return {}
if show_all_fields:
print_fields(filt_ftg)
return None
def get_field_value(info, field):
# Field is combined with `.` to
# denote nested entries.
value = info
path = field.split(".")
for key in path:
value = value[key]
if not isinstance(value, list):
value = [str(value)]
return value
## BREAKDOWN
# Split test from platform name
split_data = {}
for test, test_info in filt_ftg.items():
splitter = "/pgo-"
if "/opt-" in test:
splitter = "/opt-"
if splitter not in test:
platform, test = "unknown-platform", test
try:
platform = test_info.get("dependencies", {}).get("build")
if not platform:
platform = "unknown-platform"
except Exception as e:
print("Error trying to get platform for %s" % test)
print("%s %s" % (e.__class__.__name__, e))
else:
platform, test = test.split(splitter)
platform = platform + splitter.replace("-", "")
first = test
second = platform
if platform_breakdown:
first = platform
second = test
if first not in split_data:
split_data[first] = {}
if second not in split_data[first]:
split_data[first][second] = []
projects = get_field_value(test_info, field)
if not projects:
projects = ["none"]
split_data[first][second].extend(projects)
if branch_breakdown:
# Reorder the data
new_split_data = {}
for first, second_data in split_data.items():
for second, projects in second_data.items():
for project in projects:
if project not in new_split_data:
new_split_data[project] = {}
if first not in new_split_data[project]:
new_split_data[project][first] = []
new_split_data[project][first].append(second)
split_data = new_split_data
return split_data
def view_report(report, output, ignore_no_projects=False, branch_breakdown=False):
"""
Expecting a report with the form (or with test and platform swapped):
{'test-name': {'platform-name': [projects]}, ...}
"""
print("Report Breakdown\n")
for first, second_info in sorted(report.items()):
indent = " "
print(first)
printed = False
for second, projects in sorted(second_info.items()):
def _check_empty(projects):
if len(projects) == 0:
return True
if len(projects) > 1:
return False
if projects[0] == "none":
return True
if ignore_no_projects and _check_empty(projects):
continue
if not branch_breakdown:
print(indent + second + ": " + ", ".join(projects))
printed = True
else:
print("")
print(indent + second + ":")
for entry in projects:
print(indent + indent + entry)
printed = True
if not printed:
print(indent + "No tests satisfying criteria")
print("")
return
if __name__ == "__main__":
args = reporter_parser().parse_args()
report = generate_report(
args.decision_task_id,
args.tests,
args.platforms,
platform_breakdown=args.platform_breakdown,
branch_breakdown=args.branch_breakdown,
match_all_tests=args.match_all_tests,
field=args.field,
show_all_fields=args.show_all_fields,
ftg_path=args.full_task_graph_path,
)
if report:
view_report(
report,
args.output,
ignore_no_projects=args.ignore_no_projects,
branch_breakdown=args.branch_breakdown,
)