Fix warnings reported by gen_doc.sh
[csit.git] / resources / libraries / python / DropRateSearch.py
index 354e7d4..912d2e9 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (c) 2016 Cisco and/or its affiliates.
+# Copyright (c) 2018 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:
@@ -109,7 +109,7 @@ class DropRateSearch(object):
 
     @abstractmethod
     def measure_loss(self, rate, frame_size, loss_acceptance,
-                     loss_acceptance_type, traffic_type):
+                     loss_acceptance_type, traffic_type, skip_warmup=False):
         """Send traffic from TG and measure count of dropped frames.
 
         :param rate: Offered traffic load.
@@ -117,13 +117,15 @@ class DropRateSearch(object):
         :param loss_acceptance: Permitted drop ratio or frames count.
         :param loss_acceptance_type: Type of permitted loss.
         :param traffic_type: Traffic profile ([2,3]-node-L[2,3], ...).
+        :param skip_warmup: Start TRex without warmup traffic if true.
         :type rate: int
         :type frame_size: str
         :type loss_acceptance: float
         :type loss_acceptance_type: LossAcceptanceType
         :type traffic_type: str
+        :type traffic_type: bool
         :returns: Drop threshold exceeded? (True/False)
-        :rtype bool
+        :rtype: bool
         """
         pass
 
@@ -135,7 +137,7 @@ class DropRateSearch(object):
         :type max_rate: float
         :type min_rate: float
         :returns: nothing
-        :raises: ValueError if min rate is lower than 0 and higher than max rate
+        :raises ValueError: If min rate is lower than 0 or higher than max rate.
         """
         if float(min_rate) <= 0:
             raise ValueError("min_rate must be higher than 0")
@@ -151,7 +153,7 @@ class DropRateSearch(object):
         :param loss_acceptance: Loss acceptance treshold for PDR search.
         :type loss_acceptance: str
         :returns: nothing
-        :raises: ValueError if loss acceptance is lower than zero
+        :raises ValueError: If loss acceptance is lower than zero.
         """
         if float(loss_acceptance) < 0:
             raise ValueError("Loss acceptance must be higher or equal 0")
@@ -225,7 +227,7 @@ class DropRateSearch(object):
         :param rate_type: Type of rate to set.
         :type rate_type: RateType
         :returns: nothing
-        :raises: Exception if rate type is unknown
+        :raises Exception: If rate type is unknown.
         """
         if rate_type not in RateType:
             raise Exception("rate_type unknown: {}".format(rate_type))
@@ -280,7 +282,7 @@ class DropRateSearch(object):
 
         :returns: String representation of rate type.
         :rtype: str
-        :raises: ValueError if rate type is unknown
+        :raises ValueError: If rate type is unknown.
         """
         if self._rate_type == RateType.PERCENTAGE:
             return "%"
@@ -297,7 +299,7 @@ class DropRateSearch(object):
         :param max_attempts: Number of traffic runs.
         :type max_attempts: int
         :returns: nothing
-        :raises: ValueError if max attempts is lower than zero
+        :raises ValueError: If max attempts is lower than zero.
         """
         if int(max_attempts) > 0:
             self._max_attempts = int(max_attempts)
@@ -332,7 +334,7 @@ class DropRateSearch(object):
         :param search_type: Type of search result evaluation to set.
         :type search_type: SearchResultType
         :returns: nothing
-        :raises: ValueError if search type is unknown
+        :raises ValueError: If search type is unknown.
         """
         if search_type not in SearchResultType:
             raise ValueError("search_type unknown: {}".format(search_type))
@@ -370,7 +372,7 @@ class DropRateSearch(object):
         :type res_list: list
         :returns: Boolean based on search result type.
         :rtype: boolean
-        :raises: ValueError if search result type is unknown
+        :raises ValueError: If search result type is unknown.
         """
         if self._search_result_type == SearchResultType.BEST_OF_N:
             return self._get_best_of_n(res_list)
@@ -387,7 +389,7 @@ class DropRateSearch(object):
         :type start_rate: float
         :type traffic_type: str
         :returns: nothing
-        :raises: ValueError if start rate is not in range
+        :raises ValueError: If start rate is not in range.
         """
 
         if not self._rate_min <= float(start_rate) <= self._rate_max:
@@ -415,12 +417,11 @@ class DropRateSearch(object):
                     if prev_rate is None:
                         self._search_result = SearchResults.FAILURE
                         self._search_result_rate = None
