#!/usr/bin/python

import os
import sys
import re
import statistics

where = '.'
if len(sys.argv) > 1:
    where = sys.argv[1]

dir_postfix = '-iozone-logs'
dirs = [d.name for d in os.scandir(where) if d.name.endswith(dir_postfix) and d.is_dir()]
match_log = re.compile(r'\s+(?P<key>Children.*)\s+=\s+(?P<val>\d+\.\d{2}) kB')
stats = {}

def filter_log(logfile):
    with open(os.path.join(path, logfile)) as log:
        for l in log:
            m = match_log.match(l)
            if m:
                insert_stat(m)

def insert_stat(match):
    key = match.group('key').strip()
    val = float(match.group('val').strip())
    if key in stats.keys():
        stats[key].append(val)
    else:
        stats[key] = [val]

for d in dirs:
    path = os.path.join(where,d)
    logs = [f.name for f in os.scandir(path) if 'log' in f.name and f.is_file()]
    for l in logs:
        filter_log(l)

for s in stats.keys():
    mean = statistics.mean(stats[s])
    print("%s = %0.2f" % (s, mean))
