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