-                        return
                     # else we found the rate, which is value from previous run
                     else:
                         self._search_result = SearchResults.SUCCESS
                         self._search_result_rate = prev_rate
-                        return
+                    return
                 # there was no loss / loss below acceptance criteria
                 elif res:
                     prev_rate = rate
@@ -472,7 +473,7 @@ class DropRateSearch(object):
 
         :returns: Result rate and latency stats.
         :rtype: tuple
-        :raises: Exception if search failed
+        :raises Exception: If search failed.
         """
         if self._search_result == SearchResults.FAILURE:
             raise Exception('Search FAILED')
@@ -480,19 +481,22 @@ class DropRateSearch(object):
                                      SearchResults.SUSPICIOUS]:
             return self._search_result_rate, self.get_latency()
 
-    def binary_search(self, b_min, b_max, traffic_type, skip_max_rate=False):
+    def binary_search(self, b_min, b_max, traffic_type, skip_max_rate=False,
+                      skip_warmup=False):
         """Binary search of rate with loss below acceptance criteria.
 
         :param b_min: Min range rate.
         :param b_max: Max range rate.
         :param traffic_type: Traffic profile.
         :param skip_max_rate: Start with max rate first
+        :param skip_warmup: Start TRex without warmup traffic if true.
         :type b_min: float
         :type b_max: float
         :type traffic_type: str
         :type skip_max_rate: bool
+        :type skip_warmup: bool
         :returns: nothing
-        :raises: ValueError if input values are not valid
+        :raises ValueError: If input values are not valid.
         """
 
         if not self._rate_min <= float(b_min) <= self._rate_max:
@@ -502,22 +506,17 @@ class DropRateSearch(object):
         if float(b_max) < float(b_min):
             raise ValueError("Min rate is greater than max rate")
 
-        # binary search
-        if skip_max_rate:
-            # rate is half of interval + start of interval
-            rate = ((float(b_max) - float(b_min)) / 2) + float(b_min)
-        else:
-            # rate is max of interval
-            rate = float(b_max)
+        # rate is half of interval + start of interval if not using max rate
+        rate = ((float(b_max) - float(b_min)) / 2) + float(b_min) \
+            if skip_max_rate else float(b_max)
+
         # rate diff with previous run
         rate_diff = abs(self._last_binary_rate - rate)
 
         # convergence criterium
         if float(rate_diff) < float(self._binary_convergence_threshold):
-            if not self._search_result_rate:
-                self._search_result = SearchResults.FAILURE
-            else:
-                self._search_result = SearchResults.SUCCESS
+            self._search_result = SearchResults.SUCCESS \
+                if self._search_result_rate else SearchResults.FAILURE
             return
 
         self._last_binary_rate = rate
@@ -527,17 +526,17 @@ class DropRateSearch(object):
             res.append(self.measure_loss(rate, self._frame_size,
                                          self._loss_acceptance,
                                          self._loss_acceptance_type,
-                                         traffic_type))
+                                         traffic_type, skip_warmup=skip_warmup))
 
         res = self._get_res_based_on_search_type(res)
 
         # loss occurred and it was above acceptance criteria
         if not res:
-            self.binary_search(b_min, rate, traffic_type, True)
+            self.binary_search(b_min, rate, traffic_type, True, True)
         # there was no loss / loss below acceptance criteria
         else:
             self._search_result_rate = rate
-            self.binary_search(rate, b_max, traffic_type, True)
+            self.binary_search(rate, b_max, traffic_type, True, True)
 
     def combined_search(self, start_rate, traffic_type):
         """Combined search of rate with loss below acceptance criteria.
@@ -547,7 +546,7 @@ class DropRateSearch(object):
         :type start_rate: float
         :type traffic_type: str
         :returns: nothing
-        :raises: RuntimeError if linear search failed
+        :raises RuntimeError: If linear search failed.
         """
 
         self.linear_search(start_rate, traffic_type)
@@ -597,7 +596,7 @@ class DropRateSearch(object):
         :returns: Returns True if num_a is close in value to num_b or equal.
                  False otherwise.
         :rtype: boolean
-        :raises: ValueError if input values are not valid
+        :raises ValueError: If input values are not valid.
         """
 
         if num_a == num_b:
@@ -608,4 +607,3 @@ class DropRateSearch(object):
 
         return abs(num_b - num_a) <= max(rel_tol * max(abs(num_a), abs(num_b)),
                                          abs_tol)
-