UTI: Move constatns to a separate file
[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 from ..utils.constants import Constants as C
24
25
26 def _get_color(idx: int) -> str:
27     """
28     """
29     return C.PLOT_COLORS[idx % len(C.PLOT_COLORS)]
30
31
32 def get_short_version(version: str, dut_type: str="vpp") -> str:
33     """
34     """
35
36     if dut_type in ("trex", "dpdk"):
37         return version
38
39     s_version = str()
40     groups = re.search(
41         pattern=re.compile(r"^(\d{2}).(\d{2})-(rc0|rc1|rc2|release$)"),
42         string=version
43     )
44     if groups:
45         try:
46             s_version = \
47                 f"{groups.group(1)}.{groups.group(2)}.{groups.group(3)}".\
48                     replace("release", "rls")
49         except IndexError:
50             pass
51
52     return s_version
53
54
55 def select_iterative_data(data: pd.DataFrame, itm:dict) -> pd.DataFrame:
56     """
57     """
58
59     phy = itm["phy"].split("-")
60     if len(phy) == 4:
61         topo, arch, nic, drv = phy
62         if drv == "dpdk":
63             drv = ""
64         else:
65             drv += "-"
66             drv = drv.replace("_", "-")
67     else:
68         return None
69
70     core = str() if itm["dut"] == "trex" else f"{itm['core']}"
71     ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
72     dut_v100 = "none" if itm["dut"] == "trex" else itm["dut"]
73     dut_v101 = itm["dut"]
74
75     df = data.loc[(
76         (data["release"] == itm["rls"]) &
77         (
78             (
79                 (data["version"] == "1.0.0") &
80                 (data["dut_type"].str.lower() == dut_v100)
81             ) |
82             (
83                 (data["version"] == "1.0.1") &
84                 (data["dut_type"].str.lower() == dut_v101)
85             )
86         ) &
87         (data["test_type"] == ttype) &
88         (data["passed"] == True)
89     )]
90     regex_test = \
91         f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$"
92     df = df[
93         (df.job.str.endswith(f"{topo}-{arch}")) &
94         (df.dut_version.str.contains(itm["dutver"].replace(".r", "-r").\
95             replace("rls", "release"))) &
96         (df.test_id.str.contains(regex_test, regex=True))
97     ]
98
99     return df
100
101
102 def graph_iterative(data: pd.DataFrame, sel:dict, layout: dict,
103         normalize: bool) -> tuple:
104     """
105     """
106
107     fig_tput = None
108     fig_lat = None
109
110     tput_traces = list()
111     y_tput_max = 0
112     lat_traces = list()
113     y_lat_max = 0
114     x_lat = list()
115     show_latency = False
116     show_tput = False
117     for idx, itm in enumerate(sel):
118         itm_data = select_iterative_data(data, itm)
119         if itm_data.empty:
120             continue
121         phy = itm["phy"].split("-")
122         topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
123         norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY[topo_arch]) \
124             if normalize else 1.0
125         if itm["testtype"] == "mrr":
126             y_data_raw = itm_data[C.VALUE_ITER[itm["testtype"]]].to_list()[0]
127             y_data = [(y * norm_factor) for y in y_data_raw]
128             if len(y_data) > 0:
129                 y_tput_max = \
130                     max(y_data) if max(y_data) > y_tput_max else y_tput_max
131         else:
132             y_data_raw = itm_data[C.VALUE_ITER[itm["testtype"]]].to_list()
133             y_data = [(y * norm_factor) for y in y_data_raw]
134             if y_data:
135                 y_tput_max = \
136                     max(y_data) if max(y_data) > y_tput_max else y_tput_max
137         nr_of_samples = len(y_data)
138         tput_kwargs = dict(
139             y=y_data,
140             name=(
141                 f"{idx + 1}. "
142                 f"({nr_of_samples:02d} "
143                 f"run{'s' if nr_of_samples > 1 else ''}) "
144                 f"{itm['id']}"
145             ),
146             hoverinfo=u"y+name",
147             boxpoints="all",
148             jitter=0.3,
149             marker=dict(color=_get_color(idx))
150         )
151         tput_traces.append(go.Box(**tput_kwargs))
152         show_tput = True
153
154         if itm["testtype"] == "pdr":
155             y_lat_row = itm_data[C.VALUE_ITER["pdr-lat"]].to_list()
156             y_lat = [(y / norm_factor) for y in y_lat_row]
157             if y_lat:
158                 y_lat_max = max(y_lat) if max(y_lat) > y_lat_max else y_lat_max
159             nr_of_samples = len(y_lat)
160             lat_kwargs = dict(
161                 y=y_lat,
162                 name=(
163                     f"{idx + 1}. "
164                     f"({nr_of_samples:02d} "
165                     f"run{u's' if nr_of_samples > 1 else u''}) "
166                     f"{itm['id']}"
167                 ),
168                 hoverinfo="all",
169                 boxpoints="all",
170                 jitter=0.3,
171                 marker=dict(color=_get_color(idx))
172             )
173             x_lat.append(idx + 1)
174             lat_traces.append(go.Box(**lat_kwargs))
175             show_latency = True
176         else:
177             lat_traces.append(go.Box())
178
179     if show_tput:
180         pl_tput = deepcopy(layout["plot-throughput"])
181         pl_tput["xaxis"]["tickvals"] = [i for i in range(len(sel))]
182         pl_tput["xaxis"]["ticktext"] = [str(i + 1) for i in range(len(sel))]
183         if y_tput_max:
184             pl_tput["yaxis"]["range"] = [0, (int(y_tput_max / 1e6) + 1) * 1e6]
185         fig_tput = go.Figure(data=tput_traces, layout=pl_tput)
186
187     if show_latency:
188         pl_lat = deepcopy(layout["plot-latency"])
189         pl_lat["xaxis"]["tickvals"] = [i for i in range(len(x_lat))]
190         pl_lat["xaxis"]["ticktext"] = x_lat
191         if y_lat_max:
192             pl_lat["yaxis"]["range"] = [0, (int(y_lat_max / 10) + 1) * 10]
193         fig_lat = go.Figure(data=lat_traces, layout=pl_lat)
194
195     return fig_tput, fig_lat
196
197
198 def table_comparison(data: pd.DataFrame, sel:dict,
199         normalize: bool) -> pd.DataFrame:
200     """
201     """
202     table = pd.DataFrame(
203         {
204             "Test Case": [
205                 "64b-2t1c-avf-eth-l2xcbase-eth-2memif-1dcr",
206                 "64b-2t1c-avf-eth-l2xcbase-eth-2vhostvr1024-1vm-vppl2xc",
207                 "64b-2t1c-avf-ethip4udp-ip4base-iacl50sl-10kflows",
208                 "78b-2t1c-avf-ethip6-ip6scale2m-rnd "],
209             "2106.0-8": [
210                 "14.45 +- 0.08",
211                 "9.63 +- 0.05",
212                 "9.7 +- 0.02",
213                 "8.95 +- 0.06"],
214             "2110.0-8": [
215                 "14.45 +- 0.08",
216                 "9.63 +- 0.05",
217                 "9.7 +- 0.02",
218                 "8.95 +- 0.06"],
219             "2110.0-9": [
220                 "14.45 +- 0.08",
221                 "9.63 +- 0.05",
222                 "9.7 +- 0.02",
223                 "8.95 +- 0.06"],
224             "2202.0-9": [
225                 "14.45 +- 0.08",
226                 "9.63 +- 0.05",
227                 "9.7 +- 0.02",
228                 "8.95 +- 0.06"],
229             "2110.0-9 vs 2110.0-8": [
230                 "-0.23 +-  0.62",
231                 "-1.37 +-   1.3",
232                 "+0.08 +-   0.2",
233                 "-2.16 +-  0.83"],
234             "2202.0-9 vs 2110.0-9": [
235                 "+6.95 +-  0.72",
236                 "+5.35 +-  1.26",
237                 "+4.48 +-  1.48",
238                 "+4.09 +-  0.95"]
239         }
240     )
241
242     return pd.DataFrame()  #table