import json
import os
import subprocess

def runBenchmark(testname, testsize, seed):
    with subprocess.Popen(
        ["./bin/main", "--test=%s" % testname, "--size=%d" % testsize,
         "--seed=%d" % seed], stdout=subprocess.PIPE) as p:
        output, _ = p.communicate()
        output = output.decode('UTF-8').split('\n')
        for line in output:
            print(line)
        output = float(output[2])
    return output


# Run all benchmarks
# output = "mult\n"
# for testsize in [1000, 2000, 3000, 4000, 5000]:
#     total_ms = 0.0
#     for seed in [1, 2, 3, 4, 5]:
#         total_ms += runBenchmark("mult", testsize, seed)
#     total_ms = total_ms / 5
#     output += "%s: %.2f\n" % (testsize, total_ms)

# print(output)

output = "solve\n"
for testsize in [1000, 2000, 3000, 4000, 5000]:
    total_ms = 0.0
    for seed in [1, 2, 3, 4, 5]:
        total_ms += runBenchmark("solve", testsize, seed)
    total_ms = total_ms / 5
    output += "%s: %.2f\n" % (testsize, total_ms)

print(output)

# output = "inv\n"
# for testsize in [1000, 1500, 2000, 2500, 3000]:
#     total_ms = []
#     for seed in [1, 2, 3, 4, 5]:
#         total_ms.append(runBenchmark("inv", testsize, seed))
#     total_ms = (sum(total_ms) - min(total_ms) - max(total_ms)) / 3.0
#     output += "%s: %.2f\n" % (testsize, total_ms)

# print(output)