#!bin/python3
import sys
import os

if len(sys.argv) < 2:
    print("Please pass the name of the benchmark target as an argument!")
    exit(1)

target = sys.argv[1]
print('Comparing current modifications for benchmark target ' + target)

print('Executing old version...')
git_stash_result = os.popen('git stash push').read()
if 'No local changes' in git_stash_result:
    print('No local changes, nothing to compare, exiting!')
    exit(1)
print(git_stash_result)
print(os.popen('cd cmake-build-release; make ' + target).read())
before = os.popen('nice -20 ./cmake-build-release/bin/' + target).read()
print(os.popen('git stash pop').read())

print('Executing current version...')
print(os.popen('cd cmake-build-release; make ' + target).read())
current = os.popen('nice -20 ./cmake-build-release/bin/' + target).read()

print('=======================================================')
current = [float(value) for value in current.split(',')]
before = [float(value) for value in before.split(',')]
current.append(sum(current))
before.append(sum(before))


def formate_change(change):
    if change > 1.05:
        color = '31'
    elif change < 0.95:
        color = '32'
    else:
        color = '30'

    return '\033[1;' + color + ';40m %9.2f' % (change * 100) + '  %'


format_string = '|'.join(['%10.2f us'] * len(current))
print('old     | ' + format_string % tuple(before))
print('new     | ' + format_string % tuple(current))
change = [c / b for b, c in zip(before, current)]
formated_change = '|'.join(list(map(formate_change, change)))
print('change  |' + formated_change)
