def testCase3():
	post_delay = 0.3
	post_count = 200
	post_size = 700

	def recordPDML(clientsocket, listener):

		clientsocket.send("cache\\clear")
		time.sleep(const_inter_cmd_delay)
		# start capturing
		filename = "recording_" + datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
		
		th = prepare_recording(filename)

		cnt = 0
		for i in range(post_count):
			clientsocket.send("post\\" + str(post_size))
			time.sleep(post_delay)
			cnt += 1

		print "Sent", cnt, "POST messages"
		
		waitForTCP()
		
		finalize_recording(th)

		return filename + ".pdml"

	server, clientsocket, listener = startup()

	filewriter.writeDelimiter("CASE 3 measure")

	## HTTP2
	clientsocket.send("http2\\enable")
	time.sleep(const_inter_cmd_delay)
	prefix = "case3.http2"
	filewriter.writeResult(prefix + ".post.count", post_count)
	filewriter.writeResult(prefix + ".post.size", post_size)
	pdmlFile = recordPDML(clientsocket, listener)
	analyze.analyzePDML(pdmlFile, prefix)
	if delete_traces:
		os.remove(pdmlFile)

	## HTTP1
	clientsocket.send("http2\\disable")
	time.sleep(const_inter_cmd_delay)
	prefix = "case3.http1"
	filewriter.writeResult(prefix + ".post.count", post_count)
	filewriter.writeResult(prefix + ".post.size", post_size)
	pdmlFile = recordPDML(clientsocket, listener)
	analyze.analyzePDML(pdmlFile, prefix)
	if delete_traces:
		os.remove(pdmlFile)

	shutdown(server, clientsocket, listener)
def testCase2():

	file_delay = 7
	file_count = 12

	def recordPDML(clientsocket, listener):

		clientsocket.send("cache\\clear")
		time.sleep(const_inter_cmd_delay)
		# start capture
		filename = "recording_" + datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
		
		th = prepare_recording(filename)

		for i in range(file_count):
			getfile = str(i+1) + ".file"
			print "Retrieving file", getfile
			clientsocket.send("get\\" + getfile)
			time.sleep(file_delay)

		waitForTCP()
		
		finalize_recording(th)

		return filename + ".pdml"


	server, clientsocket, listener = startup()

	filewriter.writeDelimiter("CASE 2 measure")

	## HTTP2
	clientsocket.send("http2\\enable")
	time.sleep(const_inter_cmd_delay)
	prefix = "case2.http2"
	pdmlFile = recordPDML(clientsocket, listener)
	analyze.analyzePDML(pdmlFile, prefix)
	if delete_traces:
		os.remove(pdmlFile)

	## HTTP1
	clientsocket.send("http2\\disable")
	time.sleep(const_inter_cmd_delay)
	prefix = "case2.http1"
	pdmlFile = recordPDML(clientsocket, listener)
	analyze.analyzePDML(pdmlFile, prefix)
	if delete_traces:
		os.remove(pdmlFile)

	shutdown(server, clientsocket, listener)
