bm2bq.py 1.9 KB

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