Improve IXIA TG Rx/TX throughput calculation
[yardstick.git] / yardstick / network_services / traffic_profile / base.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
15 import re
16
17 from yardstick.common import exceptions
18 from yardstick.common import utils
19
20
21 class TrafficProfileConfig(object):
22     """Class to contain the TrafficProfile class information
23
24     This object will parse and validate the traffic profile information.
25     """
26     DEFAULT_SCHEMA = 'nsb:traffic_profile:0.1'
27     DEFAULT_FRAME_RATE = '100'
28     DEFAULT_DURATION = 30
29     RATE_FPS = 'fps'
30     RATE_PERCENTAGE = '%'
31     RATE_REGEX = re.compile(r'([0-9]*\.[0-9]+|[0-9]+)\s*(fps|%)*(.*)')
32
33     def __init__(self, tp_config):
34         self.schema = tp_config.get('schema', self.DEFAULT_SCHEMA)
35         self.name = tp_config.get('name')
36         self.description = tp_config.get('description')
37         tprofile = tp_config['traffic_profile']
38         self.traffic_type = tprofile.get('traffic_type')
39         self.frame_rate, self.rate_unit = self._parse_rate(
40             tprofile.get('frame_rate', self.DEFAULT_FRAME_RATE))
41         self.test_precision = tprofile.get('test_precision')
42         self.packet_sizes = tprofile.get('packet_sizes')
43         self.duration = tprofile.get('duration', self.DEFAULT_DURATION)
44         self.lower_bound = tprofile.get('lower_bound')
45         self.upper_bound = tprofile.get('upper_bound')
46         self.step_interval = tprofile.get('step_interval')
47
48     def _parse_rate(self, rate):
49         """Parse traffic profile rate
50
51         The line rate can be defined in fps or percentage over the maximum line
52         rate:
53           - frame_rate = 5000 (by default, unit is 'fps')
54           - frame_rate = 5000fps
55           - frame_rate = 25%
56
57         :param rate: (string, int) line rate in fps or %
58         :return: (tuple: int, string) line rate number and unit
59         """
60         match = self.RATE_REGEX.match(str(rate))
61         if not match:
62             exceptions.TrafficProfileRate()
63         rate = float(match.group(1))
64         unit = match.group(2) if match.group(2) else self.RATE_FPS
65         if match.group(3):
66             raise exceptions.TrafficProfileRate()
67         return rate, unit
68
69
70 class TrafficProfile(object):
71     """
72     This class defines the behavior
73
74     """
75     UPLINK = "uplink"
76     DOWNLINK = "downlink"
77
78     @staticmethod
79     def get(tp_config):
80         """Get the traffic profile instance for the given traffic type
81
82         :param tp_config: loaded YAML file
83         :return:
84         """
85         profile_class = tp_config["traffic_profile"]["traffic_type"]
86         try:
87             return next(c for c in utils.itersubclasses(TrafficProfile)
88                         if c.__name__ == profile_class)(tp_config)
89         except StopIteration:
90             raise exceptions.TrafficProfileNotImplemented(
91                 profile_class=profile_class)
92
93     def __init__(self, tp_config):
94         # e.g. RFC2544 start_ip, stop_ip, drop_rate,
95         # IMIX = {"10K": 0.1, "100M": 0.5}
96         self.params = tp_config
97         self.config = TrafficProfileConfig(tp_config)
98
99     def execute_traffic(self, traffic_generator, **kawrgs):
100         """ This methods defines the behavior of the traffic generator.
101         It will be called in a loop until the traffic generator exits.
102
103         :param traffic_generator: TrafficGen instance
104         :return: None
105         """
106         raise NotImplementedError()
107
108
109 class DummyProfile(TrafficProfile):
110     """
111     This is an empty TrafficProfile implementation - if it is used,
112     the traffic will be completely handled by the Traffic Generator
113     implementation with no regard for the Traffic Profile.
114     """
115     def execute(self, traffic_generator):
116         pass