Commit 6f899314 authored by Aymeric Bernard's avatar Aymeric Bernard
Browse files

Add Python script to plot graphs

parent 18cfa6e7
data/
output/
node_modules/
venv
__pycache__
*.pyc
......
import time
from typing import List
import matplotlib.pyplot as plt
import numpy as np
import scipy.stats as stats
def prettify_time_interval(start, end=None):
"""Display the time interval to be displayed on the plot"""
end = time.time() if end is None else end
sec_elapsed = end - start
h = int(sec_elapsed / (60 * 60))
m = int((sec_elapsed % (60 * 60)) / 60)
s = sec_elapsed % 60
if h > 0:
return f'{h}h {m}m {int(round(s))}s'
elif m > 0:
return f'{m}m{s:.01f}s'
elif s > 1:
return f'{s:.3f}s'
else:
return f'{s*1000:.3f}ms'
def plot_time_distribution_bar_graph(deltas: List[float], tmax: float = 200, yscale: str = None, save: str = None):
"""Actually plot the graph. Deltas in ms"""
bar_interval = 1
x_axis = np.arange(0, min(max(deltas), tmax) + bar_interval, bar_interval, dtype=int) // bar_interval
y_data = np.zeros(len(x_axis), dtype=int)
for d in deltas:
d = min(d, tmax)
y_index = round(d) // bar_interval
y_data[y_index] += 1
plt.figure(figsize=(24, 12))
if yscale is not None:
plt.yscale(yscale)
plt.bar(x_axis, y_data)
deltas_avg = np.average(deltas)
deltas_std = np.std(deltas)
plt.ylabel('Number of duplicates in one bar')
plt.xlabel(f'Delta intervals, each bar is {bar_interval} seconds wide')
plt.title(f'Time delta distribution, data on {len(deltas):,d} duplicates --- ' +
f'deltas avg {prettify_time_interval(0, deltas_avg)}, ' +
f'delta std {prettify_time_interval(0, deltas_std)}')
if save:
plt.savefig(save)
else:
plt.show()
def plot_time_distribution_raise(deltas: List[float], tmax: float = 200, yscale: str = None, save: str = None):
"""Actually plot the graph. Deltas in ms"""
deltas = np.asarray(sorted([min(d, tmax) for d in deltas]))
y_axis = np.arange(1, deltas.size + 1, dtype=float) / deltas.size
plt.figure(figsize=(16, 8))
if yscale is not None:
plt.yscale(yscale)
plt.plot(deltas, y_axis)
deltas_avg = np.average(deltas)
deltas_std = np.std(deltas)
plt.ylabel('Ratio of deltas below value')
plt.xlabel(f'Deltas (ms)')
plt.title(f'Time delta distribution, data on {deltas.size:,d} rtt --- ' +
f'deltas avg {prettify_time_interval(0, deltas_avg)}, ' +
f'delta std {prettify_time_interval(0, deltas_std)}')
if save:
plt.savefig(save)
else:
plt.show()
def plot_time_distribution_gauss(deltas: List[float], tmax: float = 200, save: str = None, title="RTT distribution"):
"""Actually plot the graph. Deltas in ms"""
deltas = [min(d, tmax) for d in deltas]
x1 = np.array(deltas)
xs = np.linspace(0, min(max(deltas), tmax), num=1000)
density = stats.kde.gaussian_kde(x1, bw_method=0.02) # 0.02
plt.figure(figsize=(16, 8))
plt.plot(x1, np.ones(x1.shape) / (4. * x1.size), '|', color="k", label='Data points (rescaled)')
plt.plot(xs, density(xs))
deltas_avg = np.average(deltas)
deltas_std = np.std(deltas)
plt.ylabel('Value')
plt.xlabel(f'Time (ms)')
plt.title(f'{title}, data on {len(deltas):,d} data points --- ' +
f'deltas avg {prettify_time_interval(0, deltas_avg/1000)}, ' +
f'delta std {prettify_time_interval(0, deltas_std/1000)}')
if save:
plt.savefig(save)
else:
plt.show()
if __name__ == "__main__":
pass
# times = []
# with open("ping.txt", "r") as f:
# for line in f.readlines():
# t = line.split("=")[-1].split(" ms")[0]
# t = float(t)
# times.append(t)
#
# print(times)
# plot_time_distribution_gauss(times)
# rtt = []
# diff = []
# with open("log_1121_mw.txt", "r") as f:
# for line in f.readlines():
# line_data = line.strip().split(" ")
# _r = int(line_data[1]) / 1000
# _d = int(line_data[2]) / 1000
# rtt.append(_r)
# diff.append(_d)
# print(rtt)
# plot_time_distribution_gauss(rtt, tmax=10, title="RTT distribution MR")
# plot_time_distribution_gauss(rtt, tmax=10, title="RTT distribution MR", save="rtt_mr.png")
# plot_time_distribution_gauss(rtt, tmax=10, title="RTT distribution MW")
# plot_time_distribution_gauss(rtt, tmax=10, title="RTT distribution MW", save="rtt_mw.png")
# diff.append(9.9)
# print(diff)
# plot_time_distribution_gauss(diff, tmax=10, title="Diff distribution MR")
# plot_time_distribution_gauss(diff, tmax=10, title="Diff distribution MR", save="diff_mr.png")
# plot_time_distribution_gauss(diff, tmax=10, title="Diff distribution MR")
# plot_time_distribution_gauss(diff, tmax=10, title="Diff distribution MW", save="diff_mw.png")
# --- RTT only ---
# with open("data/rtt_box.txt", "r") as f:
# times = [float(line.split("=")[-1].split(" ms")[0]) for line in f.readlines() if line.strip()]
# plot_time_distribution_gauss(times)
# plot_time_distribution_raise(times)
# with open("data/rtt_server_wifi.txt", "r") as f:
# times = [float(line.split(" = ")[-1]) for line in f.readlines() if line.strip()]
# plot_time_distribution_gauss(times)
# plot_time_distribution_raise(times)
# with open("data/rtt_server_wired.txt", "r") as f:
# times = [float(line.split(" = ")[-1]) for line in f.readlines() if line.strip()]
# plot_time_distribution_gauss(times)
# plot_time_distribution_raise(times)
# print(times)
# plot_time_distribution_gauss(times)
Markdown is supported
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