d74a6ab5df66d5486ed5358cd7f14591def9c9ae
[csit.git] / resources / libraries / python / model / ExportResult.py
1 # Copyright (c) 2021 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 """Module with keywords that publish parts of result structure."""
15
16 from robot.libraries.BuiltIn import BuiltIn
17
18 from resources.libraries.python.model.util import descend, get_export_data
19
20
21 def export_dut_type_and_version(dut_type=u"unknown", dut_version=u"unknown"):
22     """Export the arguments as dut type and version.
23
24     Robot tends to convert "none" into None, hence the unusual default values.
25
26     If either argument is missing, the value from robot variable is used.
27     If argument is present, the value is also stored to robot suite variable.
28
29     :param dut_type: DUT type, e.g. VPP or DPDK.
30     :param dut_version: DUT version as determined by the caller.
31     :type dut_type: Optional[str]
32     :type dut_version: Optiona[str]
33     :raises RuntimeError: If value is neither in argument not robot variable.
34     """
35     if dut_type == u"unknown":
36         dut_type = BuiltIn().get_variable_value(u"\\${DUT_TYPE}", u"unknown")
37         if dut_type == u"unknown":
38             raise RuntimeError(u"Dut type not provided.")
39     else:
40         # We want to set a variable in higher level suite setup
41         # to be available to test setup several levels lower.
42         # Documentation [0] looks like "children" is a keyword argument,
43         # but code [1] lines 1458 and 1511-1512 show
44         # it is just last stringy argument.
45         # [0] http://robotframework.org/robotframework/
46         #     3.1.2/libraries/BuiltIn.html#Set%20Suite%20Variable
47         # [1] https://github.com/robotframework/robotframework/blob/
48         #     v3.1.2/src/robot/libraries/BuiltIn.py
49         BuiltIn().set_suite_variable(
50             u"\\${DUT_TYPE}", dut_type, u"children=True"
51         )
52     if dut_version == u"unknown":
53         dut_version = BuiltIn().get_variable_value(u"\\${DUT_VERSION}", u"unknown")
54         if dut_type == u"unknown":
55             raise RuntimeError(u"Dut version not provided.")
56     else:
57         BuiltIn().set_suite_variable(
58             u"\\${DUT_VERSION}", dut_version, u"children=True"
59         )
60     data = get_export_data()
61     data[u"dut_type"] = dut_type
62     data[u"dut_version"] = dut_version
63
64
65 def append_mrr_value(mrr_value, unit):
66     """Store mrr value to proper place so it is dumped into json.
67
68     The value is appended only when unit is not empty.
69
70     :param mrr_value: Forwarding rate from MRR trial.
71     :param unit: Unit of measurement for the rate.
72     :type mrr_value: float
73     :type unit: str
74     """
75     if not unit:
76         return
77     data = get_export_data()
78     data[u"result"][u"type"] = u"mrr"
79     rate_node = descend(descend(data[u"result"], u"receive_rate"), "rate")
80     rate_node[u"unit"] = str(unit)
81     values_list = descend(rate_node, u"values", list)
82     values_list.append(float(mrr_value))
83     # TODO: Fill in the bandwidth part for pps?
84
85
86 def export_search_bound(text, value, unit, bandwidth=None):
87     """Store bound value and unit.
88
89     This function works for both NDRPDR and SOAK, decided by text.
90
91     If a node does not exist, it is created.
92     If a previous value exists, it is overwritten silently.
93     Result type is set (overwritten) to ndrpdr (or soak).
94
95     Text is used to determine whether it is ndr or pdr, upper or lower bound,
96     as the Robot caller has the information only there.
97
98     :param text: Info from Robot caller to determime bound type.
99     :param value: The bound value in packets (or connections) per second.
100     :param unit: Rate unit the bound is measured (or estimated) in.
101     :param bandwidth: The same value recomputed into L1 bits per second.
102     :type text: str
103     :type value: float
104     :type unit: str
105     :type bandwidth: Optional[float]
106     """
107     value = float(value)
108     text = str(text).lower()
109     result_type = u"soak" if u"plrsearch" in text else u"ndrpdr"
110     upper_or_lower = u"upper" if u"upper" in text else u"lower"
111     ndr_or_pdr = u"ndr" if u"ndr" in text else u"pdr"
112
113     data = get_export_data()
114     result_node = data[u"result"]
115     result_node[u"type"] = result_type
116     rate_item = dict(rate=dict(value=value, unit=unit))
117     if bandwidth:
118         rate_item[u"bandwidth"] = dict(value=float(bandwidth), unit=u"bps")
119     if result_type == u"soak":
120         descend(result_node, u"critical_rate")[upper_or_lower] = rate_item
121         return
122     descend(result_node, ndr_or_pdr)[upper_or_lower] = rate_item
123
124
125 def _add_latency(result_node, percent, whichward, latency_string):
126     """Descend to a corresponding node and add values from latency string.
127
128     This is an internal block, moved out from export_ndrpdr_latency,
129     as it can be called up to 4 times.
130
131     :param result_node: UTI tree node to descend from.
132     :param percent: Percent value to use in node key (90, 50, 10, 0).
133     :param whichward: "forward" or "reverse".
134     :param latency_item: Unidir output from TRex utility, min/avg/max/hdrh.
135     :type result_node: dict
136     :type percent: int
137     :type whichward: str
138     :latency_string: str
139     """
140     l_min, l_avg, l_max, l_hdrh = latency_string.split(u"/", 3)
141     whichward_node = descend(result_node, f"latency_{whichward}")
142     percent_node = descend(whichward_node, f"pdr_{percent}")
143     percent_node[u"min"] = int(l_min)
144     percent_node[u"avg"] = int(l_avg)
145     percent_node[u"max"] = int(l_max)
146     percent_node[u"hdrh"] = l_hdrh
147     percent_node[u"unit"] = u"us"
148
149
150 def export_ndrpdr_latency(text, latency):
151     """Store NDRPDR hdrh latency data.
152
153     If "latency" node does not exist, it is created.
154     If a previous value exists, it is overwritten silently.
155
156     Text is used to determine what percentage of PDR is the load,
157     as the Robot caller has the information only there.
158
159     Reverse data may be missing, we assume the test was unidirectional.
160
161     :param text: Info from Robot caller to determime load.
162     :param latency: Output from TRex utility, min/avg/max/hdrh.
163     :type text: str
164     :type latency: 1-tuple or 2-tuple of str
165     """
166     data = get_export_data()
167     result_node = data[u"result"]
168     percent = 0
169     if u"90" in text:
170         percent = 90
171     elif u"50" in text:
172         percent = 50
173     elif u"10" in text:
174         percent = 10
175     _add_latency(result_node, percent, u"forward", latency[0])
176     # Else TRex does not support latency measurement for this traffic profile.
177     if len(latency) < 2:
178         return
179     _add_latency(result_node, percent, u"reverse", latency[1])