#! /usr/bin/python

import os, time

CLEAN_CMD = 'rm -f *.o *.hi *.bin'
BUILD_CMD = 'ghc -main-is main%s --make sortbench.hs -o %s'
BINARY_NAME = 'sortbench%s.bin'
TYPES = ['OnSorted','OnRevsorted','OnRandom']

def clean():
    os.system(CLEAN_CMD)

def build( benchType ):
    binName = BINARY_NAME % (benchType,)
    os.system(BUILD_CMD % (benchType, binName))

def test( benchType ):
    print 'Benchmark type:', benchType
    
    clean()
    build(benchType)
    binName = BINARY_NAME % (benchType,)
    DATA_LENGTH = 1000000 # beware, 3000000 gives ~700Mb memory consumption for Int
    TEST_CASES = 10
    dateNow = "_".join(map(str,time.localtime()[:-3]))[2:] # ugly, but works
    output_log = 'sortbench%s_%s.txt' % (benchType,dateNow)

    start = time.time()
    for i in range(TEST_CASES):
        print "%d/%d" % (i+1,TEST_CASES)
        os.system('./%s %d 1>/dev/null 2>>%s' % (binName, DATA_LENGTH, output_log) )
    stop = time.time()

    print 'Total time:',stop-start

    dc = {}
    for line in open(output_log):
        (t,al) = eval(line)
        dc.setdefault( al, 0 );
        dc[al] += t

    print "Scaled vs best.:"

    g = list(dc.iteritems())
    g.sort(key = lambda (x,y) : y )
    for el in g:
        print tuple([el[0],float(el[1]) / g[0][1]])

def main():
    for t in TYPES:
        test(t)

if __name__ == '__main__':
    main()
