1#!/usr/bin/env python2.7 2# 3# Copyright 2017 gRPC authors. 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17# Convert google-benchmark json output to something that can be uploaded to 18# BigQuery 19 20import sys 21import json 22import csv 23import bm_json 24import json 25import subprocess 26 27columns = [] 28 29for row in json.loads( 30 subprocess.check_output( 31 ['bq', '--format=json', 'show', 32 'microbenchmarks.microbenchmarks']))['schema']['fields']: 33 columns.append((row['name'], row['type'].lower())) 34 35SANITIZE = { 36 'integer': int, 37 'float': float, 38 'boolean': bool, 39 'string': str, 40 'timestamp': str, 41} 42 43if sys.argv[1] == '--schema': 44 print ',\n'.join('%s:%s' % (k, t.upper()) for k, t in columns) 45 sys.exit(0) 46 47with open(sys.argv[1]) as f: 48 js = json.loads(f.read()) 49 50if len(sys.argv) > 2: 51 with open(sys.argv[2]) as f: 52 js2 = json.loads(f.read()) 53else: 54 js2 = None 55 56# TODO(jtattermusch): write directly to a file instead of stdout 57writer = csv.DictWriter(sys.stdout, [c for c, t in columns]) 58 59for row in bm_json.expand_json(js, js2): 60 sane_row = {} 61 for name, sql_type in columns: 62 if name in row: 63 if row[name] == '': continue 64 sane_row[name] = SANITIZE[sql_type](row[name]) 65 writer.writerow(sane_row) 66