aboutsummaryrefslogtreecommitdiffstats
path: root/resources/libraries/python/model/ExportResult.py
blob: f1558489133841b1f5023abe3007b3673b0a6d3f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
# Copyright (c) 2023 Cisco and/or its affiliates.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""Module with keywords that publish parts of result structure."""

from robot.libraries.BuiltIn import BuiltIn

from resources.libraries.python.model.util import descend, get_export_data


def export_dut_type_and_version(dut_type="unknown", dut_version="unknown"):
    """Export the arguments as dut type and version.

    Robot tends to convert "none" into None, hence the unusual default values.

    If either argument is missing, the value from robot variable is used.
    If argument is present, the value is also stored to robot suite variable.

    :param dut_type: DUT type, e.g. VPP or DPDK.
    :param dut_version: DUT version as determined by the caller.
    :type dut_type: Optional[str]
    :type dut_version: Optiona[str]
    :raises RuntimeError: If value is neither in argument not robot variable.
    """
    if dut_type == "unknown":
        dut_type = BuiltIn().get_variable_value("\\${DUT_TYPE}", "unknown")
        if dut_type == "unknown":
            raise RuntimeError("Dut type not provided.")
    else:
        # We want to set a variable in higher level suite setup
        # to be available to test setup several levels lower.
        BuiltIn().set_suite_variable(
            "\\${DUT_TYPE}", dut_type, "children=True"
        )
    if dut_version == "unknown":
        dut_version = BuiltIn().get_variable_value(
            "\\${DUT_VERSION}", "unknown"
        )
        if dut_type == "unknown":
            raise RuntimeError("Dut version not provided.")
    else:
        BuiltIn().set_suite_variable(
            "\\${DUT_VERSION}", dut_version, "children=True"
        )
    data = get_export_data()
    data["dut_type"] = dut_type.lower()
    data["dut_version"] = dut_version


def export_tg_type_and_version(tg_type="unknown", tg_version="unknown"):
    """Export the arguments as tg type and version.

    Robot tends to convert "none" into None, hence the unusual default values.

    If either argument is missing, the value from robot variable is used.
    If argument is present, the value is also stored to robot suite variable.

    :param tg_type: TG type, e.g. TREX.
    :param tg_version: TG version as determined by the caller.
    :type tg_type: Optional[str]
    :type tg_version: Optiona[str]
    :raises RuntimeError: If value is neither in argument not robot variable.
    """
    if tg_type == "unknown":
        tg_type = BuiltIn().get_variable_value("\\${TG_TYPE}", "unknown")
        if tg_type == "unknown":
            raise RuntimeError("TG type not provided!")
    else:
        # We want to set a variable in higher level suite setup
        # to be available to test setup several levels lower.
        BuiltIn().set_suite_variable(
            "\\${TG_TYPE}", tg_type, "children=True"
        )
    if tg_version == "unknown":
        tg_version = BuiltIn().get_variable_value(
            "\\${TG_VERSION}", "unknown"
        )
        if tg_type == "unknown":
            raise RuntimeError("TG version not provided!")
    else:
        BuiltIn().set_suite_variable(
            "\\${TG_VERSION}", tg_version, "children=True"
        )
    data = get_export_data()
    data["tg_type"] = tg_type.lower()
    data["tg_version"] = tg_version


def append_mrr_value(mrr_value, mrr_unit, bandwidth_value=None,
        bandwidth_unit="bps"):
    """Store mrr value to proper place so it is dumped into json.

    The value is appended only when unit is not empty.

    :param mrr_value: Forwarding rate from MRR trial.
    :param mrr_unit: Unit of measurement for the rate.
    :param bandwidth_value: The same value recomputed into L1 bits per second.
    :type mrr_value: float
    :type mrr_unit: str
    :type bandwidth_value: Optional[float]
    :type bandwidth_unit: Optional[str]
    """
    if not mrr_unit:
        return
    data = get_export_data()
    data["result"]["type"] = "mrr"

    for node_val, node_unit, node_name in ((mrr_value, mrr_unit, "rate"),
            (bandwidth_value, bandwidth_unit, "bandwidth")):
        if node_val is not None:
            node = descend(descend(data["result"], "receive_rate"), node_name)
            node["unit"] = str(node_unit)
            values_list = descend(node, "values", list)
            values_list.append(float(node_val))


def export_search_bound(text, value, unit, bandwidth=None):
    """Store bound value and unit.

    This function works for both NDRPDR and SOAK, decided by text.

    If a node does not exist, it is created.
    If a previous value exists, it is overwritten silently.
    Result type is set (overwritten) to ndrpdr (or soak).

    Text is used to determine whether it is ndr or pdr, upper or lower bound,
    as the Robot caller has the information only there.

    :param text: Info from Robot caller to determime bound type.
    :param value: The bound value in packets (or connections) per second.
    :param unit: Rate unit the bound is measured (or estimated) in.
    :param bandwidth: The same value recomputed into L1 bits per second.
    :type text: str
    :type value: float
    :type unit: str
    :type bandwidth: Optional[float]
    """
    value = float(value)
    text = str(text).lower()
    result_type = "soak" if "plrsearch" in text else "ndrpdr"
    upper_or_lower = "upper" if "upper" in text else "lower"
    ndr_or_pdr = "ndr" if "ndr" in text else "pdr"

    result_node = get_export_data()["result"]
    result_node["type"] = result_type
    rate_item = dict(rate=dict(value=value, unit=unit))
    if bandwidth:
        rate_item["bandwidth"] = dict(value=float(bandwidth), unit="bps")
    if result_type == "soak":
        descend(result_node, "critical_rate")[upper_or_lower] = rate_item
        return
    descend(result_node, ndr_or_pdr)[upper_or_lower] = rate_item


