aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/network_services/traffic_profile/rfc2544.py
blob: c6facc9f5bbfaebfcbd2b3165941a76d76b3fb9e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# Copyright (c) 2016-2017 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
""" RFC2544 Throughput implemenation """

from __future__ import absolute_import
import logging

from yardstick.network_services.traffic_profile.traffic_profile \
    import TrexProfile

LOGGING = logging.getLogger(__name__)


class RFC2544Profile(TrexProfile):
    """ This class handles rfc2544 implemenation. """

    def __init__(self, traffic_generator):
        super(RFC2544Profile, self).__init__(traffic_generator)
        self.max_rate = None
        self.min_rate = None
        self.rate = 100
        self.tmp_drop = None
        self.tmp_throughput = None
        self.profile_data = None

    def execute(self, traffic_generator):
        ''' Generate the stream and run traffic on the given ports '''
        if self.first_run:
            self.profile_data = self.params.get('private', '')
            ports = [traffic_generator.my_ports[0]]
            traffic_generator.client.add_streams(self.get_streams(),
                                                 ports=ports[0])
            profile_data = self.params.get('public', '')
            if profile_data:
                self.profile_data = profile_data
                ports.append(traffic_generator.my_ports[1])
                traffic_generator.client.add_streams(self.get_streams(),
                                                     ports=ports[1])

            self.max_rate = self.rate
            self.min_rate = 0
            traffic_generator.client.start(ports=ports,
                                           mult=self.get_multiplier(),
                                           duration=30, force=True)
            self.tmp_drop = 0
            self.tmp_throughput = 0

    def get_multiplier(self):
        ''' Get the rate at which next iternation to run '''
        self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
        multiplier = round(self.rate / self.pps, 2)
        return str(multiplier)

    def get_drop_percentage(self, traffic_generator,
                            samples, tol_min, tolerance):
        ''' Calculate the drop percentage and run the traffic '''
        in_packets = sum([samples[iface]['in_packets'] for iface in samples])
        out_packets = sum([samples[iface]['out_packets'] for iface in samples])
        packet_drop = abs(out_packets - in_packets)
        drop_percent = 100.0
        try:
            drop_percent = round((packet_drop / float(out_packets)) * 100, 2)
        except ZeroDivisionError:
            LOGGING.info('No traffic is flowing')
        samples['TxThroughput'] = out_packets / 30
        samples['RxThroughput'] = in_packets / 30
        samples['CurrentDropPercentage'] = drop_percent
        samples['Throughput'] = self.tmp_throughput
        samples['DropPercentage'] = self.tmp_drop
        if drop_percent > tolerance and self.tmp_throughput == 0:
            samples['Throughput'] = (in_packets / 30)
            samples['DropPercentage'] = drop_percent
        if self.first_run:
            max_supported_rate = out_packets / 30
            self.rate = max_supported_rate
            self.first_run = False
        if drop_percent > tolerance:
            self.max_rate = self.rate
        elif drop_percent < tol_min:
            self.min_rate = self.rate
            if drop_percent >= self.tmp_drop:
                self.tmp_drop = drop_percent
                self.tmp_throughput = (in_packets / 30)
                samples['Throughput'] = (in_packets / 30)
                samples['DropPercentage'] = drop_percent
        else:
            samples['Throughput'] = (in_packets / 30)
            samples['DropPercentage'] = drop_percent

        traffic_generator.client.clear_stats(ports=traffic_generator.my_ports)
        traffic_generator.client.start(ports=traffic_generator.my_ports,
                                       mult=self.get_multiplier(),
                                       duration=30, force=True)
        return samples