Ansible: i40e driver install
[csit.git] / resources / libraries / python / MLRsearch / ReceiveRateMeasurement.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 defining ReceiveRateMeasurement class."""
15
16
17 class ReceiveRateMeasurement:
18     """Structure defining the result of single Rr measurement."""
19
20     def __init__(
21             self, duration, target_tr, transmit_count, loss_count,
22             approximated_duration=0.0, partial_transmit_count=0,
23             effective_loss_ratio=None):
24         """Constructor, normalize primary and compute secondary quantities.
25
26         If approximated_duration is nonzero, it is stored.
27         If approximated_duration is zero, duration value is stored.
28         Either way, additional secondary quantities are computed
29         from the store value.
30
31         If there is zero transmit_count, ratios are set to zero.
32
33         In some cases, traffic generator does not attempt all the needed
34         transactions. In that case, nonzero partial_transmit_count
35         holds (an estimate of) count of the actually attempted transactions.
36         This is used to populate some secondary quantities.
37
38         TODO: Use None instead of zero?
39
40         Field effective_loss_ratio is specific for use in MLRsearch,
41         where measurements with lower loss ratio at higher target_tr
42         cannot be relied upon if there is a measurement with higher loss ratio
43         at lower target_tr. In this case, the higher loss ratio from
44         other measurement is stored as effective loss ratio in this measurement.
45         If None, the computed loss ratio of this measurement is used.
46         If not None, the computed ratio can still be apllied if it is larger.
47
48         :param duration: Measurement duration [s].
49         :param target_tr: Target transmit rate [pps].
50             If bidirectional traffic is measured, this is bidirectional rate.
51         :param transmit_count: Number of packets transmitted [1].
52         :param loss_count: Number of packets transmitted but not received [1].
53         :param approximated_duration: Estimate of the actual time of the trial.
54         :param partial_transmit_count: Estimate count of actually attempted
55             transactions.
56         :param effective_loss_ratio: None or highest loss ratio so far.
57         :type duration: float
58         :type target_tr: float
59         :type transmit_count: int
60         :type loss_count: int
61         :type approximated_duration: float
62         :type partial_transmit_count: int
63         """
64         self.duration = float(duration)
65         self.target_tr = float(target_tr)
66         self.transmit_count = int(transmit_count)
67         self.loss_count = int(loss_count)
68         self.receive_count = transmit_count - loss_count
69         self.transmit_rate = transmit_count / self.duration
70         self.loss_rate = loss_count / self.duration
71         self.receive_rate = self.receive_count / self.duration
72         self.loss_ratio = (
73             float(self.loss_count) / self.transmit_count
74             if self.transmit_count > 0 else 1.0
75         )
76         self.effective_loss_ratio = self.loss_ratio
77         if effective_loss_ratio is not None:
78             if effective_loss_ratio > self.loss_ratio:
79                 self.effective_loss_ratio = float(effective_loss_ratio)
80         self.receive_ratio = (
81             float(self.receive_count) / self.transmit_count
82             if self.transmit_count > 0 else 0.0
83         )
84         self.approximated_duration = (
85             float(approximated_duration) if approximated_duration
86             else self.duration
87         )
88         self.approximated_receive_rate = (
89             self.receive_count / self.approximated_duration
90             if self.approximated_duration > 0.0 else 0.0
91         )
92         # If the traffic generator is unreliable and sends less packets,
93         # the absolute receive rate might be too low for next target.
94         self.partial_transmit_count = (
95             int(partial_transmit_count) if partial_transmit_count
96             else self.transmit_count
97         )
98         self.partial_receive_ratio = (
99             float(self.receive_count) / self.partial_transmit_count
100             if self.partial_transmit_count > 0 else 0.0
101         )
102         self.partial_receive_rate = (
103             self.target_tr * self.partial_receive_ratio
104         )
105         # We use relative packet ratios in order to support cases
106         # where target_tr is in transactions per second,
107         # but there are multiple packets per transaction.
108         self.relative_receive_rate = (
109             self.target_tr * self.receive_count / self.transmit_count
110         )
111
112     def __str__(self):
113         """Return string reporting input and loss ratio."""
114         return f"d={self.duration!s},Tr={self.target_tr!s}," \
115             f"Df={self.loss_ratio!s}"
116
117     def __repr__(self):
118         """Return string evaluable as a constructor call."""
119         return f"ReceiveRateMeasurement(duration={self.duration!r}," \
120             f"target_tr={self.target_tr!r}," \
121             f"transmit_count={self.transmit_count!r}," \
122             f"loss_count={self.loss_count!r}," \
123             f"approximated_duration={self.approximated_duration!r}," \
124             f"partial_transmit_count={self.partial_transmit_count!r}," \
125             f"effective_loss_ratio={self.effective_loss_ratio!r})"