Merge "pylint fixes: remove redundant parens, fix comparison order"
[yardstick.git] / yardstick / network_services / traffic_profile / rfc2544.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 """ RFC2544 Throughput implemenation """
15
16 from __future__ import absolute_import
17 from __future__ import division
18 import logging
19
20 from yardstick.network_services.traffic_profile.traffic_profile \
21     import TrexProfile
22
23 LOGGING = logging.getLogger(__name__)
24
25
26 class RFC2544Profile(TrexProfile):
27     """ This class handles rfc2544 implemenation. """
28
29     def __init__(self, traffic_generator):
30         super(RFC2544Profile, self).__init__(traffic_generator)
31         self.max_rate = None
32         self.min_rate = None
33         self.rate = 100
34         self.tmp_drop = None
35         self.tmp_throughput = None
36         self.profile_data = None
37
38     def execute(self, traffic_generator):
39         ''' Generate the stream and run traffic on the given ports '''
40         if self.first_run:
41             self.profile_data = self.params.get('private', '')
42             ports = [traffic_generator.my_ports[0]]
43             traffic_generator.client.add_streams(self.get_streams(),
44                                                  ports=ports[0])
45             profile_data = self.params.get('public', '')
46             if profile_data:
47                 self.profile_data = profile_data
48                 ports.append(traffic_generator.my_ports[1])
49                 traffic_generator.client.add_streams(self.get_streams(),
50                                                      ports=ports[1])
51
52             self.max_rate = self.rate
53             self.min_rate = 0
54             traffic_generator.client.start(ports=ports,
55                                            mult=self.get_multiplier(),
56                                            duration=30, force=True)
57             self.tmp_drop = 0
58             self.tmp_throughput = 0
59
60     def get_multiplier(self):
61         ''' Get the rate at which next iternation to run '''
62         self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
63         multiplier = round(self.rate / self.pps, 2)
64         return str(multiplier)
65
66     def get_drop_percentage(self, traffic_generator,
67                             samples, tol_min, tolerance):
68         ''' Calculate the drop percentage and run the traffic '''
69         in_packets = sum([samples[iface]['in_packets'] for iface in samples])
70         out_packets = sum([samples[iface]['out_packets'] for iface in samples])
71         packet_drop = abs(out_packets - in_packets)
72         drop_percent = 100.0
73         try:
74             drop_percent = round((packet_drop / float(out_packets)) * 100, 2)
75         except ZeroDivisionError:
76             LOGGING.info('No traffic is flowing')
77         samples['TxThroughput'] = out_packets / 30
78         samples['RxThroughput'] = in_packets / 30
79         samples['CurrentDropPercentage'] = drop_percent
80         samples['Throughput'] = self.tmp_throughput
81         samples['DropPercentage'] = self.tmp_drop
82         if drop_percent > tolerance and self.tmp_throughput == 0:
83             samples['Throughput'] = (in_packets / 30)
84             samples['DropPercentage'] = drop_percent
85         if self.first_run:
86             max_supported_rate = out_packets / 30
87             self.rate = max_supported_rate
88             self.first_run = False
89         if drop_percent > tolerance:
90             self.max_rate = self.rate
91         elif drop_percent < tol_min:
92             self.min_rate = self.rate
93             if drop_percent >= self.tmp_drop:
94                 self.tmp_drop = drop_percent
95                 self.tmp_throughput = (in_packets / 30)
96                 samples['Throughput'] = (in_packets / 30)
97                 samples['DropPercentage'] = drop_percent
98         else:
99             samples['Throughput'] = (in_packets / 30)
100             samples['DropPercentage'] = drop_percent
101
102         traffic_generator.client.clear_stats(ports=traffic_generator.my_ports)
103         traffic_generator.client.start(ports=traffic_generator.my_ports,
104                                        mult=self.get_multiplier(),
105                                        duration=30, force=True)
106         return samples