From 6f899314152a702db1c28dfb83c21cbdbde09420 Mon Sep 17 00:00:00 2001
From: aymeric <aymeric@feedly.com>
Date: Sun, 6 Jan 2019 21:24:39 +0100
Subject: [PATCH] Add Python script to plot graphs

---
 .gitignore       |   4 ++
 plot_graph.py    | 165 +++++++++++++++++++++++++++++++++++++++++++++++
 requirements.txt |   3 +
 3 files changed, 172 insertions(+)
 create mode 100644 plot_graph.py
 create mode 100644 requirements.txt

diff --git a/.gitignore b/.gitignore
index 354cd78..7c789bc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,4 +1,8 @@
+data/
+output/
+
 node_modules/
+
 venv
 __pycache__
 *.pyc
diff --git a/plot_graph.py b/plot_graph.py
new file mode 100644
index 0000000..e4596cf
--- /dev/null
+++ b/plot_graph.py
@@ -0,0 +1,165 @@
+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)
diff --git a/requirements.txt b/requirements.txt
new file mode 100644
index 0000000..f23634b
--- /dev/null
+++ b/requirements.txt
@@ -0,0 +1,3 @@
+numpy
+scipy
+matplotlib
-- 
GitLab