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