def _add_latency(result_node, percent, whichward, latency_string):
    """Descend to a corresponding node and add values from latency string.

    This is an internal block, moved out from export_ndrpdr_latency,
    as it can be called up to 4 times.

    :param result_node: UTI tree node to descend from.
    :param percent: Percent value to use in node key (90, 50, 10, 0).
    :param whichward: "forward" or "reverse".
    :param latency_item: Unidir output from TRex utility, min/avg/max/hdrh.
    :type result_node: dict
    :type percent: int
    :type whichward: str
    :latency_string: str
    """
    l_min, l_avg, l_max, l_hdrh = latency_string.split("/", 3)
    whichward_node = descend(result_node, f"latency_{whichward}")
    percent_node = descend(whichward_node, f"pdr_{percent}")
    percent_node["min"] = int(l_min)
    percent_node["avg"] = int(l_avg)
    percent_node["max"] = int(l_max)
    percent_node["hdrh"] = l_hdrh
    percent_node["unit"] = "us"


def export_ndrpdr_latency(text, latency):
    """Store NDRPDR hdrh latency data.

    If "latency" node does not exist, it is created.
    If a previous value exists, it is overwritten silently.

    Text is used to determine what percentage of PDR is the load,
    as the Robot caller has the information only there.

    Reverse data may be missing, we assume the test was unidirectional.

    :param text: Info from Robot caller to determime load.
    :param latency: Output from TRex utility, min/avg/max/hdrh.
    :type text: str
    :type latency: 1-tuple or 2-tuple of str
    """
    result_node = get_export_data()["result"]
    percent = 0
    if "90" in text:
        percent = 90
    elif "50" in text:
        percent = 50
    elif "10" in text:
        percent = 10
    _add_latency(result_node, percent, "forward", latency[0])
    # Else TRex does not support latency measurement for this traffic profile.
    if len(latency) < 2:
        return
    _add_latency(result_node, percent, "reverse", latency[1])


def export_reconf_result(packet_rate, packet_loss, bandwidth):
    """Export the RECONF type results.

    Result type is set to reconf.

    :param packet_rate: Aggregate offered load in packets per second.
    :param packet_loss: How many of the packets were dropped or unsent.
    :param bandwidth: The offered load recomputed into L1 bits per second.
    :type packet_rate: float
    :type packet_loss: int
    :type bandwidth: float
    """
    result_node = get_export_data()["result"]
    result_node["type"] = "reconf"

    time_loss = int(packet_loss) / float(packet_rate)
    result_node["aggregate_rate"] = dict(
        bandwidth=dict(
            unit="bps",
            value=float(bandwidth)
        ),
        rate=dict(
            unit="pps",
            value=float(packet_rate)
        )
    )
    result_node["loss"] = dict(
        packet=dict(
            unit="packets",
            value=int(packet_loss)
        ),
        time=dict(
            unit="s",
            value=time_loss
        )
    )


def export_hoststack_results(
        bandwidth, rate=None, rate_unit=None, latency=None,
        failed_requests=None, completed_requests=None, retransmits=None,
        duration=None
):
    """Export the HOSTSTACK type results.

    Result type is set to hoststack.

    :param bandwidth: Measured transfer rate using bps as a unit.
    :param rate: Resulting rate measured by the test. [Optional]
    :param rate_unit: CPS or RPS. [Optional]
    :param latency: Measure latency. [Optional]
    :param failed_requests: Number of failed requests. [Optional]
    :param completed_requests: Number of completed requests. [Optional]
    :param retransmits: Retransmitted TCP packets. [Optional]
    :param duration: Measurment duration. [Optional]
    :type bandwidth: float
    :type rate: float
    :type rate_unit: str
    :type latency: float
    :type failed_requests: int
    :type completed_requests: int
    :type retransmits: int
    :type duration: float
    """
    result_node = get_export_data()["result"]
    result_node["type"] = "hoststack"

    result_node["bandwidth"] = dict(unit="bps", value=bandwidth)
    if rate is not None:
        result_node["rate"] = \
            dict(unit=rate_unit, value=rate)
    if latency is not None:
        result_node["latency"] = \
            dict(unit="ms", value=latency)
    if failed_requests is not None:
        result_node["failed_requests"] = \
            dict(unit="requests", value=failed_requests)
    if completed_requests is not None:
        result_node["completed_requests"] = \
            dict(unit="requests", value=completed_requests)
    if retransmits is not None:
        result_node["retransmits"] = \
            dict(unit="packets", value=retransmits)
    if duration is not None:
        result_node["duration"] = \
            dict(unit="s", value=duration)


def append_telemetry(telemetry_item):
    """Append telemetry entry to proper place so it is dumped into json.

    :param telemetry_item: Telemetry entry.
    :type telemetry_item: str
    """
    data = get_export_data()
    data["telemetry"].append(telemetry_item)