blob: 67729d0b250c5388533108a935d65aae65921534 [file] [log] [blame]
Patrik Höglundcb0b8742019-11-18 13:46:38 +01001#!/usr/bin/env python
2# Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
3#
4# Use of this source code is governed by a BSD-style license
5# that can be found in the LICENSE file in the root of the source
6# tree. An additional intellectual property rights grant can be found
7# in the file PATENTS. All contributing project authors may
8# be found in the AUTHORS file in the root of the source tree.
9
Patrik Höglundabea2682020-01-17 13:36:29 +010010"""Adds build info to perf results and uploads them.
Patrik Höglundcb0b8742019-11-18 13:46:38 +010011
Patrik Höglundabea2682020-01-17 13:36:29 +010012The tests don't know which bot executed the tests or at what revision, so we
Patrik Höglund83245bd2020-01-30 09:33:57 +010013need to take their output and enrich it with this information. We load the proto
Patrik Höglundabea2682020-01-17 13:36:29 +010014from the tests, add the build information as shared diagnostics and then
15upload it to the dashboard.
Patrik Höglundcb0b8742019-11-18 13:46:38 +010016
17This script can't be in recipes, because we can't access the catapult APIs from
18there. It needs to be here source-side.
Patrik Höglundcb0b8742019-11-18 13:46:38 +010019"""
20
21import argparse
22import httplib2
23import json
Patrik Höglundabea2682020-01-17 13:36:29 +010024import os
Patrik Höglundcb0b8742019-11-18 13:46:38 +010025import sys
26import subprocess
27import zlib
28
Patrik Höglund83245bd2020-01-30 09:33:57 +010029# We just yank the python scripts we require into the PYTHONPATH. You could also
30# imagine a solution where we use for instance protobuf:py_proto_runtime to copy
31# catapult and protobuf code to out/, but this approach is allowed by
32# convention. Fortunately neither catapult nor protobuf require any build rules
33# to be executed. We can't do this for the histogram proto stub though because
34# it's generated; see _LoadHistogramSetFromProto.
35#
36# It would be better if there was an equivalent to py_binary in GN, but there's
37# not.
Patrik Höglundabea2682020-01-17 13:36:29 +010038SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
39CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir))
40sys.path.insert(0, os.path.join(CHECKOUT_ROOT, 'third_party', 'catapult',
41 'tracing'))
Patrik Höglund83245bd2020-01-30 09:33:57 +010042sys.path.insert(0, os.path.join(CHECKOUT_ROOT, 'third_party', 'protobuf',
43 'python'))
Patrik Höglundabea2682020-01-17 13:36:29 +010044
45from tracing.value import histogram_set
46from tracing.value.diagnostics import generic_set
47from tracing.value.diagnostics import reserved_infos
Patrik Höglundcb0b8742019-11-18 13:46:38 +010048
Patrik Höglund83245bd2020-01-30 09:33:57 +010049from google.protobuf import json_format
50
Patrik Höglundcb0b8742019-11-18 13:46:38 +010051
52def _GenerateOauthToken():
53 args = ['luci-auth', 'token']
54 p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
55 if p.wait() == 0:
56 output = p.stdout.read()
57 return output.strip()
58 else:
59 raise RuntimeError(
60 'Error generating authentication token.\nStdout: %s\nStderr:%s' %
61 (p.stdout.read(), p.stderr.read()))
62
63
Patrik Höglundabea2682020-01-17 13:36:29 +010064def _SendHistogramSet(url, histograms, oauth_token):
Patrik Höglundcb0b8742019-11-18 13:46:38 +010065 """Make a HTTP POST with the given JSON to the Performance Dashboard.
66
67 Args:
68 url: URL of Performance Dashboard instance, e.g.
69 "https://chromeperf.appspot.com".
Patrik Höglundabea2682020-01-17 13:36:29 +010070 histograms: a histogram set object that contains the data to be sent.
Patrik Höglundcb0b8742019-11-18 13:46:38 +010071 oauth_token: An oauth token to use for authorization.
72 """
73 headers = {'Authorization': 'Bearer %s' % oauth_token}
Patrik Höglundabea2682020-01-17 13:36:29 +010074 serialized = json.dumps(histograms.AsDicts(), indent=4)
75
76 if url.startswith('http://localhost'):
77 # The catapult server turns off compression in developer mode.
78 data = serialized
79 else:
80 data = zlib.compress(serialized)
Patrik Höglundcb0b8742019-11-18 13:46:38 +010081
82 http = httplib2.Http()
83 response, content = http.request(url + '/add_histograms', method='POST',
84 body=data, headers=headers)
85 return response, content
86
87
Patrik Höglund83245bd2020-01-30 09:33:57 +010088def _LoadHistogramSetFromProto(options):
89 # The webrtc_dashboard_upload gn rule will build the protobuf stub for python,
90 # so put it in the path for this script before we attempt to import it.
91 histogram_proto_path = os.path.join(options.outdir, 'pyproto', 'tracing',
92 'tracing', 'proto')
93 sys.path.insert(0, histogram_proto_path)
Patrik Höglundcb0b8742019-11-18 13:46:38 +010094
Patrik Höglund83245bd2020-01-30 09:33:57 +010095 # TODO(https://crbug.com/1029452): Get rid of this import hack once we can
96 # just hand the contents of input_results_file straight to the histogram set.
97 try:
98 import histogram_pb2
99 except ImportError:
100 raise ImportError('Could not find histogram_pb2. You need to build the '
101 'webrtc_dashboard_upload target before invoking this '
102 'script. Expected to find '
103 'histogram_pb2 in %s.' % histogram_proto_path)
104
105 with options.input_results_file as f:
106 histograms = histogram_pb2.HistogramSet()
107 histograms.ParseFromString(f.read())
108
109 # TODO(https://crbug.com/1029452): Don't convert to JSON as a middle step once
110 # there is a proto de-serializer ready in catapult.
111 json_data = json.loads(json_format.MessageToJson(histograms))
112 hs = histogram_set.HistogramSet()
113 hs.ImportDicts(json_data)
114 return hs
Patrik Höglundcb0b8742019-11-18 13:46:38 +0100115
Patrik Höglundabea2682020-01-17 13:36:29 +0100116
117def _AddBuildInfo(histograms, options):
118 common_diagnostics = {
119 reserved_infos.MASTERS: options.perf_dashboard_machine_group,
120 reserved_infos.BOTS: options.bot,
121 reserved_infos.POINT_ID: options.commit_position,
122 reserved_infos.BENCHMARKS: options.test_suite,
123 reserved_infos.WEBRTC_REVISIONS: str(options.webrtc_git_hash),
124 reserved_infos.BUILD_URLS: options.build_page_url,
125 }
126
127 for k, v in common_diagnostics.items():
128 histograms.AddSharedDiagnosticToAllHistograms(
129 k.name, generic_set.GenericSet([v]))
130
131
132def _DumpOutput(histograms, output_file):
133 with output_file:
134 json.dump(histograms.AsDicts(), output_file, indent=4)
Patrik Höglundcb0b8742019-11-18 13:46:38 +0100135
136
137def _CreateParser():
138 parser = argparse.ArgumentParser()
139 parser.add_argument('--perf-dashboard-machine-group', required=True,
140 help='The "master" the bots are grouped under. This '
141 'string is the group in the the perf dashboard path '
142 'group/bot/perf_id/metric/subtest.')
143 parser.add_argument('--bot', required=True,
144 help='The bot running the test (e.g. '
145 'webrtc-win-large-tests).')
146 parser.add_argument('--test-suite', required=True,
147 help='The key for the test in the dashboard (i.e. what '
148 'you select in the top-level test suite selector in the '
149 'dashboard')
150 parser.add_argument('--webrtc-git-hash', required=True,
151 help='webrtc.googlesource.com commit hash.')
152 parser.add_argument('--commit-position', type=int, required=True,
153 help='Commit pos corresponding to the git hash.')
154 parser.add_argument('--build-page-url', required=True,
155 help='URL to the build page for this build.')
156 parser.add_argument('--dashboard-url', required=True,
157 help='Which dashboard to use.')
158 parser.add_argument('--input-results-file', type=argparse.FileType(),
159 required=True,
160 help='A JSON file with output from WebRTC tests.')
161 parser.add_argument('--output-json-file', type=argparse.FileType('w'),
162 help='Where to write the output (for debugging).')
Patrik Höglund83245bd2020-01-30 09:33:57 +0100163 parser.add_argument('--outdir', required=True,
164 help='Path to the local out/ dir (usually out/Default)')
Patrik Höglundcb0b8742019-11-18 13:46:38 +0100165 return parser
166
167
168def main(args):
169 parser = _CreateParser()
170 options = parser.parse_args(args)
171
Patrik Höglund83245bd2020-01-30 09:33:57 +0100172 histograms = _LoadHistogramSetFromProto(options)
Patrik Höglundabea2682020-01-17 13:36:29 +0100173 _AddBuildInfo(histograms, options)
Patrik Höglundcb0b8742019-11-18 13:46:38 +0100174
175 if options.output_json_file:
Patrik Höglundabea2682020-01-17 13:36:29 +0100176 _DumpOutput(histograms, options.output_json_file)
Patrik Höglundcb0b8742019-11-18 13:46:38 +0100177
178 oauth_token = _GenerateOauthToken()
Patrik Höglundabea2682020-01-17 13:36:29 +0100179 response, content = _SendHistogramSet(
180 options.dashboard_url, histograms, oauth_token)
Patrik Höglundcb0b8742019-11-18 13:46:38 +0100181
182 if response.status == 200:
183 return 0
184 else:
185 print("Upload failed with %d: %s\n\n%s" % (response.status, response.reason,
186 content))
187 return 1
188
189
190if __name__ == '__main__':
191 sys.exit(main(sys.argv[1:]))