#!/usr/bin/env python
#
# Very simple script for timing the execution of mplayer and gstreamer
#
# usage: script.py filename [filename2 filename3 ...]
# OL 02/2010
#############

import os, subprocess, sys

### Add more tests here: first entry of tuple is a tag (it's nicer when the names have the same
### length), second is the command, in which %s is replaced once by the filename
tests = [ ("mplayer_", "mplayer -really-quiet -ao alsa %s"),
          ("playbin2", "gst-launch-0.10 -q playbin2 audio-sink=\"alsasink\" uri=file://%s")
        ]

### The tests are performed for every filename, so they should accept any filetype

def test(cmd):
    nIt   = 3
    args  = cmd.split()
    print "Running %s %i times..." % (args[0], nIt) 
    start = os.times()
    ret   = map(lambda x: subprocess.call(args), xrange(nIt))
    stop  = os.times()
    
    if sum(ret) > 0: sys.stderr.write("At least one iteration of \"%s\" exited with non-zero return code!\n" % cmd);

    return map(lambda x: float(x[1]-x[0])/nIt, zip(start, stop))

def prettyOutput( resultData ):
    # not pretty, yet :D
    fn         = resultData[0]
    actualData = resultData[1:]
    strings    = [ "Playing of file: %s\nTag\tcpu [s]\tcpu [%%]" % fn ]
    map(lambda x: strings.append("%s\t%5.1f\t%5.1f" %(x[1][0], x[0][2], x[0][2]/x[0][4])), actualData)

    return "\n".join(strings) 

files = map (os.path.abspath, sys.argv[1:])
res   = map (lambda fn : [fn] + [ (test(i[1] % fn), i) for i in tests ], files)

print "\n\n".join(map(prettyOutput, res))

    

