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:
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"]
73 dut_v100 = "none" if itm["dut"] == "trex" else itm["dut"]
79 (data["version"] == "1.0.0") &
80 (data["dut_type"].str.lower() == dut_v100)
83 (data["version"] == "1.0.1") &
84 (data["dut_type"].str.lower() == dut_v101)
87 (data["test_type"] == ttype) &
88 (data["passed"] == True)
90 df = df[df.job.str.endswith(f"{topo}-{arch}")]
91 df = df[df.test_id.str.contains(
92 f"^.*[.|-]{nic}.*{itm['framesize']}-{core}-{drv}{itm['test']}-{ttype}$",
94 )].sort_values(by="start_time", ignore_index=True)
99 def _generate_trending_traces(ttype: str, name: str, df: pd.DataFrame,
100 color: str, norm_factor: float) -> list:
101 """Generate the trending traces for the trending graph.
103 :param ttype: Test type (MRR, NDR, PDR).
104 :param name: The test name to be displayed as the graph title.
105 :param df: Data frame with test data.
106 :param color: The color of the trace (samples and trend line).
107 :param norm_factor: The factor used for normalization of the results to CPU
108 frequency set to Constants.NORM_FREQUENCY.
111 :type df: pandas.DataFrame
113 :type norm_factor: float
114 :returns: Traces (samples, trending line, anomalies)
118 df = df.dropna(subset=[C.VALUE[ttype], ])
122 x_axis = df["start_time"].tolist()
123 if ttype == "pdr-lat":
124 y_data = [(itm / norm_factor) for itm in df[C.VALUE[ttype]].tolist()]
126 y_data = [(itm * norm_factor) for itm in df[C.VALUE[ttype]].tolist()]
128 anomalies, trend_avg, trend_stdev = classify_anomalies(
129 {k: v for k, v in zip(x_axis, y_data)}
134 customdata_samples = list()
135 for idx, (_, row) in enumerate(df.iterrows()):
136 d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
138 f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
139 f"<prop> [{row[C.UNIT[ttype]]}]: {y_data[idx]:,.0f}<br>"
141 f"{d_type}-ref: {row['dut_version']}<br>"
142 f"csit-ref: {row['job']}/{row['build']}<br>"
143 f"hosts: {', '.join(row['hosts'])}"
147 f"stdev [{row['result_receive_rate_rate_unit']}]: "
148 f"{row['result_receive_rate_rate_stdev']:,.0f}<br>"
152 hover_itm = hover_itm.replace(
153 "<prop>", "latency" if ttype == "pdr-lat" else "average"
154 ).replace("<stdev>", stdev)
155 hover.append(hover_itm)
156 if ttype == "pdr-lat":
157 customdata_samples.append(_get_hdrh_latencies(row, name))
158 customdata.append({"name": name})
160 customdata_samples.append({"name": name, "show_telemetry": True})
161 customdata.append({"name": name})
164 for avg, stdev, (_, row) in zip(trend_avg, trend_stdev, df.iterrows()):
165 d_type = "trex" if row["dut_type"] == "none" else row["dut_type"]
167 f"date: {row['start_time'].strftime('%Y-%m-%d %H:%M:%S')}<br>"
168 f"trend [pps]: {avg:,.0f}<br>"
169 f"stdev [pps]: {stdev:,.0f}<br>"
170 f"{d_type}-ref: {row['dut_version']}<br>"
171 f"csit-ref: {row['job']}/{row['build']}<br>"
172 f"hosts: {', '.join(row['hosts'])}"
174 if ttype == "pdr-lat":
175 hover_itm = hover_itm.replace("[pps]", "[us]")
176 hover_trend.append(hover_itm)
179 go.Scatter( # Samples
190 hoverinfo="text+name",
193 customdata=customdata_samples
195 go.Scatter( # Trend line
206 hoverinfo="text+name",
209 customdata=customdata
216 anomaly_color = list()
218 for idx, anomaly in enumerate(anomalies):
219 if anomaly in ("regression", "progression"):
220 anomaly_x.append(x_axis[idx])
221 anomaly_y.append(trend_avg[idx])
222 anomaly_color.append(C.ANOMALY_COLOR[anomaly])
224 f"date: {x_axis[idx].strftime('%Y-%m-%d %H:%M:%S')}<br>"
225 f"trend [pps]: {trend_avg[idx]:,.0f}<br>"
226 f"classification: {anomaly}"
228 if ttype == "pdr-lat":
229 hover_itm = hover_itm.replace("[pps]", "[us]")
230 hover.append(hover_itm)
231 anomaly_color.extend([0.0, 0.5, 1.0])
238 hoverinfo="text+name",
242 customdata=customdata,
245 "symbol": "circle-open",
246 "color": anomaly_color,
247 "colorscale": C.COLORSCALE_LAT \
248 if ttype == "pdr-lat" else C.COLORSCALE_TPUT,
256 "title": "Circles Marking Data Classification",
257 "titleside": "right",
259 "tickvals": [0.167, 0.500, 0.833],
260 "ticktext": C.TICK_TEXT_LAT \
261 if ttype == "pdr-lat" else C.TICK_TEXT_TPUT,
274 def graph_trending(data: pd.DataFrame, sel:dict, layout: dict,
275 normalize: bool) -> tuple:
276 """Generate the trending graph(s) - MRR, NDR, PDR and for PDR also Latences
277 (result_latency_forward_pdr_50_avg).
279 :param data: Data frame with test results.
280 :param sel: Selected tests.
281 :param layout: Layout of plot.ly graph.
282 :param normalize: If True, the data is normalized to CPU frquency
283 Constants.NORM_FREQUENCY.
284 :type data: pandas.DataFrame
287 :type normalize: bool
288 :returns: Trending graph(s)
289 :rtype: tuple(plotly.graph_objects.Figure, plotly.graph_objects.Figure)
297 for idx, itm in enumerate(sel):
299 df = select_trending_data(data, itm)
300 if df is None or df.empty:
304 phy = itm["phy"].split("-")
305 topo_arch = f"{phy[0]}-{phy[1]}" if len(phy) == 4 else str()
306 norm_factor = (C.NORM_FREQUENCY / C.FREQUENCY[topo_arch]) \
307 if topo_arch else 1.0
310 traces = _generate_trending_traces(itm["testtype"], itm["id"], df,
311 get_color(idx), norm_factor)
314 fig_tput = go.Figure()
315 fig_tput.add_traces(traces)
317 if itm["testtype"] == "pdr":
318 traces = _generate_trending_traces("pdr-lat", itm["id"], df,
319 get_color(idx), norm_factor)
322 fig_lat = go.Figure()
323 fig_lat.add_traces(traces)
326 fig_tput.update_layout(layout.get("plot-trending-tput", dict()))
328 fig_lat.update_layout(layout.get("plot-trending-lat", dict()))
330 return fig_tput, fig_lat
333 def graph_hdrh_latency(data: dict, layout: dict) -> go.Figure:
334 """Generate HDR Latency histogram graphs.
336 :param data: HDRH data.
337 :param layout: Layout of plot.ly graph.
340 :returns: HDR latency Histogram.
341 :rtype: plotly.graph_objects.Figure
347 for idx, (lat_name, lat_hdrh) in enumerate(data.items()):
349 decoded = hdrh.histogram.HdrHistogram.decode(lat_hdrh)
350 except (hdrh.codec.HdrLengthException, TypeError):
357 for item in decoded.get_recorded_iterator():
358 # The real value is "percentile".
359 # For 100%, we cut that down to "x_perc" to avoid
361 percentile = item.percentile_level_iterated_to
362 x_perc = min(percentile, C.PERCENTILE_MAX)
363 xaxis.append(previous_x)
364 yaxis.append(item.value_iterated_to)
366 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
367 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
368 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
369 f"Latency: {item.value_iterated_to}uSec"
371 next_x = 100.0 / (100.0 - x_perc)
373 yaxis.append(item.value_iterated_to)
375 f"<b>{C.GRAPH_LAT_HDRH_DESC[lat_name]}</b><br>"
376 f"Direction: {('W-E', 'E-W')[idx % 2]}<br>"
377 f"Percentile: {prev_perc:.5f}-{percentile:.5f}%<br>"
378 f"Latency: {item.value_iterated_to}uSec"
381 prev_perc = percentile
387 name=C.GRAPH_LAT_HDRH_DESC[lat_name],
389 legendgroup=C.GRAPH_LAT_HDRH_DESC[lat_name],
390 showlegend=bool(idx % 2),
392 color=get_color(int(idx/2)),
394 width=1 if idx % 2 else 2
402 fig.add_traces(traces)
403 layout_hdrh = layout.get("plot-hdrh-latency", None)
405 fig.update_layout(layout_hdrh)