Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python2.7 |
| 2 | # Copyright 2016, Google Inc. |
| 3 | # All rights reserved. |
| 4 | # |
| 5 | # Redistribution and use in source and binary forms, with or without |
| 6 | # modification, are permitted provided that the following conditions are |
| 7 | # met: |
| 8 | # |
| 9 | # * Redistributions of source code must retain the above copyright |
| 10 | # notice, this list of conditions and the following disclaimer. |
| 11 | # * Redistributions in binary form must reproduce the above |
| 12 | # copyright notice, this list of conditions and the following disclaimer |
| 13 | # in the documentation and/or other materials provided with the |
| 14 | # distribution. |
| 15 | # * Neither the name of Google Inc. nor the names of its |
| 16 | # contributors may be used to endorse or promote products derived from |
| 17 | # this software without specific prior written permission. |
| 18 | # |
| 19 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | """Tool to get build statistics from Jenkins and upload to BigQuery.""" |
| 32 | |
| 33 | import argparse |
| 34 | import jenkinsapi |
| 35 | from jenkinsapi.custom_exceptions import JenkinsAPIException |
| 36 | from jenkinsapi.jenkins import Jenkins |
| 37 | import json |
| 38 | import os |
| 39 | import re |
| 40 | import sys |
| 41 | import urllib |
| 42 | |
| 43 | |
| 44 | gcp_utils_dir = os.path.abspath(os.path.join( |
| 45 | os.path.dirname(__file__), '../gcp/utils')) |
| 46 | sys.path.append(gcp_utils_dir) |
| 47 | import big_query_utils |
| 48 | |
| 49 | |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 50 | _PROJECT_ID = 'grpc-testing' |
| 51 | _HAS_MATRIX = True |
Matt Kwong | 983c2da | 2016-11-10 15:07:59 -0800 | [diff] [blame] | 52 | _BUILDS = {'gRPC_interop_master': not _HAS_MATRIX, |
| 53 | 'gRPC_master_linux': not _HAS_MATRIX, |
| 54 | 'gRPC_master_macos': not _HAS_MATRIX, |
| 55 | 'gRPC_master_windows': not _HAS_MATRIX, |
| 56 | 'gRPC_performance_master': not _HAS_MATRIX, |
| 57 | 'gRPC_portability_master_linux': not _HAS_MATRIX, |
| 58 | 'gRPC_portability_master_windows': not _HAS_MATRIX, |
| 59 | 'gRPC_master_asanitizer_c': not _HAS_MATRIX, |
| 60 | 'gRPC_master_asanitizer_cpp': not _HAS_MATRIX, |
| 61 | 'gRPC_master_msan_c': not _HAS_MATRIX, |
| 62 | 'gRPC_master_tsanitizer_c': not _HAS_MATRIX, |
| 63 | 'gRPC_master_tsan_cpp': not _HAS_MATRIX, |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 64 | 'gRPC_interop_pull_requests': not _HAS_MATRIX, |
Matt Kwong | 983c2da | 2016-11-10 15:07:59 -0800 | [diff] [blame] | 65 | 'gRPC_performance_pull_requests': not _HAS_MATRIX, |
| 66 | 'gRPC_portability_pull_requests_linux': not _HAS_MATRIX, |
| 67 | 'gRPC_portability_pr_win': not _HAS_MATRIX, |
| 68 | 'gRPC_pull_requests_linux': not _HAS_MATRIX, |
| 69 | 'gRPC_pull_requests_macos': not _HAS_MATRIX, |
| 70 | 'gRPC_pr_win': not _HAS_MATRIX, |
| 71 | 'gRPC_pull_requests_asan_c': not _HAS_MATRIX, |
| 72 | 'gRPC_pull_requests_asan_cpp': not _HAS_MATRIX, |
| 73 | 'gRPC_pull_requests_msan_c': not _HAS_MATRIX, |
| 74 | 'gRPC_pull_requests_tsan_c': not _HAS_MATRIX, |
| 75 | 'gRPC_pull_requests_tsan_cpp': not _HAS_MATRIX, |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 76 | } |
| 77 | _URL_BASE = 'https://grpc-testing.appspot.com/job' |
Adele Zhou | 445534e | 2016-09-06 17:57:11 -0700 | [diff] [blame] | 78 | |
| 79 | # This is a dynamic list where known and active issues should be added. |
| 80 | # Fixed ones should be removed. |
| 81 | # Also try not to add multiple messages from the same failure. |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 82 | _KNOWN_ERRORS = [ |
| 83 | 'Failed to build workspace Tests with scheme AllTests', |
| 84 | 'Build timed out', |
Adele Zhou | 859d2a4 | 2016-09-16 15:15:44 -0700 | [diff] [blame] | 85 | 'TIMEOUT: tools/run_tests/pre_build_node.sh', |
| 86 | 'TIMEOUT: tools/run_tests/pre_build_ruby.sh', |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 87 | 'FATAL: Unable to produce a script file', |
Adele Zhou | 107e51e | 2016-09-08 11:37:30 -0700 | [diff] [blame] | 88 | 'FAILED: build_docker_c\+\+', |
Adele Zhou | 445534e | 2016-09-06 17:57:11 -0700 | [diff] [blame] | 89 | 'cannot find package \"cloud.google.com/go/compute/metadata\"', |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 90 | 'LLVM ERROR: IO failure on output stream.', |
| 91 | 'MSBUILD : error MSB1009: Project file does not exist.', |
Adele Zhou | 445534e | 2016-09-06 17:57:11 -0700 | [diff] [blame] | 92 | 'fatal: git fetch_pack: expected ACK/NAK', |
| 93 | 'Failed to fetch from http://github.com/grpc/grpc.git', |
| 94 | ('hudson.remoting.RemotingSystemException: java.io.IOException: ' |
| 95 | 'Backing channel is disconnected.'), |
Adele Zhou | 107e51e | 2016-09-08 11:37:30 -0700 | [diff] [blame] | 96 | 'hudson.remoting.ChannelClosedException', |
Adele Zhou | 59af64b | 2016-09-22 15:23:54 -0700 | [diff] [blame] | 97 | 'Could not initialize class hudson.Util', |
| 98 | 'Too many open files in system', |
Adele Zhou | 445534e | 2016-09-06 17:57:11 -0700 | [diff] [blame] | 99 | 'FAILED: bins/tsan/qps_openloop_test GRPC_POLL_STRATEGY=epoll', |
| 100 | 'FAILED: bins/tsan/qps_openloop_test GRPC_POLL_STRATEGY=legacy', |
| 101 | 'FAILED: bins/tsan/qps_openloop_test GRPC_POLL_STRATEGY=poll', |
| 102 | ('tests.bins/asan/h2_proxy_test streaming_error_response ' |
| 103 | 'GRPC_POLL_STRATEGY=legacy'), |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 104 | ] |
Matt Kwong | 983c2da | 2016-11-10 15:07:59 -0800 | [diff] [blame] | 105 | _NO_REPORT_FILES_FOUND_ERROR = 'No test report files were found. Configuration error?' |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 106 | _UNKNOWN_ERROR = 'Unknown error' |
| 107 | _DATASET_ID = 'build_statistics' |
| 108 | |
| 109 | |
| 110 | def _scrape_for_known_errors(html): |
| 111 | error_list = [] |
| 112 | known_error_count = 0 |
| 113 | for known_error in _KNOWN_ERRORS: |
| 114 | errors = re.findall(known_error, html) |
| 115 | this_error_count = len(errors) |
| 116 | if this_error_count > 0: |
| 117 | known_error_count += this_error_count |
| 118 | error_list.append({'description': known_error, |
| 119 | 'count': this_error_count}) |
| 120 | print('====> %d failures due to %s' % (this_error_count, known_error)) |
| 121 | return error_list, known_error_count |
| 122 | |
| 123 | |
Matt Kwong | 983c2da | 2016-11-10 15:07:59 -0800 | [diff] [blame] | 124 | def _no_report_files_found(html): |
| 125 | return _NO_REPORT_FILES_FOUND_ERROR in html |
| 126 | |
| 127 | |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 128 | def _get_last_processed_buildnumber(build_name): |
| 129 | query = 'SELECT max(build_number) FROM [%s:%s.%s];' % ( |
| 130 | _PROJECT_ID, _DATASET_ID, build_name) |
| 131 | query_job = big_query_utils.sync_query_job(bq, _PROJECT_ID, query) |
| 132 | page = bq.jobs().getQueryResults( |
| 133 | pageToken=None, |
| 134 | **query_job['jobReference']).execute(num_retries=3) |
| 135 | if page['rows'][0]['f'][0]['v']: |
| 136 | return int(page['rows'][0]['f'][0]['v']) |
| 137 | return 0 |
| 138 | |
| 139 | |
| 140 | def _process_matrix(build, url_base): |
| 141 | matrix_list = [] |
| 142 | for matrix in build.get_matrix_runs(): |
| 143 | matrix_str = re.match('.*\\xc2\\xbb ((?:[^,]+,?)+) #.*', |
| 144 | matrix.name).groups()[0] |
| 145 | matrix_tuple = matrix_str.split(',') |
| 146 | json_url = '%s/config=%s,language=%s,platform=%s/testReport/api/json' % ( |
| 147 | url_base, matrix_tuple[0], matrix_tuple[1], matrix_tuple[2]) |
| 148 | console_url = '%s/config=%s,language=%s,platform=%s/consoleFull' % ( |
| 149 | url_base, matrix_tuple[0], matrix_tuple[1], matrix_tuple[2]) |
| 150 | matrix_dict = {'name': matrix_str, |
| 151 | 'duration': matrix.get_duration().total_seconds()} |
| 152 | matrix_dict.update(_process_build(json_url, console_url)) |
| 153 | matrix_list.append(matrix_dict) |
| 154 | |
| 155 | return matrix_list |
| 156 | |
| 157 | |
| 158 | def _process_build(json_url, console_url): |
| 159 | build_result = {} |
| 160 | error_list = [] |
| 161 | try: |
| 162 | html = urllib.urlopen(json_url).read() |
| 163 | test_result = json.loads(html) |
| 164 | print('====> Parsing result from %s' % json_url) |
| 165 | failure_count = test_result['failCount'] |
| 166 | build_result['pass_count'] = test_result['passCount'] |
| 167 | build_result['failure_count'] = failure_count |
Matt Kwong | 983c2da | 2016-11-10 15:07:59 -0800 | [diff] [blame] | 168 | build_result['no_report_files_found'] = _no_report_files_found(html) |
Adele Zhou | bf3b769 | 2016-08-11 18:45:18 -0700 | [diff] [blame] | 169 | if failure_count > 0: |
| 170 | error_list, known_error_count = _scrape_for_known_errors(html) |
| 171 | unknown_error_count = failure_count - known_error_count |
| 172 | # This can happen if the same error occurs multiple times in one test. |
| 173 | if failure_count < known_error_count: |
| 174 | print('====> Some errors are duplicates.') |
| 175 | unknown_error_count = 0 |
| 176 | error_list.append({'description': _UNKNOWN_ERROR, |
| 177 | 'count': unknown_error_count}) |
| 178 | except Exception as e: |
| 179 | print('====> Got exception for %s: %s.' % (json_url, str(e))) |
| 180 | print('====> Parsing errors from %s.' % console_url) |
| 181 | html = urllib.urlopen(console_url).read() |
| 182 | build_result['pass_count'] = 0 |
| 183 | build_result['failure_count'] = 1 |
| 184 | error_list, _ = _scrape_for_known_errors(html) |
| 185 | if error_list: |
| 186 | error_list.append({'description': _UNKNOWN_ERROR, 'count': 0}) |
| 187 | else: |
| 188 | error_list.append({'description': _UNKNOWN_ERROR, 'count': 1}) |
| 189 | |
| 190 | if error_list: |
| 191 | build_result['error'] = error_list |
| 192 | |
| 193 | return build_result |
| 194 | |
| 195 | |
| 196 | # parse command line |
| 197 | argp = argparse.ArgumentParser(description='Get build statistics.') |
| 198 | argp.add_argument('-u', '--username', default='jenkins') |
| 199 | argp.add_argument('-b', '--builds', |
| 200 | choices=['all'] + sorted(_BUILDS.keys()), |
| 201 | nargs='+', |
| 202 | default=['all']) |
| 203 | args = argp.parse_args() |
| 204 | |
| 205 | J = Jenkins('https://grpc-testing.appspot.com', args.username, 'apiToken') |
| 206 | bq = big_query_utils.create_big_query() |
| 207 | |
| 208 | for build_name in _BUILDS.keys() if 'all' in args.builds else args.builds: |
| 209 | print('====> Build: %s' % build_name) |
| 210 | # Since get_last_completed_build() always fails due to malformatted string |
| 211 | # error, we use get_build_metadata() instead. |
| 212 | job = None |
| 213 | try: |
| 214 | job = J[build_name] |
| 215 | except Exception as e: |
| 216 | print('====> Failed to get build %s: %s.' % (build_name, str(e))) |
| 217 | continue |
| 218 | last_processed_build_number = _get_last_processed_buildnumber(build_name) |
| 219 | last_complete_build_number = job.get_last_completed_buildnumber() |
| 220 | # To avoid processing all builds for a project never looked at. In this case, |
| 221 | # only examine 10 latest builds. |
| 222 | starting_build_number = max(last_processed_build_number+1, |
| 223 | last_complete_build_number-9) |
| 224 | for build_number in xrange(starting_build_number, |
| 225 | last_complete_build_number+1): |
| 226 | print('====> Processing %s build %d.' % (build_name, build_number)) |
| 227 | build = None |
| 228 | try: |
| 229 | build = job.get_build_metadata(build_number) |
| 230 | except KeyError: |
| 231 | print('====> Build %s is missing. Skip.' % build_number) |
| 232 | continue |
| 233 | build_result = {'build_number': build_number, |
| 234 | 'timestamp': str(build.get_timestamp())} |
| 235 | url_base = json_url = '%s/%s/%d' % (_URL_BASE, build_name, build_number) |
| 236 | if _BUILDS[build_name]: # The build has matrix, such as gRPC_master. |
| 237 | build_result['matrix'] = _process_matrix(build, url_base) |
| 238 | else: |
| 239 | json_url = '%s/testReport/api/json' % url_base |
| 240 | console_url = '%s/consoleFull' % url_base |
| 241 | build_result['duration'] = build.get_duration().total_seconds() |
| 242 | build_result.update(_process_build(json_url, console_url)) |
| 243 | rows = [big_query_utils.make_row(build_number, build_result)] |
| 244 | if not big_query_utils.insert_rows(bq, _PROJECT_ID, _DATASET_ID, build_name, |
| 245 | rows): |
| 246 | print '====> Error uploading result to bigquery.' |
| 247 | sys.exit(1) |
| 248 | |