Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 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 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 10 | """Adds build info to perf results and uploads them. |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 11 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 12 | The tests don't know which bot executed the tests or at what revision, so we |
| 13 | need to take their output and enrich it with this information. We load the JSON |
| 14 | from the tests, add the build information as shared diagnostics and then |
| 15 | upload it to the dashboard. |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 16 | |
| 17 | This script can't be in recipes, because we can't access the catapult APIs from |
| 18 | there. It needs to be here source-side. |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 19 | """ |
| 20 | |
| 21 | import argparse |
| 22 | import httplib2 |
| 23 | import json |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 24 | import os |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 25 | import sys |
| 26 | import subprocess |
| 27 | import zlib |
| 28 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 29 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 30 | CHECKOUT_ROOT = os.path.abspath(os.path.join(SCRIPT_DIR, os.pardir, os.pardir)) |
| 31 | sys.path.insert(0, os.path.join(CHECKOUT_ROOT, 'third_party', 'catapult', |
| 32 | 'tracing')) |
| 33 | |
| 34 | from tracing.value import histogram_set |
| 35 | from tracing.value.diagnostics import generic_set |
| 36 | from tracing.value.diagnostics import reserved_infos |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 37 | |
| 38 | |
| 39 | def _GenerateOauthToken(): |
| 40 | args = ['luci-auth', 'token'] |
| 41 | p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) |
| 42 | if p.wait() == 0: |
| 43 | output = p.stdout.read() |
| 44 | return output.strip() |
| 45 | else: |
| 46 | raise RuntimeError( |
| 47 | 'Error generating authentication token.\nStdout: %s\nStderr:%s' % |
| 48 | (p.stdout.read(), p.stderr.read())) |
| 49 | |
| 50 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 51 | def _SendHistogramSet(url, histograms, oauth_token): |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 52 | """Make a HTTP POST with the given JSON to the Performance Dashboard. |
| 53 | |
| 54 | Args: |
| 55 | url: URL of Performance Dashboard instance, e.g. |
| 56 | "https://chromeperf.appspot.com". |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 57 | histograms: a histogram set object that contains the data to be sent. |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 58 | oauth_token: An oauth token to use for authorization. |
| 59 | """ |
| 60 | headers = {'Authorization': 'Bearer %s' % oauth_token} |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 61 | serialized = json.dumps(histograms.AsDicts(), indent=4) |
| 62 | |
| 63 | if url.startswith('http://localhost'): |
| 64 | # The catapult server turns off compression in developer mode. |
| 65 | data = serialized |
| 66 | else: |
| 67 | data = zlib.compress(serialized) |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 68 | |
| 69 | http = httplib2.Http() |
| 70 | response, content = http.request(url + '/add_histograms', method='POST', |
| 71 | body=data, headers=headers) |
| 72 | return response, content |
| 73 | |
| 74 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 75 | def _LoadHistogramSetFromJson(options): |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 76 | with options.input_results_file as f: |
| 77 | json_data = json.load(f) |
| 78 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 79 | histograms = histogram_set.HistogramSet() |
| 80 | histograms.ImportDicts(json_data) |
| 81 | return histograms |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 82 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 83 | |
| 84 | def _AddBuildInfo(histograms, options): |
| 85 | common_diagnostics = { |
| 86 | reserved_infos.MASTERS: options.perf_dashboard_machine_group, |
| 87 | reserved_infos.BOTS: options.bot, |
| 88 | reserved_infos.POINT_ID: options.commit_position, |
| 89 | reserved_infos.BENCHMARKS: options.test_suite, |
| 90 | reserved_infos.WEBRTC_REVISIONS: str(options.webrtc_git_hash), |
| 91 | reserved_infos.BUILD_URLS: options.build_page_url, |
| 92 | } |
| 93 | |
| 94 | for k, v in common_diagnostics.items(): |
| 95 | histograms.AddSharedDiagnosticToAllHistograms( |
| 96 | k.name, generic_set.GenericSet([v])) |
| 97 | |
| 98 | |
| 99 | def _DumpOutput(histograms, output_file): |
| 100 | with output_file: |
| 101 | json.dump(histograms.AsDicts(), output_file, indent=4) |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 102 | |
| 103 | |
| 104 | def _CreateParser(): |
| 105 | parser = argparse.ArgumentParser() |
| 106 | parser.add_argument('--perf-dashboard-machine-group', required=True, |
| 107 | help='The "master" the bots are grouped under. This ' |
| 108 | 'string is the group in the the perf dashboard path ' |
| 109 | 'group/bot/perf_id/metric/subtest.') |
| 110 | parser.add_argument('--bot', required=True, |
| 111 | help='The bot running the test (e.g. ' |
| 112 | 'webrtc-win-large-tests).') |
| 113 | parser.add_argument('--test-suite', required=True, |
| 114 | help='The key for the test in the dashboard (i.e. what ' |
| 115 | 'you select in the top-level test suite selector in the ' |
| 116 | 'dashboard') |
| 117 | parser.add_argument('--webrtc-git-hash', required=True, |
| 118 | help='webrtc.googlesource.com commit hash.') |
| 119 | parser.add_argument('--commit-position', type=int, required=True, |
| 120 | help='Commit pos corresponding to the git hash.') |
| 121 | parser.add_argument('--build-page-url', required=True, |
| 122 | help='URL to the build page for this build.') |
| 123 | parser.add_argument('--dashboard-url', required=True, |
| 124 | help='Which dashboard to use.') |
| 125 | parser.add_argument('--input-results-file', type=argparse.FileType(), |
| 126 | required=True, |
| 127 | help='A JSON file with output from WebRTC tests.') |
| 128 | parser.add_argument('--output-json-file', type=argparse.FileType('w'), |
| 129 | help='Where to write the output (for debugging).') |
| 130 | return parser |
| 131 | |
| 132 | |
| 133 | def main(args): |
| 134 | parser = _CreateParser() |
| 135 | options = parser.parse_args(args) |
| 136 | |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 137 | histograms = _LoadHistogramSetFromJson(options) |
| 138 | _AddBuildInfo(histograms, options) |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 139 | |
| 140 | if options.output_json_file: |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 141 | _DumpOutput(histograms, options.output_json_file) |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 142 | |
| 143 | oauth_token = _GenerateOauthToken() |
Patrik Höglund | abea268 | 2020-01-17 13:36:29 +0100 | [diff] [blame] | 144 | response, content = _SendHistogramSet( |
| 145 | options.dashboard_url, histograms, oauth_token) |
Patrik Höglund | cb0b874 | 2019-11-18 13:46:38 +0100 | [diff] [blame] | 146 | |
| 147 | if response.status == 200: |
| 148 | return 0 |
| 149 | else: |
| 150 | print("Upload failed with %d: %s\n\n%s" % (response.status, response.reason, |
| 151 | content)) |
| 152 | return 1 |
| 153 | |
| 154 | |
| 155 | if __name__ == '__main__': |
| 156 | sys.exit(main(sys.argv[1:])) |