#!/usr/bin/env python3

import sys
import json
import csv
import re

NAME_RE = re.compile(
        '^BM<' +
        '(?P<counter>\w+)(<(?P<array>\w*)>)?' +
        ',\s*' +
        '(?P<type>\w+?)_(?P<matrix>\w+?)(_(?P<height>\d+)x(?P<width>\d+))?' +
        '(_(?P<id>(\d|\w|_)*))?' +
        '>$')

TIME_MULTIPLIER = 1e-6

def load_run_info(d):
    info = NAME_RE.match(d['name']).groupdict()
    info['cpu time ms'] = round(d['cpu_time'] * TIME_MULTIPLIER, 2)
    return info

def read_json(json_file=None):
    json_file = json_file or sys.stdin
    data = json.load(json_file)
    return [load_run_info(bench) for bench in data['benchmarks']]

def write_csv(runs, csv_file=None):
    csv_file = csv_file or sys.stdout
    writer = csv.DictWriter(csv_file,
            ['type', 'counter', 'array', 'matrix',
             'height', 'width', 'id', 'cpu time ms'])
    writer.writeheader()
    for r in runs:
        writer.writerow(r)

if __name__ == '__main__':
    runs = read_json()
    write_csv(runs)


@@ -121,16 +121,38 @@
 %3Ccounter
+_signature%3E(?P%3Ccounter
 %3E%5Cw+)(%3C(
@@ -167,16 +167,17 @@
 %3E%5Cw*)%3E)?
+)
 ' +%0A    
@@ -185,24 +185,54 @@
    ',%5Cs*' +%0A
+        '(?P%3Ctest_signature%3E'%0A
         '(?P
@@ -341,16 +341,17 @@
        '
+)
 %3E$')%0A%0ATI
@@ -510,16 +510,86 @@
 IER, 2)%0A
+    info%5B'real time ms'%5D = round(d%5B'real_time'%5D * TIME_MULTIPLIER, 2)%0A
     retu
@@ -602,24 +602,35 @@
 o%0A%0Adef read_
+benchmarks_
 json(json_fi
@@ -783,24 +783,35 @@
 %0A%0Adef write_
+benchmarks_
 csv(runs, cs
@@ -1003,16 +1003,51 @@
 ime ms'%5D
+,%0A            extrasaction='ignore'
 )%0A    wr
@@ -1116,67 +1116,726 @@
 r)%0A%0A
-if __name__ == '__main__':%0A    runs = read_json()%0A    write
+def update_best(runs, filepath):%0A    try:%0A        with open(filepath, 'r') as fin:%0A            best_runs = json.load(fin)%0A    except FileNotFoundError:%0A        best_runs = %7B%7D%0A%0A    for r in runs:%0A        t = r%5B'test_signature'%5D%0A        if t not in best_runs or best_runs%5Bt%5D%5B'time'%5D %3E r%5B'real time ms'%5D:%0A            best_runs%5Bt%5D = %7B%0A                'time': r%5B'real time ms'%5D,%0A                'counter_signature': r%5B'counter_signature'%5D,%0A            %7D%0A    with open(filepath, 'w') as fout:%0A        json.dump(best_runs, fout, sort_keys=True, indent=2)%0A%0Aif __name__ == '__main__':%0A    runs = read_benchmarks_json()%0A    if '--pre-commit' in sys.argv:%0A        update_best(runs, 'best.json')%0A    else:%0A        write_benchmarks
 _csv

Add --pre-commit functionality to report