1 # Copyright (c) 2023 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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 import plotly.graph_objects as go
23 from ..utils.constants import Constants as C
24 from ..utils.utils import classify_anomalies, get_color
27 def _get_hdrh_latencies(row: pd.Series, name: str) -> dict:
28 """Get the HDRH latencies from the test data.
30 :param row: A row fron the data frame with test data.
31 :param name: The test name to be displayed as the graph title.
32 :type row: pandas.Series
34 :returns: Dictionary with HDRH latencies.
38 latencies = {"name": name}
39 for key in C.LAT_HDRH:
41 latencies[key] = row[key]
48 def select_trending_data(data: pd.DataFrame, itm:dict) -> pd.DataFrame:
49 """Select the data for graphs from the provided data frame.
51 :param data: Data frame with data for graphs.
52 :param itm: Item (in this case job name) which data will be selected from
54 :type data: pandas.DataFrame
56 :returns: A data frame with selected data.
57 :rtype: pandas.DataFrame
60 phy = itm["phy"].split("-")
62 topo, arch, nic, drv = phy
67 drv = drv.replace("_", "-")
71 core = str() if itm["dut"] == "trex" else f"{itm['core']}"
72 ttype = "ndrpdr" if itm["testtype"] in ("ndr", "pdr") else itm["testtype"]
75 (data["test_type"] == ttype) &
76 (data["passed"] == True)
78 df = df[df.job.str.endswith(f"{topo}-{arch}")]
79 df = df[df.test_id.str.contains(
80 f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$",
82 )].sort_values(by="start_time", ignore_index=True)
87 def _generate_trending_traces(ttype: str, name: str, df: pd.DataFrame,
88 color: str, norm_factor: float) -> list:
89 """Generate the trending traces for the trending graph.
91 :param ttype: Test type (MRR, NDR, PDR).
92 :param name: The test name to be displayed as the graph title.
93 :param df: Data frame with test data.
94 :param color: The color of the trace (samples and trend line).
95 :param norm_factor: The factor used for normalization of the results to CPU
96 frequency set to Constants.NORM_FREQUENCY.
99 :type df: pandas.DataFrame
101 :type norm_factor: float
102 :returns: Traces (samples, trending line, anomalies)
106 df = df.dropna(subset=[C.VALUE[ttype], ])
110 x_axis = df["start_time"].tolist()
111 if ttype == "pdr-lat":
112 y_data = [(itm / norm_factor) for itm in df[C.VALUE[ttype]].tolist()]
114 y_data = [(itm * norm_factor) for itm in df[C.VALUE[ttype]].tolist()]
116 anomalies, trend_avg, trend_stdev = classify_anomalies(
117 {k: v for k, v in zip(x_axis, y_data)}
122 customdata_samples = list()
123 for idx, (_, row) in enumerate(df.iterrows()):
124 d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
126 f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
127 f"<prop> [{row[C.UNIT[ttype]]}]: {y_data[idx]:,.0f}<br>"
129 f"{d_type}-ref: {row['dut_version']}<br>"
130 f"csit-ref: {row['job']}/{row['build']}<br>"
131 f"hosts: {', '.join(row['hosts'])}"
135 f"stdev [{row['result_receive_rate_rate_unit']}]: "
136 f"{row['result_receive_rate_rate_stdev']:,.0f}<br>"
140 hover_itm = hover_itm.replace(
141 "<prop>", "latency" if ttype == "pdr-lat" else "average"
142 ).replace("<stdev>", stdev)
143 hover.append(hover_itm)
144 if ttype == "pdr-lat":
145 customdata_samples.append(_get_hdrh_latencies(row, name))
146 customdata.append({"name": name})
148 customdata_samples.append({"name": name, "show_telemetry": True})
149 customdata.append({"name": name})
152 for avg, stdev, (_, row) in zip(trend_avg, trend_stdev, df.iterrows()):
153 d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
155 f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
156 f"trend [pps]: {avg:,.0f}<br>"
157 f"stdev [pps]: {stdev:,.0f}<br>"
158 f"{d_type}-ref: {row['dut_version']}<br>"
159 f"csit-ref: {row['job']}/{row['build']}<br>"
160 f"hosts: {', '.join(row['hosts'])}"
162 if ttype == "pdr-lat":
163 hover_itm = hover_itm.replace("[pps]", "[us]")
164 hover_trend.append(hover_itm)
167 go.Scatter( # Samples
178 hoverinfo="text+name",
181 customdata=customdata_samples
183 go.Scatter( # Trend line
194 hoverinfo="text+name",
197 customdata=customdata
204 anomaly_color = list()
206 for idx, anomaly in enumerate(anomalies):
207 if anomaly in ("regression", "progression"):
208 anomaly_x.append(x_axis[idx])
209 anomaly_y.append(trend_avg[idx])
210 anomaly_color.append(C.ANOMALY_COLOR[anomaly])
212 f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}<br>"
213 f"trend [pps]: {trend_avg[idx]:,.0f}<br>"
214 f"classification: {anomaly}"
216 if ttype == "pdr-lat":
217 hover_itm = hover_itm.replace("[pps]", "[us]")
218 hover.append(hover_itm)
219 anomaly_color.extend([0.0, 0.5, 1.0])
226 hoverinfo="text+name",
230 customdata=customdata,
233 "symbol": "circle-open",
234 "color": anomaly_color,
235 "colorscale": C.COLORSCALE_LAT \
236 if ttype == "pdr-lat" else C.COLORSCALE_TPUT,
244 "title": "Circles Marking Data Classification",
245 "titleside": "right",
247 "tickvals": [0.167, 0.500, 0.833],
248 "ticktext": C.TICK_TEXT_LAT \
249 if ttype == "pdr-lat" else C.TICK_TEXT_TPUT,
262 def graph_trending(data: pd.DataFrame, sel:dict, layout: dict,
263 normalize: bool) -> tuple:
264 """Generate the trending graph(s) - MRR, NDR, PDR and for PDR also Latences
265 (result_latency_forward_pdr_50_avg).
267 :param data: Data frame with test results.
268 :param sel: Selected tests.
269 :param layout: Layout of plot.ly graph.
270 :param normalize: If True, the data is normalized to CPU frquency
271 Constants.NORM_FREQUENCY.
272 :type data: pandas.DataFrame
275 :type normalize: bool
276 :returns: Trending graph(s)
277 :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
285 for idx, itm in enumerate(sel):
287 df = select_trending_data(data, itm)
288 if df is None or df.empty:
292 phy = itm["phy"].split("-")
293 topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
294 norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY[topo_arch]) \
295 if topo_arch else 1.0
298 traces = _generate_trending_traces(itm["testtype"], itm["id"], df,
299 get_color(idx), norm_factor)
302 fig_tput = go.Figure()
303 fig_tput.add_traces(traces)
305 if itm["testtype"] == "pdr":
306 traces = _generate_trending_traces("pdr-lat", itm["id"], df,
307 get_color(idx), norm_factor)
310 fig_lat = go.Figure()
311 fig_lat.add_traces(traces)
314 fig_tput.update_layout(layout.get("plot-trending-tput", dict()))
316 fig_lat.update_layout(layout.get("plot-trending-lat", dict()))
318 return fig_tput, fig_lat
321 def graph_hdrh_latency(data: dict, layout: dict) -> go.Figure:
322 """Generate HDR Latency histogram graphs.
324 :param data: HDRH data.
325 :param layout: Layout of plot.ly graph.
328 :returns: HDR latency Histogram.
329 :rtype: plotly.graph_objects.Figure
335 for idx, (lat_name, lat_hdrh) in enumerate(data.items()):
337 decoded = hdrh.histogram.HdrHistogram.decode(lat_hdrh)
338 except (hdrh.codec.HdrLengthException, TypeError):
345 for item in decoded.get_recorded_iterator():
346 # The real value is "percentile".
347 # For 100%, we cut that down to "x_perc" to avoid
349 percentile = item.percentile_level_iterated_to
350 x_perc = min(percentile, C.PERCENTILE_MAX)
351 xaxis.append(previous_x)
352 yaxis.append(item.value_iterated_to)
354 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
355 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
356 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
357 f"Latency: {item.value_iterated_to}uSec"
359 next_x = 100.0 / (100.0 - x_perc)
361 yaxis.append(item.value_iterated_to)
363 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
364 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
365 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
366 f"Latency: {item.value_iterated_to}uSec"
369 prev_perc = percentile
375 name=C.GRAPH_LAT_HDRH_DESC[lat_name],
377 legendgroup=C.GRAPH_LAT_HDRH_DESC[lat_name],
378 showlegend=bool(idx % 2),
380 color=get_color(int(idx/2)),
382 width=1 if idx % 2 else 2
390 fig.add_traces(traces)
391 layout_hdrh = layout.get("plot-hdrh-latency", None)
393 fig.update_layout(layout_hdrh)