import sys
import commands
import re


home		= "../../"

memusage	= home + "bin/memusage/memusage.sh"
errFile		= "err.txt"
reads		= home + "data/SRR006387.fastq_1.00000"
ref		= home + "data/chr22.fa"
iterations	= 5
bin		= home + "bin/razers3OA"
args		= "-id -v -i 96 -o delete.txt"


def processTime(timeout):
	m0 = re.search('real\s+(\d+)m(\d+\.\d+)s', timeout)
	m1 = re.search('user\s+(\d+)m(\d+\.\d+)s', timeout)
	m2 = re.search('sys\s+(\d+)m(\d+\.\d+)s', timeout)

	minutesReal = int(m0.group(1))
        secondsReal = float(m0.group(2)) + 60 * minutesReal

	minutes = int(m1.group(1)) + int(m2.group(1))
	seconds = float(m1.group(2)) + float(m2.group(2)) + 60 * minutes
	return (seconds, secondsReal)
	
def processMem(memout):
	m = re.search('heap peak: (\d+)', memout)
	return int(m.group(1))
	
def timeAndMem(cmd):
	#wrap time and mem around command
	cmd = "time " + memusage + " " + cmd + " 2> " + errFile

	# measure the time several times and get average
	sum = 0
	sumReal = 0
	for i in range(iterations):
		out = commands.getoutput(cmd)
		(t, tReal) = processTime(out)
		sum += t
		sumReal += tReal
	print("time: " + str(sum / iterations) + " sec (real: " + str(sumReal / iterations) + " sec)")
	
	# memory usage stays the same only need to get it once
	memout = commands.getoutput("cat " + errFile + " | grep Memory")
	myBytes = processMem(memout)
	print("mem:  " + str(myBytes) + " bytes")
	
def runScript():
	print("ref: " + ref + ", reads: " + reads + "\n")

	loadFactors = ["1.6", "1.8", "2.0"]
	qs = []
	for i in [11, 14, 16, 18, 20, 23]:
		qs.append("1" * i)
	
	for lf in loadFactors:
		for q in qs:
			cmd = bin + " " + args + " -s " + q + " -lf " + lf + " " + ref + " " + reads
			
			print("==========================\nq: " + str(len(q)) + ", loadfactor: " + lf)
			#print(cmd)

			timeAndMem(cmd)

if __name__ == '__main__':

	runScript()