Merge "Fix setting `flow` configuration in TC yaml"
[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 import datetime
20 import time
21
22 from yardstick.network_services.traffic_profile.prox_profile import ProxProfile
23 from yardstick.network_services import constants
24 from yardstick.common import constants as overall_constants
25
26 LOG = logging.getLogger(__name__)
27
28 STATUS_SUCCESS = "Success"
29 STATUS_FAIL = "Failure"
30 STATUS_RESULT = "Result"
31 STEP_CONFIRM = "Confirm retry"
32 STEP_INCREASE_LOWER = "Increase lower"
33 STEP_DECREASE_LOWER = "Decrease lower"
34 STEP_DECREASE_UPPER = "Decrease upper"
35
36
37 class ProxBinSearchProfile(ProxProfile):
38     """
39     This profile adds a single stream at the beginning of the traffic session
40     """
41
42     def __init__(self, tp_config):
43         super(ProxBinSearchProfile, self).__init__(tp_config)
44         self.current_lower = self.lower_bound
45         self.current_upper = self.upper_bound
46
47     @property
48     def delta(self):
49         return self.current_upper - self.current_lower
50
51     @property
52     def mid_point(self):
53         return (self.current_lower + self.current_upper) / 2
54
55     def bounds_iterator(self, logger=None):
56         self.current_lower = self.lower_bound
57         self.current_upper = self.upper_bound
58
59         test_value = self.current_upper
60         while abs(self.delta) >= self.precision:
61             if logger:
62                 logger.debug("New interval [%s, %s), precision: %d", self.current_lower,
63                              self.current_upper, self.step_value)
64                 logger.info("Testing with value %s", test_value)
65
66             yield test_value
67             test_value = self.mid_point
68
69     def run_test_with_pkt_size(self, traffic_gen, pkt_size, duration):
70         """Run the test for a single packet size.
71
72         :param traffic_gen: traffic generator instance
73         :type traffic_gen: TrafficGen
74         :param  pkt_size: The packet size to test with.
75         :type pkt_size: int
76         :param  duration: The duration for each try.
77         :type duration: int
78
79         """
80
81         LOG.info("Testing with packet size %d", pkt_size)
82
83         # Binary search assumes the lower value of the interval is
84         # successful and the upper value is a failure.
85         # The first value that is tested, is the maximum value. If that
86         # succeeds, no more searching is needed. If it fails, a regular
87         # binary search is performed.
88         #
89         # The test_value used for the first iteration of binary search
90         # is adjusted so that the delta between this test_value and the
91         # upper bound is a power-of-2 multiple of precision. In the
92         # optimistic situation where this first test_value results in a
93         # success, the binary search will complete on an integer multiple
94         # of the precision, rather than on a fraction of it.
95
96         theor_max_thruput = 0
97
98         result_samples = {}
99
100         test_data = {
101             "test_duration": traffic_gen.scenario_helper.scenario_cfg["runner"]["duration"],
102             "test_precision": self.params["traffic_profile"]["test_precision"],
103             "tolerated_loss": self.params["traffic_profile"]["tolerated_loss"],
104             "duration": duration
105         }
106         self.prev_time = time.time()
107
108         # throughput and packet loss from the most recent successful test
109         successful_pkt_loss = 0.0
110         line_speed = traffic_gen.scenario_helper.all_options.get(
111             "interface_speed_gbps", constants.NIC_GBPS_DEFAULT) * constants.ONE_GIGABIT_IN_BITS
112
113         ok_retry = traffic_gen.scenario_helper.scenario_cfg["runner"].get("confirmation", 0)
114         for step_id, test_value in enumerate(self.bounds_iterator(LOG)):
115             pos_retry = 0
116             neg_retry = 0
117             total_retry = 0
118
119             LOG.info("Checking MAX %s MIN %s TEST %s", self.current_upper,
120                      self.lower_bound, test_value)
121
122             while (pos_retry <= ok_retry) and (neg_retry <= ok_retry):
123
124                 total_retry = total_retry + 1
125
126                 result, port_samples = self._profile_helper.run_test(pkt_size, duration,
127                                                                      test_value,
128                                                                      self.tolerated_loss,
129                                                                      line_speed)
130
131                 if (total_retry > (ok_retry * 3)) and (ok_retry is not 0):
132                     status = STATUS_FAIL
133                     next_step = STEP_DECREASE_LOWER
134                     successful_pkt_loss = result.pkt_loss
135                     self.current_upper = test_value
136                     neg_retry = total_retry
137                 elif result.success:
138                     if (pos_retry < ok_retry) and (ok_retry is not 0):
139                         status = STATUS_SUCCESS
140                         next_step = STEP_CONFIRM
141                         successful_pkt_loss = result.pkt_loss
142                         neg_retry = 0
143                     else:
144                         status = STATUS_SUCCESS
145                         next_step = STEP_INCREASE_LOWER
146                         self.current_lower = test_value
147                         successful_pkt_loss = result.pkt_loss
148
149                     pos_retry = pos_retry + 1
150
151                 else:
152                     if (neg_retry < ok_retry) and (ok_retry is not 0):
153                         status = STATUS_FAIL
154                         next_step = STEP_CONFIRM
155                         pos_retry = 0
156                     else:
157                         status = STATUS_FAIL
158                         next_step = STEP_DECREASE_UPPER
159                         self.current_upper = test_value
160
161                     neg_retry = neg_retry + 1
162
163                 LOG.info(
164                     "Status = '%s' Next_Step = '%s'", status, next_step)
165
166                 samples = result.get_samples(pkt_size, successful_pkt_loss, port_samples)
167
168                 if theor_max_thruput < samples["TxThroughput"]:
169                     theor_max_thruput = samples['TxThroughput']
170                 samples['theor_max_throughput'] = theor_max_thruput
171
172                 samples["rx_total"] = int(result.rx_total)
173                 samples["tx_total"] = int(result.tx_total)
174                 samples["can_be_lost"] = int(result.can_be_lost)
175                 samples["drop_total"] = int(result.drop_total)
176                 samples["RxThroughput_gbps"] = \
177                     (samples["RxThroughput"] / 1000) * ((pkt_size + 20) * 8)
178                 samples['Status'] = status
179                 samples['Next_Step'] = next_step
180                 samples["MAX_Rate"] = self.current_upper
181                 samples["MIN_Rate"] = self.current_lower
182                 samples["Test_Rate"] = test_value
183                 samples["Step_Id"] = step_id
184                 samples["Confirmation_Retry"] = total_retry
185
186                 samples.update(test_data)
187
188                 if status == STATUS_SUCCESS and next_step == STEP_INCREASE_LOWER:
189                     # Store success samples for result samples
190                     result_samples = samples
191
192                 LOG.info(">>>##>>Collect TG KPIs %s %s", datetime.datetime.now(), samples)
193
194                 self.queue.put(samples, True, overall_constants.QUEUE_PUT_TIMEOUT)
195
196         LOG.info(
197             ">>>##>> Result Reached PktSize %s Theor_Max_Thruput %s Actual_throughput %s",
198             pkt_size, theor_max_thruput, result_samples.get("RxThroughput", 0))
199         result_samples["Status"] = STATUS_RESULT
200         result_samples["Next_Step"] = ""
201         result_samples["Actual_throughput"] = result_samples.get("RxThroughput", 0)
202         result_samples["theor_max_throughput"] = theor_max_thruput
203         self.queue.put(result_samples)