Files @ 6a0ab4fe9f5e
Branch filter:

Location: Morevna/src/benchmark.py

Laman
changed naming to snake case. sadly no love for camel case in this world
from time import time
import socket
import threading

from hashtree import HashTree


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


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


def selected_read():
	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 less_selected_read():
	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 short_sockets():
	def _server():
		server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		server_sock.bind(("", 12329))
		server_sock.listen(1)

		for i in range(10000):
			sock, address = server_sock.accept()
			with sock.makefile(mode="rb") as fr, sock.makefile(mode="wb") as fw:
				fr.readline()
			sock.shutdown(socket.SHUT_RDWR)
			sock.close()
		server_sock.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 long_sockets():
	def _server():
		server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
		server_sock.bind(("", 12330))
		server_sock.listen(1)
		sock, address = server_sock.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()
		server_sock.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()


# time_f(full_read) # 85.40341448783875 s
# time_f(selected_read) # 6.774365186691284 s
# time_f(less_selected_read) # 5.930811405181885 s

# time_f(short_sockets) # 3.928339719772339 s
# time_f(long_sockets) # 0.15576839447021484 s