|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import argparse |
| 4 | +import csv |
| 5 | +import json |
| 6 | +import sys |
| 7 | + |
| 8 | +def main(argv): |
| 9 | + parser = argparse.ArgumentParser( |
| 10 | + prog='parse-google-benchmark-results', |
| 11 | + description='Parse Google Benchmark result files (in JSON format) into CSV or LNT compatible output.') |
| 12 | + parser.add_argument('filename', type=argparse.FileType('r'), nargs='+', |
| 13 | + help='One of more JSON files to extract the results from. The results parsed from each ' |
| 14 | + 'file are concatenated together.') |
| 15 | + parser.add_argument('--timing', type=str, choices=['real_time', 'cpu_time'], default='real_time', |
| 16 | + help='The timing to extract from the Google Benchmark results. This can either be the ' |
| 17 | + '"real time" or the "CPU time". Default is "real time".') |
| 18 | + parser.add_argument('--output-format', type=str, choices=['csv', 'lnt'], default='csv', |
| 19 | + help='The desired output format for the data. `csv` is CSV format and `lnt` is a format compatible with ' |
| 20 | + '`lnt importreport` (see https://llvm.org/docs/lnt/importing_data.html#importing-data-in-a-text-file).') |
| 21 | + args = parser.parse_args(argv) |
| 22 | + |
| 23 | + # Parse the data from all files, aggregating the results |
| 24 | + headers = ['Benchmark', args.timing] |
| 25 | + rows = [] |
| 26 | + for file in args.filename: |
| 27 | + js = json.load(file) |
| 28 | + for bm in js['benchmarks']: |
| 29 | + row = [bm['name'], bm[args.timing]] |
| 30 | + rows.append(row) |
| 31 | + |
| 32 | + # Print the results in the right format |
| 33 | + if args.output_format == 'csv': |
| 34 | + writer = csv.writer(sys.stdout) |
| 35 | + writer.writerow(headers) |
| 36 | + for row in rows: |
| 37 | + writer.writerow(row) |
| 38 | + elif args.output_format == 'lnt': |
| 39 | + benchmark = headers.index('Benchmark') |
| 40 | + time = headers.index(args.timing) |
| 41 | + for row in rows: |
| 42 | + print(f'{row[benchmark].replace(".", "_")}.execution_time {row[time]}') |
| 43 | + |
| 44 | +if __name__ == '__main__': |
| 45 | + main(sys.argv[1:]) |
0 commit comments