1#!/usr/bin/env python3
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 csv
21import json
22import subprocess
23import sys
24
25import bm_json
26
27columns = []
28
29for row in json.loads(
30        # TODO(jtattermusch): make sure the dataset name is not hardcoded
31        subprocess.check_output(
32            ['bq', '--format=json', 'show',
33             'microbenchmarks.microbenchmarks']))['schema']['fields']:
34    columns.append((row['name'], row['type'].lower()))
35
36SANITIZE = {
37    'integer': int,
38    'float': float,
39    'boolean': bool,
40    'string': str,
41    'timestamp': str,
42}
43
44# TODO(jtattermusch): add proper argparse argument, rather than trying
45# to emulate with manual argv inspection.
46if sys.argv[1] == '--schema':
47    print(',\n'.join('%s:%s' % (k, t.upper()) for k, t in columns))
48    sys.exit(0)
49
50with open(sys.argv[1]) as f:
51    js = json.loads(f.read())
52
53if len(sys.argv) > 2:
54    with open(sys.argv[2]) as f:
55        js2 = json.loads(f.read())
56else:
57    js2 = None
58
59# TODO(jtattermusch): write directly to a file instead of stdout
60writer = csv.DictWriter(sys.stdout, [c for c, t in columns])
61
62for row in bm_json.expand_json(js, js2):
63    sane_row = {}
64    for name, sql_type in columns:
65        if name in row:
66            if row[name] == '':
67                continue
68            sane_row[name] = SANITIZE[sql_type](row[name])
69    writer.writerow(sane_row)
70