Commit 04c2f412 authored by Andreas Schmidt's avatar Andreas Schmidt
Browse files

Extract evaluation classes.

parent a8d873d8
import sys
sys.path.insert(0, "./build")
import prrt
import time as time
import datetime
import threading
import subprocess
import numpy as np
import tests.perf as perf
class receiverThread(threading.Thread):
def __init__(self, seqnoDigits):
threading.Thread.__init__(self)
self.sock = prrt.PrrtSocket(7000, False)
self.stats = {}
self.received = set()
self.duplicates = 0
self.seqnoDigits = seqnoDigits
self.receivedPackets = {}
# TODO: create proper arrays
def run(self):
while(True):
len, data = self.sock.recv()
v = data[:len].decode('UTF-8')
seqno = v[:self.seqnoDigits]
if seqno in self.received:
self.duplicates = self.duplicates + 1
else:
self.received.update(set([seqno]))
self.receivedPackets[seqno] = datetime.datetime.now()
time.sleep(0.000001)
class senderThread(threading.Thread):
def __init__(self, seqnoDigits, packetCount):
threading.Thread.__init__(self)
self.sock = prrt.PrrtSocket(7005, True)
self.packetCount = packetCount
self.seqnoDigits = seqnoDigits
self.sentPackets = {}
# TODO: create proper arrays
def run(self):
self.sock.connect("127.0.0.1", 7000)
for i in range(self.packetCount):
d = str(i).zfill(self.seqnoDigits)
self.sentPackets[d] = datetime.datetime.now()
self.sock.send(d)
time.sleep(0.001)
self.sock.close()
class TestSetup(object):
def __init__(self,packets=20,delay=1,loss=0,duplicate=0,reorder=0):
self.packets = packets
self.delay = delay
self.loss = loss
self.duplicate = duplicate
self.reorder = reorder
def netem_config(self):
return "delay %dms loss %s%% duplicate %d%% reorder %d%%" % (self.delay, self.loss, self.duplicate, self.reorder)
def run(self):
subprocess.run("tc qdisc add dev lo root netem " + self.netem_config(), shell=True)
seqnoDigits = 15
recvThread = receiverThread(seqnoDigits)
recvThread.daemon = True
sendThread = senderThread(seqnoDigits, self.packets)
recvThread.start()
sendThread.run()
time.sleep(10)
count = len(recvThread.received)
delays = np.ndarray((self.packets))
for i in range(self.packets):
seqno = str(i).zfill(seqnoDigits)
deltaTime = recvThread.receivedPackets[seqno] - sendThread.sentPackets[seqno]
delays[i] = deltaTime.total_seconds()
print("Residual Loss:", 1 - (count / self.packets), "% (received", count, "of", self.packets,")")
print("Duplicates:", recvThread.duplicates)
print("Delay Profile:", np.mean(delays), "+-", np.std(delays), "[", np.min(delays), "-", np.max(delays), "]")
subprocess.run("tc qdisc del dev lo root", shell=True, check=True)
def run_setup_and_report(setup):
results = setup.run()
print("Setup:\n ", setup)
print("Results:\n " + str(results).replace("\n","\n "))
if __name__ == "__main__":
# Packet Count Works: 2^15; Segfault at: 2^16
setup = TestSetup(packets=2**8,delay=200,loss=0,reorder=5,duplicate=1)
setup.run()
\ No newline at end of file
setups = [
perf.TestSetup(packets=2**8,delay=0,loss=0,reorder=0,duplicate=1)
]
for setup in setups:
run_setup_and_report(setup)
\ No newline at end of file
__pycache__/
\ No newline at end of file
import subprocess
import multiprocessing
import time as time
import datetime
import threading
import prrt
import numpy as np
class ReceiverThread(threading.Thread):
def __init__(self, seqnoDigits):
threading.Thread.__init__(self)
self.sock = prrt.PrrtSocket(7000, False)
self.running = True
self.stats = {}
self.received = set()
self.duplicates = 0
self.seqnoDigits = seqnoDigits
self.receivedPackets = {}
def run(self):
while(1):
len, data = self.sock.recv()
v = data[:len].decode('UTF-8')
seqno = v[:self.seqnoDigits]
if seqno in self.received:
self.duplicates = self.duplicates + 1
else:
self.received.update(set([seqno]))
self.receivedPackets[seqno] = datetime.datetime.now()
time.sleep(0.000001)
class SenderThread(threading.Thread):
def __init__(self, seqnoDigits, packetCount):
threading.Thread.__init__(self)
self.sock = prrt.PrrtSocket(7005, True)
self.packetCount = packetCount
self.seqnoDigits = seqnoDigits
self.sentPackets = {}
# TODO: create proper arrays
def run(self):
self.sock.connect("127.0.0.1", 7000)
for i in range(self.packetCount):
d = str(i).zfill(self.seqnoDigits)
self.sentPackets[d] = datetime.datetime.now()
self.sock.send(d)
time.sleep(0.001)
self.sock.close()
class TestResults(object):
def __init__(self):
self.loss = 0
self.packetsSent = 0
self.packetsReceived = 0
self.duplicates = 0
self.delays = None
def __str__(self):
plr = 1 - (self.packetsReceived / self.packetsSent)
loss = "Residual Loss: %f%% (received %d of %d)" % (plr, self.packetsReceived, self.packetsSent)
duplicates = "Duplicates: %d" % (self.duplicates)
delays = "Delay Profile: %f+-%f [%f - %f]" % (np.mean(self.delays), np.std(self.delays), np.min(self.delays), np.max(self.delays))
return "%s\n%s\n%s" % (loss, duplicates, delays)
class TestSetup(object):
def __init__(self,packets=20,delay=1,loss=0,duplicate=0,reorder=0):
self.packets = packets
self.delay = delay
self.loss = loss
self.duplicate = duplicate
self.reorder = reorder
def netem_config(self):
return "delay %dms loss %s%% duplicate %d%% reorder %d%%" % (self.delay, self.loss, self.duplicate, self.reorder)
def run(self):
subprocess.run("tc qdisc add dev lo root netem " + self.netem_config(), shell=True)
seqnoDigits = 15
recvThread = ReceiverThread(seqnoDigits)
recvThread.daemon = True
sendThread = SenderThread(seqnoDigits, self.packets)
recvThread.start()
sendThread.run()
# TODO: this time should be packet count and delay dependent
time.sleep(5)
count = len(recvThread.received)
delays = np.ndarray((self.packets))
for i in range(self.packets):
seqno = str(i).zfill(seqnoDigits)
deltaTime = recvThread.receivedPackets[seqno] - sendThread.sentPackets[seqno]
delays[i] = deltaTime.total_seconds()
subprocess.run("tc qdisc del dev lo root", shell=True, check=True)
results = TestResults()
results.packetsSent = self.packets
results.packetsReceived = count
results.duplicates = recvThread.duplicates
results.delays = delays
return results
def __str__(self):
return self.netem_config()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment