31a6f8202e0cddf6ff3f755710e21137c84ac413
[csit.git] / resources / libraries / python / MLRsearch / ReceiveRateMeasurement.py
1 # Copyright (c) 2019 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__(self, duration, target_tr, transmit_count, loss_count):
21         """Constructor, normalize primary and compute secondary quantities.
22
23         :param duration: Measurement duration [s].
24         :param target_tr: Target transmit rate [pps].
25             If bidirectional traffic is measured, this is bidirectional rate.
26         :param transmit_count: Number of packets transmitted [1].
27         :param loss_count: Number of packets transmitted but not received [1].
28         :type duration: float
29         :type target_tr: float
30         :type transmit_count: int
31         :type loss_count: int
32         """
33         self.duration = float(duration)
34         self.target_tr = float(target_tr)
35         self.transmit_count = int(transmit_count)
36         self.loss_count = int(loss_count)
37         self.receive_count = transmit_count - loss_count
38         self.transmit_rate = transmit_count / self.duration
39         self.loss_rate = loss_count / self.duration
40         self.receive_rate = self.receive_count / self.duration
41         self.loss_fraction = float(self.loss_count) / self.transmit_count
42         # TODO: Do we want to store also the real time (duration + overhead)?
43
44     def __str__(self):
45         """Return string reporting input and loss fraction."""
46         return f"d={self.duration!s},Tr={self.target_tr!s}," \
47             f"Df={self.loss_fraction!s}"
48
49     def __repr__(self):
50         """Return string evaluable as a constructor call."""
51         return f"ReceiveRateMeasurement(duration={self.duration!r}," \
52             f"target_tr={self.target_tr!r}," \
53             f"transmit_count={self.transmit_count!r}," \
54             f"loss_count={self.loss_count!r})"