-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPlotter.py
executable file
·118 lines (104 loc) · 3.91 KB
/
Plotter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!./venv/bin/python
import json
import math
import os
import sys
from scipy.integrate import cumtrapz
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
import numpy as np
from hdrh.histogram import HdrHistogram
class Plotter:
def __init__(self, files: dict, CONST_TRAFFIC, CONST_DUT, CMP: bool, **kwargs):
self.files = files
self.CONST_TRAFFIC = CONST_TRAFFIC
self.CONST_DUT = CONST_DUT
self.CMP = CMP
if CMP:
if "CMP_NAME" in kwargs:
self.CMP_NAME = kwargs["CMP_NAME"]
else:
raise TypeError("Must give CMP_NAME value if CMP is set")
d = self.decode(self.files)
self.hdr_histograms = d["hdrh"]
self.jitters = d["jitter"]
def parse_file(self, path):
f = open(path, "r")
json_data = json.loads(f.read())
f.close()
value_mean = json_data["latency"]["average"]
tot_max = json_data["latency"]["total_max"]
tot_min = json_data["latency"]["total_min"]
jitter = json_data["latency"]["jitter"]
histogram = json_data["latency"]["histogram"]
hdrh = json_data["latency"]["hdrh"]
return {
"mean": value_mean,
"tot_max": tot_max,
"tot_min": tot_min,
"jitter": jitter,
"histogram": histogram,
"hdrh": hdrh,
}
def decode(self, files):
hs = dict.fromkeys(self.CONST_DUT)
js = dict.fromkeys(self.CONST_DUT)
for e in hs:
hs[e] = {}
for e in js:
js[e] = {}
for file in files:
DUT = file.split("/")[1]
traffic_type = file.split("/")[2]
if (DUT in self.CONST_DUT) and (traffic_type in self.CONST_TRAFFIC):
parsed = self.parse_file(file)
hdrh = parsed["hdrh"]
jitter = parsed["jitter"]
h = HdrHistogram.decode(hdrh)
hs[DUT][traffic_type] = h
js[DUT][traffic_type] = jitter
return {"hdrh": hs, "jitter": js}
def do_plot(self):
if self.CMP:
for traffic in self.CONST_TRAFFIC:
outs = ""
jitters = []
for dut in self.hdr_histograms.keys():
out = "out/{}/{}_{}.txt".format(
dut.strip("."), dut.strip("."), traffic
)
outs += out + " "
self.hdr_histograms[dut][traffic].output_percentile_distribution(
open(out, "wb+"), 1000
)
jitters.append(self.jitters[dut][traffic])
print("Wrote: ", outs)
os.system(
"hdr-plot --output out/CMP_{}.png --title '{}' --jitters {} {} ".format(
f"{traffic}_{self.CMP_NAME}",
traffic,
",".join((map(str, jitters))),
outs,
)
)
hdrh_files = []
jitters = []
for dut in self.hdr_histograms.keys():
histograms = self.hdr_histograms[dut]
for traffic_type in histograms.keys():
out = "out/{}/{}.txt".format(dut, traffic_type)
print("Wrote: ", out)
hdrh_files.append(out)
histograms[traffic_type].output_percentile_distribution(
open(out, "wb+"), 1000
)
jitters.append(self.jitters[dut][traffic_type])
self.plot_multiple(hdrh_files, jitters)
def plot_multiple(self, hdrh_files, jitters):
for i in range(len(hdrh_files)):
file = hdrh_files[i]
jitter = jitters[i]
cmd = "hdr-plot --output {}.png --title '{}' --jitters {} {} ".format(
file.strip(".txt"), file.strip(".txt"), jitter, file,
)
os.system(cmd)