brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · f0bace8 Raw
51 lines · plain
1#!/usr/bin/env python32 3import argparse4import csv5import json6import sys7 8def 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 results24    headers = ['Benchmark', args.timing]25    rows = []26    for file in args.filename:27        js = json.load(file)28        for bm in js['benchmarks']:29            if args.timing not in bm:30                raise RuntimeError(f'Benchmark does not contain key for {args.timing}: {bm}')31            row = [bm['name'], bm[args.timing]]32            rows.append(row)33 34    # Print the results in the right format35    if args.output_format == 'csv':36        writer = csv.writer(sys.stdout)37        writer.writerow(headers)38        for row in rows:39            writer.writerow(row)40    elif args.output_format == 'lnt':41        benchmark = headers.index('Benchmark')42        time = headers.index(args.timing)43        for row in rows:44            # LNT format uses '.' to separate the benchmark name from the metric, and ' '45            # to separate the benchmark name + metric from the numerical value. Escape both.46            escaped = row[benchmark].replace(".", "_").replace(" ", "_")47            print(f'{escaped}.execution_time {row[time]}')48 49if __name__ == '__main__':50    main(sys.argv[1:])51