Merge "Adding example testcase to enable multiport support for http"
[yardstick.git] / yardstick / network_services / traffic_profile / prox_binsearch.py
1 # Copyright (c) 2016-2017 Intel Corporation
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #      http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 """ Fixed traffic profile definitions """
15
16 from __future__ import absolute_import
17
18 import logging
19
20 from yardstick.network_services.traffic_profile.prox_profile import ProxProfile
21
22 LOG = logging.getLogger(__name__)
23
24
25 class ProxBinSearchProfile(ProxProfile):
26     """
27     This profile adds a single stream at the beginning of the traffic session
28     """
29
30     def __init__(self, tp_config):
31         super(ProxBinSearchProfile, self).__init__(tp_config)
32         self.current_lower = self.lower_bound
33         self.current_upper = self.upper_bound
34
35     @property
36     def delta(self):
37         return self.current_upper - self.current_lower
38
39     @property
40     def mid_point(self):
41         return (self.current_lower + self.current_upper) / 2
42
43     def bounds_iterator(self, logger=None):
44         self.current_lower = self.lower_bound
45         self.current_upper = self.upper_bound
46
47         test_value = self.current_upper
48         while abs(self.delta) >= self.precision:
49             if logger:
50                 logger.debug("New interval [%s, %s), precision: %d", self.current_lower,
51                              self.current_upper, self.step_value)
52                 logger.info("Testing with value %s", test_value)
53
54             yield test_value
55             test_value = self.mid_point
56
57     def run_test_with_pkt_size(self, traffic_gen, pkt_size, duration):
58         """Run the test for a single packet size.
59
60         :param traffic_gen: traffic generator instance
61         :type traffic_gen: TrafficGen
62         :param  pkt_size: The packet size to test with.
63         :type pkt_size: int
64         :param  duration: The duration for each try.
65         :type duration: int
66
67         """
68
69         LOG.info("Testing with packet size %d", pkt_size)
70
71         # Binary search assumes the lower value of the interval is
72         # successful and the upper value is a failure.
73         # The first value that is tested, is the maximum value. If that
74         # succeeds, no more searching is needed. If it fails, a regular
75         # binary search is performed.
76         #
77         # The test_value used for the first iteration of binary search
78         # is adjusted so that the delta between this test_value and the
79         # upper bound is a power-of-2 multiple of precision. In the
80         # optimistic situation where this first test_value results in a
81         # success, the binary search will complete on an integer multiple
82         # of the precision, rather than on a fraction of it.
83
84         # throughput and packet loss from the most recent successful test
85         successful_pkt_loss = 0.0
86         for test_value in self.bounds_iterator(LOG):
87             result, port_samples = self._profile_helper.run_test(pkt_size, duration,
88                                                                  test_value, self.tolerated_loss)
89
90             if result.success:
91                 LOG.debug("Success! Increasing lower bound")
92                 self.current_lower = test_value
93                 successful_pkt_loss = result.pkt_loss
94             else:
95                 LOG.debug("Failure... Decreasing upper bound")
96                 self.current_upper = test_value
97
98             samples = result.get_samples(pkt_size, successful_pkt_loss, port_samples)
99             self.queue.put(samples)