#Alejandro Bustelo
#55882514
#This file reads the timing log line by line, adding the total times in each of the two categories (TS and TJ) and then dividing by the total number of lines read, getting average times, in nanoseconds.
#Place this script and the timing.log file generated by search servlet in same directory, then, with Python 3 installed on the system, run the following command:
#py -3 average_time.py

def main():
	totalTStime = 0;
	totalTJtime = 0;
	totalLines = 0;
	lineContents = []
	
	print("Average TS and TJ times in timing.log")
	print("")
	with open("timing.log", newline='\n') as logfile:
		for line in logfile:
			lineContents = line.split(":")
			
			tsTime = lineContents[1].strip()
			tjTime = lineContents[3].strip()
			
			totalTStime = totalTStime + int(tsTime)
			totalTJtime = totalTJtime + int(tjTime)
			totalLines = totalLines + 1
			
			ts = str(totalLines) + ": TS: " + tsTime
			
			spaces = 20 - len(ts)
			if (spaces <= 0):
				spaces = spaces + (-spaces + 1)
			space = ""
			for x in range(0, spaces):
				space = space + " "
			
			tj = "TJ: " + tjTime
			
			print(ts + space + tj)
	if (totalLines > 0):
		print("")
		print("TS Average: " + str(totalTStime/totalLines) + " ns, TJ Average: " + str(totalTJtime/totalLines) + " ns")
		print("")
		print("TS Average: " + str((totalTStime/totalLines)/1000000) + " ms, TJ Average: " + str((totalTJtime/totalLines)/1000000) + " ms")
if __name__ == '__main__':
	main()