UTI: Colors in iteraive graphs
[csit.git] / resources / tools / dash / app / pal / report / graphs.py
1 # Copyright (c) 2022 Cisco and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
5 #
6 #     http://www.apache.org/licenses/LICENSE-2.0
7 #
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
13
14 """
15 """
16
17 import re
18 import plotly.graph_objects as go
19 import pandas as pd
20
21 from copy import deepcopy
22
23 import hdrh.histogram
24 import hdrh.codec
25
26
27 _VALUE = {
28     "mrr": "result_receive_rate_rate_values",
29     "ndr": "result_ndr_lower_rate_value",
30     "pdr": "result_pdr_lower_rate_value",
31     "pdr-lat": "result_latency_forward_pdr_50_avg"
32 }
33 _UNIT = {
34     "mrr": "result_receive_rate_rate_unit",
35     "ndr": "result_ndr_lower_rate_unit",
36     "pdr": "result_pdr_lower_rate_unit",
37     "pdr-lat": "result_latency_forward_pdr_50_unit"
38 }
39 _LAT_HDRH = (  # Do not change the order
40     "result_latency_forward_pdr_0_hdrh",
41     "result_latency_reverse_pdr_0_hdrh",
42     "result_latency_forward_pdr_10_hdrh",
43     "result_latency_reverse_pdr_10_hdrh",
44     "result_latency_forward_pdr_50_hdrh",
45     "result_latency_reverse_pdr_50_hdrh",
46     "result_latency_forward_pdr_90_hdrh",
47     "result_latency_reverse_pdr_90_hdrh",
48 )
49 # This value depends on latency stream rate (9001 pps) and duration (5s).
50 # Keep it slightly higher to ensure rounding errors to not remove tick mark.
51 PERCENTILE_MAX = 99.999501
52
53 _GRAPH_LAT_HDRH_DESC = {
54     "result_latency_forward_pdr_0_hdrh": "No-load.",
55     "result_latency_reverse_pdr_0_hdrh": "No-load.",
56     "result_latency_forward_pdr_10_hdrh": "Low-load, 10% PDR.",
57     "result_latency_reverse_pdr_10_hdrh": "Low-load, 10% PDR.",
58     "result_latency_forward_pdr_50_hdrh": "Mid-load, 50% PDR.",
59     "result_latency_reverse_pdr_50_hdrh": "Mid-load, 50% PDR.",
60     "result_latency_forward_pdr_90_hdrh": "High-load, 90% PDR.",
61     "result_latency_reverse_pdr_90_hdrh": "High-load, 90% PDR."
62 }
63
64
65 def _get_color(idx: int) -> str:
66     """
67     """
68     _COLORS = (
69         "#1A1110", "#DA2647", "#214FC6", "#01786F", "#BD8260", "#FFD12A",
70         "#A6E7FF", "#738276", "#C95A49", "#FC5A8D", "#CEC8EF", "#391285",
71         "#6F2DA8", "#FF878D", "#45A27D", "#FFD0B9", "#FD5240", "#DB91EF",
72         "#44D7A8", "#4F86F7", "#84DE02", "#FFCFF1", "#614051"
73     )
74     return _COLORS[idx % len(_COLORS)]
75
76
77 def get_short_version(version: str, dut_type: str="vpp") -> str:
78     """
79     """
80
81     if dut_type in ("trex", "dpdk"):
82         return version
83
84     s_version = str()
85     groups = re.search(
86         pattern=re.compile(r"^(\d{2}).(\d{2})-(rc0|rc1|rc2|release$)"),
87         string=version
88     )
89     if groups:
90         try:
91             s_version = \
92                 f"{groups.group(1)}.{groups.group(2)}.{groups.group(3)}".\
93                     replace("release", "rls")
94         except IndexError:
95             pass
96
97     return s_version
98
99
100 def select_iterative_data(data: pd.DataFrame, itm:dict) -> pd.DataFrame:
101     """
102     """
103
104     phy = itm["phy"].split("-")
105     if len(phy) == 4:
106         topo, arch, nic, drv = phy
107         if drv == "dpdk":
108             drv = ""
109         else:
110             drv += "-"
111             drv = drv.replace("_", "-")
112     else:
113         return None
114
115     core = str() if itm["dut"] == "trex" else f"{itm['core']}"
116     ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
117     dut_v100 = "none" if itm["dut"] == "trex" else itm["dut"]
118     dut_v101 = itm["dut"]
119
120     df = data.loc[(
121         (data["release"] == itm["rls"]) &
122         (
123             (
124                 (data["version"] == "1.0.0") &
125                 (data["dut_type"].str.lower() == dut_v100)
126             ) |
127             (
128                 (data["version"] == "1.0.1") &
129                 (data["dut_type"].str.lower() == dut_v101)
130             )
131         ) &
132         (data["test_type"] == ttype) &
133         (data["passed"] == True)
134     )]
135     regex_test = \
136         f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$"
137     df = df[
138         (df.job.str.endswith(f"{topo}-{arch}")) &
139         (df.dut_version.str.contains(itm["dutver"].replace(".r", "-r").\
140             replace("rls", "release"))) &
141         (df.test_id.str.contains(regex_test, regex=True))
142     ]
143
144     return df
145
146
147 def graph_iterative(data: pd.DataFrame, sel:dict, layout: dict) -> tuple:
148     """
149     """
150
151     fig_tput = None
152     fig_lat = None
153
154     tput_traces = list()
155     y_tput_max = 0
156     lat_traces = list()
157     y_lat_max = 0
158     x_lat = list()
159     show_latency = False
160     show_tput = False
161     for idx, itm in enumerate(sel):
162         itm_data = select_iterative_data(data, itm)
163         if itm_data.empty:
164             continue
165         if itm["testtype"] == "mrr":
166             y_data = itm_data[_VALUE[itm["testtype"]]].to_list()[0]
167             if y_data.size > 0:
168                 y_tput_max = \
169                     max(y_data) if max(y_data) > y_tput_max else y_tput_max
170         else:
171             y_data = itm_data[_VALUE[itm["testtype"]]].to_list()
172             if y_data:
173                 y_tput_max = \
174                     max(y_data) if max(y_data) > y_tput_max else y_tput_max
175         nr_of_samples = len(y_data)
176         tput_kwargs = dict(
177             y=y_data,
178             name=(
179                 f"{idx + 1}. "
180                 f"({nr_of_samples:02d} "
181                 f"run{'s' if nr_of_samples > 1 else ''}) "
182                 f"{itm['id']}"
183             ),
184             hoverinfo=u"y+name",
185             boxpoints="all",
186             jitter=0.3,
187             marker=dict(color=_get_color(idx))
188         )
189         tput_traces.append(go.Box(**tput_kwargs))
190         show_tput = True
191
192         if itm["testtype"] == "pdr":
193             y_lat = itm_data[_VALUE["pdr-lat"]].to_list()
194             if y_lat:
195                 y_lat_max = max(y_lat) if max(y_lat) > y_lat_max else y_lat_max
196             nr_of_samples = len(y_lat)
197             lat_kwargs = dict(
198                 y=y_lat,
199                 name=(
200                     f"{idx + 1}. "
201                     f"({nr_of_samples:02d} "
202                     f"run{u's' if nr_of_samples > 1 else u''}) "
203                     f"{itm['id']}"
204                 ),
205                 hoverinfo="all",
206                 boxpoints="all",
207                 jitter=0.3,
208                 marker=dict(color=_get_color(idx))
209             )
210             x_lat.append(idx + 1)
211             lat_traces.append(go.Box(**lat_kwargs))
212             show_latency = True
213         else:
214             lat_traces.append(go.Box())
215
216     if show_tput:
217         pl_tput = deepcopy(layout["plot-throughput"])
218         pl_tput["xaxis"]["tickvals"] = [i for i in range(len(sel))]
219         pl_tput["xaxis"]["ticktext"] = [str(i + 1) for i in range(len(sel))]
220         if y_tput_max:
221             pl_tput["yaxis"]["range"] = [0, (int(y_tput_max / 1e6) + 1) * 1e6]
222         fig_tput = go.Figure(data=tput_traces, layout=pl_tput)
223
224     if show_latency:
225         pl_lat = deepcopy(layout["plot-latency"])
226         pl_lat["xaxis"]["tickvals"] = [i for i in range(len(x_lat))]
227         pl_lat["xaxis"]["ticktext"] = x_lat
228         if y_lat_max:
229             pl_lat["yaxis"]["range"] = [0, (int(y_lat_max / 10) + 1) * 10]
230         fig_lat = go.Figure(data=lat_traces, layout=pl_lat)
231
232     return fig_tput, fig_lat
233
234
235 def table_comparison(data: pd.DataFrame, sel:dict) -> pd.DataFrame:
236     """
237     """
238     table = pd.DataFrame(
239         {
240             "Test Case": [
241                 "64b-2t1c-avf-eth-l2xcbase-eth-2memif-1dcr",
242                 "64b-2t1c-avf-eth-l2xcbase-eth-2vhostvr1024-1vm-vppl2xc",
243                 "64b-2t1c-avf-ethip4udp-ip4base-iacl50sl-10kflows",
244                 "78b-2t1c-avf-ethip6-ip6scale2m-rnd "],
245             "2106.0-8": [
246                 "14.45 +- 0.08",
247                 "9.63 +- 0.05",
248                 "9.7 +- 0.02",
249                 "8.95 +- 0.06"],
250             "2110.0-8": [
251                 "14.45 +- 0.08",
252                 "9.63 +- 0.05",
253                 "9.7 +- 0.02",
254                 "8.95 +- 0.06"],
255             "2110.0-9": [
256                 "14.45 +- 0.08",
257                 "9.63 +- 0.05",
258                 "9.7 +- 0.02",
259                 "8.95 +- 0.06"],
260             "2202.0-9": [
261                 "14.45 +- 0.08",
262                 "9.63 +- 0.05",
263                 "9.7 +- 0.02",
264                 "8.95 +- 0.06"],
265             "2110.0-9 vs 2110.0-8": [
266                 "-0.23 +-  0.62",
267                 "-1.37 +-   1.3",
268                 "+0.08 +-   0.2",
269                 "-2.16 +-  0.83"],
270             "2202.0-9 vs 2110.0-9": [
271                 "+6.95 +-  0.72",
272                 "+5.35 +-  1.26",
273                 "+4.48 +-  1.48",
274                 "+4.09 +-  0.95"]
275         }
276     )
277
278     return pd.DataFrame()  #table
279
280
281 def graph_hdrh_latency(data: dict, layout: dict) -> go.Figure:
282     """
283     """
284
285     fig = None
286
287     traces = list()
288     for idx, (lat_name, lat_hdrh) in enumerate(data.items()):
289         try:
290             decoded = hdrh.histogram.HdrHistogram.decode(lat_hdrh)
291         except (hdrh.codec.HdrLengthException, TypeError) as err:
292             continue
293         previous_x = 0.0
294         prev_perc = 0.0
295         xaxis = list()
296         yaxis = list()
297         hovertext = list()
298         for item in decoded.get_recorded_iterator():
299             # The real value is "percentile".
300             # For 100%, we cut that down to "x_perc" to avoid
301             # infinity.
302             percentile = item.percentile_level_iterated_to
303             x_perc = min(percentile, PERCENTILE_MAX)
304             xaxis.append(previous_x)
305             yaxis.append(item.value_iterated_to)
306             hovertext.append(
307                 f"<b>{_GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
308                 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
309                 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
310                 f"Latency: {item.value_iterated_to}uSec"
311             )
312             next_x = 100.0 / (100.0 - x_perc)
313             xaxis.append(next_x)
314             yaxis.append(item.value_iterated_to)
315             hovertext.append(
316                 f"<b>{_GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
317                 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
318                 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
319                 f"Latency: {item.value_iterated_to}uSec"
320             )
321             previous_x = next_x
322             prev_perc = percentile
323
324         traces.append(
325             go.Scatter(
326                 x=xaxis,
327                 y=yaxis,
328                 name=_GRAPH_LAT_HDRH_DESC[lat_name],
329                 mode="lines",
330                 legendgroup=_GRAPH_LAT_HDRH_DESC[lat_name],
331                 showlegend=bool(idx % 2),
332                 line=dict(
333                     color=_get_color(int(idx/2)),
334                     dash="solid",
335                     width=1 if idx % 2 else 2
336                 ),
337                 hovertext=hovertext,
338                 hoverinfo="text"
339             )
340         )
341     if traces:
342         fig = go.Figure()
343         fig.add_traces(traces)
344         layout_hdrh = layout.get("plot-hdrh-latency", None)
345         if lat_hdrh:
346             fig.update_layout(layout_hdrh)
347
348     return fig