Files @ e6f9a4843e49
Branch filter:

Location: Morevna/src/benchmark.py

Laman
configurable log path
from time import time
import socket
import threading

from hashtree import HashTree


def timeF(f):
	start=time()
	f()
	end=time()
	print((end-start),"s")


def fullRead():
	block=True
	with open("/home/laman/ext2.img",mode="rb") as f:
		while block:
			block=f.read(HashTree.BLOCK_SIZE)


def selectedRead():
	with open("/home/laman/blocks.txt") as f:
		blocks=[int(x) for x in f]
	with open("/home/laman/ext2.img",mode="rb") as f:
		i1=-1
		for i2 in blocks:
			if i1+1!=i2:
				f.seek(i2*HashTree.BLOCK_SIZE)
			block=f.read(HashTree.BLOCK_SIZE)
			i1=i2


def lessSelectedRead():
	with open("/home/laman/blocks.txt") as f:
		blocks=[int(x) for x in f]
	with open("/home/laman/ext2.img",mode="rb") as f:
		i1=-1
		for i2 in blocks:
			if i2<=i1+8:
				block=f.read(HashTree.BLOCK_SIZE*(i2-i1))
			else:
				f.seek(i2*HashTree.BLOCK_SIZE)
				block=f.read(HashTree.BLOCK_SIZE)
			i1=i2


def shortSockets():
	def _server():
		serverSock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		serverSock.bind(("",12329))
		serverSock.listen(1)

		for i in range(10000):
			sock, address = serverSock.accept()
			with sock.makefile(mode="rb") as fr, sock.makefile(mode="wb") as fw:
				fr.readline()
			sock.shutdown(socket.SHUT_RDWR)
			sock.close()
		serverSock.close()

	def _client():
		for i in range(10000):
			sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
			sock.connect(("127.0.0.1", 12329))
			with sock.makefile(mode="rb") as fr, sock.makefile(mode="wb") as fw:
				fw.write(b"x"*4096+b"\n")
				fw.flush()
			sock.shutdown(socket.SHUT_RDWR)
			sock.close()

	s=threading.Thread(target=_server)
	s.start()
	c=threading.Thread(target=_client)
	c.start()
	s.join()
	c.join()


def longSockets():
	def _server():
		serverSock=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		serverSock.bind(("",12330))
		serverSock.listen(1)
		sock, address = serverSock.accept()

		with sock.makefile(mode="rb") as fr, sock.makefile(mode="wb") as fw:
			for i in range(10000):
				fr.readline()

		sock.shutdown(socket.SHUT_RDWR)
		sock.close()
		serverSock.close()

	def _client():
		sock=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
		sock.connect(("127.0.0.1", 12330))
		with sock.makefile(mode="rb") as fr, sock.makefile(mode="wb") as fw:
			for i in range(10000):
					fw.write(b"x"*4096+b"\n")
					fw.flush()
		sock.shutdown(socket.SHUT_RDWR)
		sock.close()

	s=threading.Thread(target=_server)
	s.start()
	c=threading.Thread(target=_client)
	c.start()
	s.join()
	c.join()


# timeF(fullRead) # 85.40341448783875 s
# timeF(selectedRead) # 6.774365186691284 s
# timeF(lessSelectedRead) # 5.930811405181885 s

# timeF(shortSockets) # 3.928339719772339 s
# timeF(longSockets) # 0.15576839447021484 s