def testCase1(modes, testCount = 1):

	htmlPath = case1_target_page

	def measurePLT(clientsocket, listener, count, useCache):
		avg = 0
		resultList = []

		if useCache:
			clientsocket.send("tab\\" + htmlPath)
			time.sleep(const_inter_cmd_delay)
			listener.waitForPLT()
			clientsocket.send("tab\\close")
			time.sleep(const_inter_cmd_delay)
			waitForTCP()
		else:
			clientsocket.send("cache\\clear")
			time.sleep(const_inter_cmd_delay)

		cnt = 0
		for i in range(count):
			print "--- Run #" + str(i+1) + " for PLT measure ---"
			listener.resetPLT()
			clientsocket.send("tab\\" + htmlPath)
			time.sleep(const_inter_cmd_delay)
			val = int(listener.waitForPLT())
			#print "DEBUG: val:", val
			avg += val
			resultList.append(val)
			clientsocket.send("tab\\close")
			waitForTCP()
			cnt += 1
			if not useCache:
				clientsocket.send("cache\\clear")
				time.sleep(const_inter_cmd_delay)

		avg = avg / cnt
		cacheMsg = useCache and "(while using Cache)" or "(while NOT using Cache)"
		print "Avg over", cnt, "times:", avg, cacheMsg
		return cnt, avg, resultList
	
	def recordPDML(clientsocket, listener, useCache):

		if useCache:
			clientsocket.send("tab\\" + htmlPath)
			time.sleep(const_inter_cmd_delay)
			listener.waitForPLT()
			clientsocket.send("tab\\close")
			time.sleep(const_inter_cmd_delay)
			waitForTCP()
		else:
			clientsocket.send("cache\\clear")
			time.sleep(const_inter_cmd_delay)

		# start capture
		filename = "recording_" + datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
		
		th = prepare_recording(filename)

		listener.resetPLT()
		clientsocket.send("tab\\" + htmlPath)
		time.sleep(const_inter_cmd_delay)
		listener.waitForPLT()
		print "PLT received"

		clientsocket.send("tab\\close")
		time.sleep(const_inter_cmd_delay)
		
		waitForTCP()
		
		finalize_recording(th)

		return filename + ".pdml"

	if "plt" in modes:
		server, clientsocket, listener = startup()

		### SCRIPT MAIN EXECUTION

		pltLoopCount = testCount

		filewriter.writeDelimiter("PLT measure")

		clientsocket.send("http2\\enable")
		time.sleep(const_inter_cmd_delay)
		print "---"
		print "Measuring PLT via HTTP2"
		cnt, avg, resultList = measurePLT(clientsocket, listener, pltLoopCount, False)
		for result in resultList:
			filewriter.writeResult("case1.http2.nocache.plt.value", result)
		filewriter.writeResult("case1.http2.nocache.plt.count", cnt)
		filewriter.writeResult("case1.http2.nocache.plt.average", avg)

		clientsocket.send("http2\\disable")
		time.sleep(const_inter_cmd_delay)
		print "---"
		print "Measuring PLT via HTTP1.1"
		cnt, avg, resultList = measurePLT(clientsocket, listener, pltLoopCount, False)
		for result in resultList:
			filewriter.writeResult("case1.http1.nocache.plt.value", result)
		filewriter.writeResult("case1.http1.nocache.plt.count", cnt)
		filewriter.writeResult("case1.http1.nocache.plt.average", avg)

		clientsocket.send("http2\\enable")
		time.sleep(const_inter_cmd_delay)
		print "---"
		print "Measuring PLT via HTTP2"
		cnt, avg, resultList = measurePLT(clientsocket, listener, pltLoopCount, True)
		for result in resultList:
			filewriter.writeResult("case1.http2.cache.plt.value", result)
		filewriter.writeResult("case1.http2.cache.plt.count", cnt)
		filewriter.writeResult("case1.http2.cache.plt.average", avg)

		clientsocket.send("http2\\disable")
		time.sleep(const_inter_cmd_delay)
		print "---"
		print "Measuring PLT via HTTP1.1"
		cnt, avg, resultList = measurePLT(clientsocket, listener, pltLoopCount, True)
		for result in resultList:
			filewriter.writeResult("case1.http1.cache.plt.value", result)
		filewriter.writeResult("case1.http1.cache.plt.count", cnt)
		filewriter.writeResult("case1.http1.cache.plt.average", avg)

		shutdown(server, clientsocket, listener)

		time.sleep(3) # static sleep time between phases

	if "other" in modes:

		server, clientsocket, listener = startup()

		filewriter.writeDelimiter("CASE 1 measure")

		## HTTP2 NOCACHE
		clientsocket.send("http2\\enable")
		time.sleep(const_inter_cmd_delay)
		prefix = "case1.http2.nocache"
		pdmlFile = recordPDML(clientsocket, listener, False)
		analyze.analyzePDML(pdmlFile, prefix)
		if delete_traces:
			os.remove(pdmlFile)

		## HTTP2 CACHE
		clientsocket.send("http2\\enable")
		time.sleep(const_inter_cmd_delay)
		prefix = "case1.http2.cache"
		pdmlFile = recordPDML(clientsocket, listener, True)
		analyze.analyzePDML(pdmlFile, prefix)
		if delete_traces:
			os.remove(pdmlFile)

		## HTTP1 NOCACHE
		clientsocket.send("http2\\disable")
		time.sleep(const_inter_cmd_delay)
		prefix = "case1.http1.nocache"
		pdmlFile = recordPDML(clientsocket, listener, False)
		analyze.analyzePDML(pdmlFile, prefix)
		if delete_traces:
			os.remove(pdmlFile)

		## HTTP1 CACHE
		clientsocket.send("http2\\disable")
		time.sleep(const_inter_cmd_delay)
		prefix = "case1.http1.cache"
		pdmlFile = recordPDML(clientsocket, listener, True)
		analyze.analyzePDML(pdmlFile, prefix)
		if delete_traces:
			os.remove(pdmlFile)

		shutdown(server, clientsocket, listener)