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