1#!/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
10"""Adds build info to perf results and uploads them.
11
12The tests don't know which bot executed the tests or at what revision, so we
13need to take their output and enrich it with this information. We load the proto
14from the tests, add the build information as shared diagnostics and then
15upload it to the dashboard.
16
17This script can't be in recipes, because we can't access the catapult APIs from
18there. It needs to be here source-side.
19"""
20
21import argparse
22import os
23import sys
24
25
26def _CreateParser():
27  parser = argparse.ArgumentParser()
28  parser.add_argument('--perf-dashboard-machine-group', required=True,
29                      help='The "master" the bots are grouped under. This '
30                           'string is the group in the the perf dashboard path '
31                            'group/bot/perf_id/metric/subtest.')
32  parser.add_argument('--bot', required=True,
33                      help='The bot running the test (e.g. '
34                           'webrtc-win-large-tests).')
35  parser.add_argument('--test-suite', required=True,
36                      help='The key for the test in the dashboard (i.e. what '
37                      'you select in the top-level test suite selector in the '
38                      'dashboard')
39  parser.add_argument('--webrtc-git-hash', required=True,
40                      help='webrtc.googlesource.com commit hash.')
41  parser.add_argument('--commit-position', type=int, required=True,
42                      help='Commit pos corresponding to the git hash.')
43  parser.add_argument('--build-page-url', required=True,
44                      help='URL to the build page for this build.')
45  parser.add_argument('--dashboard-url', required=True,
46                      help='Which dashboard to use.')
47  parser.add_argument('--input-results-file', type=argparse.FileType(),
48                      required=True,
49                      help='A JSON file with output from WebRTC tests.')
50  parser.add_argument('--output-json-file', type=argparse.FileType('w'),
51                      help='Where to write the output (for debugging).')
52  parser.add_argument('--outdir', required=True,
53                      help='Path to the local out/ dir (usually out/Default)')
54  return parser
55
56
57def _ConfigurePythonPath(options):
58  # We just yank the python scripts we require into the PYTHONPATH. You could
59  # also imagine a solution where we use for instance protobuf:py_proto_runtime
60  # to copy catapult and protobuf code to out/. This is the convention in
61  # Chromium and WebRTC python scripts. We do need to build histogram_pb2
62  # however, so that's why we add out/ to sys.path below.
63  #
64  # It would be better if there was an equivalent to py_binary in GN, but
65  # there's not.
66  script_dir = os.path.dirname(os.path.realpath(__file__))
67  checkout_root = os.path.abspath(
68      os.path.join(script_dir, os.pardir, os.pardir))
69
70  sys.path.insert(0, os.path.join(checkout_root, 'third_party', 'catapult',
71                                  'tracing'))
72  sys.path.insert(0, os.path.join(checkout_root, 'third_party', 'protobuf',
73                                  'python'))
74
75  # The webrtc_dashboard_upload gn rule will build the protobuf stub for python,
76  # so put it in the path for this script before we attempt to import it.
77  histogram_proto_path = os.path.join(
78      options.outdir, 'pyproto', 'tracing', 'tracing', 'proto')
79  sys.path.insert(0, histogram_proto_path)
80
81  # Fail early in case the proto hasn't been built.
82  from tracing.proto import histogram_proto
83  if not histogram_proto.HAS_PROTO:
84    raise ImportError('Could not find histogram_pb2. You need to build the '
85                      'webrtc_dashboard_upload target before invoking this '
86                      'script. Expected to find '
87                      'histogram_pb2.py in %s.' % histogram_proto_path)
88
89
90def main(args):
91  parser = _CreateParser()
92  options = parser.parse_args(args)
93
94  _ConfigurePythonPath(options)
95
96  import catapult_uploader
97
98  return catapult_uploader.UploadToDashboard(options)
99
100if __name__ == '__main__':
101  sys.exit(main(sys.argv[1:]))
102