05e0f10013b4783a8ad4a9c33ffccd4c63e62f64
[csit.git] / resources / libraries / python / search / ReceiveRateInterval.py
1 # Copyright (c) 2018 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 ReceiveRateInterval class."""
15
16 import math
17
18 from ReceiveRateMeasurement import ReceiveRateMeasurement
19
20
21 class ReceiveRateInterval(object):
22     """Structure defining two Rr measurements, and their relation."""
23
24     def __init__(self, measured_low, measured_high):
25         """Store the bound measurements after checking argument types.
26
27         :param measured_low: Measurement for the lower bound.
28         :param measured_high: Measurement for the upper bound.
29         :type measured_low: ReceiveRateMeasurement
30         :type measured_high: ReceiveRateMeasurement
31         """
32         # TODO: Type checking is not very pythonic,
33         # perhaps users can fix wrong usage without it?
34         if not isinstance(measured_low, ReceiveRateMeasurement):
35             raise TypeError("measured_low is not a ReceiveRateMeasurement: "
36                             "{low!r}".format(low=measured_low))
37         if not isinstance(measured_high, ReceiveRateMeasurement):
38             raise TypeError("measured_high is not a ReceiveRateMeasurement: "
39                             "{high!r}".format(high=measured_high))
40         self.measured_low = measured_low
41         self.measured_high = measured_high
42         # Declare secondary quantities to appease pylint.
43         self.abs_tr_width = None
44         """Absolute width of target transmit rate. Upper minus lower."""
45         self.rel_tr_width = None
46         """Relative width of target transmit rate. Absolute divided by upper."""
47         self.sort()
48
49     def sort(self):
50         """Sort bounds by target Tr, compute secondary quantities."""
51         if self.measured_low.target_tr > self.measured_high.target_tr:
52             self.measured_low, self.measured_high = (
53                 self.measured_high, self.measured_low)
54         self.abs_tr_width = (
55             self.measured_high.target_tr - self.measured_low.target_tr)
56         self.rel_tr_width = self.abs_tr_width / self.measured_high.target_tr
57
58     def width_in_goals(self, relative_width_goal):
59         """Return float value.
60
61         Relative width goal is some (negative) value on logarithmic scale.
62         Current relative width is another logarithmic value.
63         Return the latter divided by the former.
64         This is useful when investigating how did surprising widths come to be.
65
66         :param relative_width_goal: Upper bound times this is the goal
67             difference between upper bound and lower bound.
68         :type relative_width_goal: float
69         :returns: Current width as logarithmic multiple of goal width [1].
70         :rtype: float
71         """
72         return math.log(1.0 - self.rel_tr_width) / math.log(
73             1.0 - relative_width_goal)
74
75     def __str__(self):
76         """Return string as half-open interval."""
77         return "[{low!s};{high!s})".format(
78             low=self.measured_low, high=self.measured_high)
79
80     def __repr__(self):
81         """Return string evaluable as a constructor call."""
82         return ("ReceiveRateInterval(measured_low={low!r}"
83                 ",measured_high={high!r})".format(
84                     low=self.measured_low, high=self.measured_high))

©2016 FD.io a Linux Foundation Collaborative Project. All Rights Reserved.
Linux Foundation is a registered trademark of The Linux Foundation. Linux is a registered trademark of Linus Torvalds.
Please see our privacy policy and terms of use.