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:
6 # http://www.apache.org/licenses/LICENSE-2.0
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.
14 """Module defining ReceiveRateInterval class."""
19 class ReceiveRateInterval:
20 """Structure defining two Rr measurements, and their relation."""
22 def __init__(self, measured_low, measured_high):
23 """Store the bound measurements and call sort.
25 :param measured_low: Measurement for the lower bound.
26 :param measured_high: Measurement for the upper bound.
27 :type measured_low: ReceiveRateMeasurement.ReceiveRateMeasurement
28 :type measured_high: ReceiveRateMeasurement.ReceiveRateMeasurement
30 self.measured_low = measured_low
31 self.measured_high = measured_high
32 # Declare secondary quantities to appease pylint.
33 self.abs_tr_width = None
34 """Absolute width of target transmit rate. Upper minus lower."""
35 self.rel_tr_width = None
36 """Relative width of target transmit rate. Absolute divided by upper."""
40 """Sort bounds by target Tr, compute secondary quantities."""
41 if self.measured_low.target_tr > self.measured_high.target_tr:
42 self.measured_low, self.measured_high = (
43 self.measured_high, self.measured_low
46 self.measured_high.target_tr - self.measured_low.target_tr
48 self.rel_tr_width = self.abs_tr_width / self.measured_high.target_tr
51 """Return string as half-open interval."""
52 return f"[{self.measured_low!s};{self.measured_high!s})"
55 """Return string evaluable as a constructor call."""
56 return f"ReceiveRateInterval(measured_low={self.measured_low!r}," \
57 f"measured_high={self.measured_high!r})"
59 def width_in_goals(self, relative_width_goal):
60 """Return float value.
62 Relative width goal is some (negative) value on logarithmic scale.
63 Current relative width is another logarithmic value.
64 Return the latter divided by the former.
65 This is useful when investigating how did surprising widths come to be.
67 :param relative_width_goal: Upper bound times this is the goal
68 difference between upper bound and lower bound.
69 :type relative_width_goal: float
70 :returns: Current width as logarithmic multiple of goal width [1].
73 return math.log(1.0 - self.rel_tr_width) / math.log(
74 1.0 - relative_width_goal)