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.
18 import plotly.graph_objects as go
22 from datetime import datetime
23 from numpy import isnan
25 from ..jumpavg import classify
66 _TICK_TEXT_TPUT = [u"Regression", u"Normal", u"Progression"]
75 _TICK_TEXT_LAT = [u"Progression", u"Normal", u"Regression"]
77 "mrr": "result_receive_rate_rate_avg",
78 "ndr": "result_ndr_lower_rate_value",
79 "pdr": "result_pdr_lower_rate_value",
80 "pdr-lat": "result_latency_forward_pdr_50_avg"
83 "mrr": "result_receive_rate_rate_unit",
84 "ndr": "result_ndr_lower_rate_unit",
85 "pdr": "result_pdr_lower_rate_unit",
86 "pdr-lat": "result_latency_forward_pdr_50_unit"
90 def _classify_anomalies(data):
91 """Process the data and return anomalies and trending values.
93 Gather data into groups with average as trend value.
94 Decorate values within groups to be normal,
95 the first value of changed average as a regression, or a progression.
97 :param data: Full data set with unavailable samples replaced by nan.
98 :type data: OrderedDict
99 :returns: Classification and trend values
100 :rtype: 3-tuple, list of strings, list of floats and list of floats
102 # NaN means something went wrong.
103 # Use 0.0 to cause that being reported as a severe regression.
104 bare_data = [0.0 if isnan(sample) else sample for sample in data.values()]
105 # TODO: Make BitCountingGroupList a subclass of list again?
106 group_list = classify(bare_data).group_list
107 group_list.reverse() # Just to use .pop() for FIFO.
108 classification = list()
115 for sample in data.values():
117 classification.append(u"outlier")
119 stdevs.append(sample)
121 if values_left < 1 or active_group is None:
123 while values_left < 1: # Ignore empty groups (should not happen).
124 active_group = group_list.pop()
125 values_left = len(active_group.run_list)
126 avg = active_group.stats.avg
127 stdv = active_group.stats.stdev
128 classification.append(active_group.comment)
133 classification.append(u"normal")
137 return classification, avgs, stdevs
140 def trending_tput(data: pd.DataFrame, sel:dict, layout: dict, start: datetime,
148 def _generate_traces(ttype: str, name: str, df: pd.DataFrame,
149 start: datetime, end: datetime, color: str):
151 df = df.dropna(subset=[_VALUE[ttype], ])
155 x_axis = [d for d in df["start_time"] if d >= start and d <= end]
157 anomalies, trend_avg, trend_stdev = _classify_anomalies(
158 {k: v for k, v in zip(x_axis, df[_VALUE[ttype]])}
162 for _, row in df.iterrows():
164 f"date: {row['start_time'].strftime('%d-%m-%Y %H:%M:%S')}<br>"
165 f"<prop> [{row[_UNIT[ttype]]}]: {row[_VALUE[ttype]]}<br>"
167 f"{row['dut_type']}-ref: {row['dut_version']}<br>"
168 f"csit-ref: {row['job']}/{row['build']}"
172 f"stdev [{row['result_receive_rate_rate_unit']}]: "
173 f"{row['result_receive_rate_rate_stdev']}<br>"
177 hover_itm = hover_itm.replace(
178 "<prop>", "latency" if ttype == "pdr-lat" else "average"
179 ).replace("<stdev>", stdev)
180 hover.append(hover_itm)
183 for avg, stdev in zip(trend_avg, trend_stdev):
184 if ttype == "pdr-lat":
186 f"trend [us]: {avg}<br>"
187 f"stdev [us]: {stdev}"
191 f"trend [pps]: {avg}<br>"
192 f"stdev [pps]: {stdev}"
196 go.Scatter( # Samples
204 u"symbol": u"circle",
207 hoverinfo=u"text+name",
211 go.Scatter( # Trend line
222 hoverinfo=u"text+name",
231 anomaly_color = list()
232 for idx, anomaly in enumerate(anomalies):
233 if anomaly in (u"regression", u"progression"):
234 anomaly_x.append(x_axis[idx])
235 anomaly_y.append(trend_avg[idx])
236 anomaly_color.append(_ANOMALY_COLOR[anomaly])
237 anomaly_color.extend([0.0, 0.5, 1.0])
246 name=f"{name}-anomalies",
249 u"symbol": u"circle-open",
250 u"color": anomaly_color,
251 u"colorscale": _COLORSCALE_LAT \
252 if ttype == "pdr-lat" else _COLORSCALE_TPUT,
260 u"title": u"Circles Marking Data Classification",
261 u"titleside": u"right",
265 u"tickmode": u"array",
266 u"tickvals": [0.167, 0.500, 0.833],
267 u"ticktext": _TICK_TEXT_LAT \
268 if ttype == "pdr-lat" else _TICK_TEXT_TPUT,
283 for idx, itm in enumerate(sel):
284 phy = itm["phy"].split("-")
286 topo, arch, nic, drv = phy
287 if drv in ("dpdk", "ixgbe"):
291 drv = drv.replace("_", "-")
295 "weekly" if (arch == "aws" or itm["testtype"] != "mrr") else "daily"
298 f"{itm['testtype'] if itm['testtype'] == 'mrr' else 'ndrpdr'}-"
299 f"{cadence}-master-{topo}-{arch}"
301 df_sel = data.loc[(data["job"] == sel_topo_arch)]
303 f"^.*{nic}.*\.{itm['framesize']}-{itm['core']}-{drv}{itm['test']}-"
304 f"{'mrr' if itm['testtype'] == 'mrr' else 'ndrpdr'}$"
307 df_sel["test_id"].apply(
308 lambda x: True if re.search(regex, x) else False
310 ].sort_values(by="start_time", ignore_index=True)
312 f"{itm['phy']}-{itm['framesize']}-{itm['core']}-"
313 f"{itm['test']}-{itm['testtype']}"
316 traces = _generate_traces(
317 itm["testtype"], name, df, start, end, _COLORS[idx % len(_COLORS)]
321 fig_tput = go.Figure()
323 fig_tput.add_trace(trace)
325 if itm["testtype"] == "pdr":
326 traces = _generate_traces(
327 "pdr-lat", name, df, start, end, _COLORS[idx % len(_COLORS)]
331 fig_lat = go.Figure()
333 fig_lat.add_trace(trace)
336 fig_tput.update_layout(layout.get("plot-trending-tput", dict()))
338 fig_lat.update_layout(layout.get("plot-trending-lat", dict()))
340 return fig_tput, fig_lat