diff options
author | Julian T <julian@jtle.dk> | 2020-09-15 18:51:46 +0200 |
---|---|---|
committer | Julian T <julian@jtle.dk> | 2020-09-15 18:51:46 +0200 |
commit | d285183c42f5d377b197bd0efab3bb20b240ef34 (patch) | |
tree | 7c0754c4b36e6cc18a3ee9353ab605511ce53446 /sem5 | |
parent | 4bfbc29c7ea0b711f96346deb25dde0b2298ecde (diff) |
Added assignments for net
Diffstat (limited to 'sem5')
-rw-r--r-- | sem5/net/mm3/hosts.json | 10 | ||||
-rw-r--r-- | sem5/net/mm3/out.pdf | bin | 0 -> 12491 bytes | |||
-rw-r--r-- | sem5/net/mm3/pingplot.py | 90 |
3 files changed, 100 insertions, 0 deletions
diff --git a/sem5/net/mm3/hosts.json b/sem5/net/mm3/hosts.json new file mode 100644 index 0000000..58d520e --- /dev/null +++ b/sem5/net/mm3/hosts.json @@ -0,0 +1,10 @@ +[ + {"host": "dtu.dk", "dist": 223.31 }, + {"host": "mit.edu", "dist": 5739.88 }, + {"host": "usyd.edu.au", "dist": 16133.52 }, + {"host": "cambridge.org", "dist": 827.65 }, + {"host": "tudelft.nl", "dist": 664.49 }, + {"host": "dotsrc.org", "dist": 0.0 }, + {"host": "berkeley.edu", "dist": 8550.47 } +] + diff --git a/sem5/net/mm3/out.pdf b/sem5/net/mm3/out.pdf Binary files differnew file mode 100644 index 0000000..d35b43b --- /dev/null +++ b/sem5/net/mm3/out.pdf diff --git a/sem5/net/mm3/pingplot.py b/sem5/net/mm3/pingplot.py new file mode 100644 index 0000000..8811d2e --- /dev/null +++ b/sem5/net/mm3/pingplot.py @@ -0,0 +1,90 @@ + +# We are not allowed to send ICMP packets without root +# and will therefore just parse the output from standard ping. +# This requires the ping command to keep going forever +import subprocess +import sys +import numpy as np +import json +import argparse +import re +import matplotlib.pyplot as plt + +# group 1: number of bytes +# group 2: destination +# group 3: icmp_seq +# group 4: ttl +# group 5: round time (This we want to plot) +pingre = re.compile("(\d*) bytes from (.*): icmp_seq=(\d*) ttl=(\d*) time=(.*)") + +# Lets hope we only get milliseconds +timere = re.compile("([\d\.]*) ms") + +parser = argparse.ArgumentParser() +parser.add_argument("--config", "-c", default="hosts.json") +parser.add_argument("--samples", "-s", default=10, type=int) +parser.add_argument("--out", "-o", default=None) + +args = parser.parse_args() + +hosts = {} +with open(args.config, "r") as f: + hosts = json.load(f) + +# Pings the specified host, samples number of times and returns the result +# as a numpy array. +# Runs the hook function on every response with arguments: destination, index, ttl, latency +def pinghost(host, samples, hook=None): + # Well there goes cross platform + cmd = subprocess.Popen(f"ping -c {samples} {host}", shell=True, stdout=subprocess.PIPE) + + res = np.empty(samples) + + index = 0 + for line in cmd.stdout: + line = line.decode("utf-8") + m = pingre.match(str(line)) + if m is None: + continue + ms = m[5] + tm = timere.match(ms) + if tm is None: + print(f"Could not parse time {ms}, is this the correct version of ping?", file=sys.stderr) + sys.exit(1) + res[index] = float(tm[1]) + index += 1 + + if hook: + hook(m[2], index, m[4], m[5]) + + if index != samples: + print(f"Got {index} samples which is not the {samples} we asked for, is this the correct version of ping?", file=sys.stderr) + sys.exit(1) + + return res + +def statprint(dest, index, ttl, latency): + print(f"ping {index} for {dest} with latency: {latency}") + +x = np.empty(len(hosts)) +y = np.empty(len(hosts)) + +for i, host in enumerate(hosts): + print(host) + res = pinghost(host["host"], args.samples, statprint) + y[i] = np.average(res) + print(f"Average: {y[i]}") + + x[i] = host["dist"] + +sort = x.argsort() +x = x[sort] +y = y[sort] + +plt.xlabel("distance [km]") +plt.ylabel(f"gennemsnitlig pingtid over {args.samples} målinger [ms]") +plt.plot(x, y) +if args.out: + plt.savefig(args.out, dpi=300) +else: + plt.show() |