1 # Copyright (c) 2016-2017 Intel Corporation
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 from yardstick.common import exceptions
18 from yardstick.common import utils
21 class TrafficProfileConfig(object):
22 """Class to contain the TrafficProfile class information
24 This object will parse and validate the traffic profile information.
26 DEFAULT_SCHEMA = 'nsb:traffic_profile:0.1'
27 DEFAULT_FRAME_RATE = '100'
31 RATE_REGEX = re.compile(r'([0-9]*\.[0-9]+|[0-9]+)\s*(fps|%)*(.*)')
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 self.enable_latency = tprofile.get('enable_latency', False)
49 def _parse_rate(self, rate):
50 """Parse traffic profile rate
52 The line rate can be defined in fps or percentage over the maximum line
54 - frame_rate = 5000 (by default, unit is 'fps')
55 - frame_rate = 5000fps
58 :param rate: (string, int) line rate in fps or %
59 :return: (tuple: int, string) line rate number and unit
61 match = self.RATE_REGEX.match(str(rate))
63 exceptions.TrafficProfileRate()
64 rate = float(match.group(1))
65 unit = match.group(2) if match.group(2) else self.RATE_FPS
67 raise exceptions.TrafficProfileRate()
71 class TrafficProfile(object):
73 This class defines the behavior
81 """Get the traffic profile instance for the given traffic type
83 :param tp_config: loaded YAML file
86 profile_class = tp_config["traffic_profile"]["traffic_type"]
88 return next(c for c in utils.itersubclasses(TrafficProfile)
89 if c.__name__ == profile_class)(tp_config)
91 raise exceptions.TrafficProfileNotImplemented(
92 profile_class=profile_class)
94 def __init__(self, tp_config):
95 # e.g. RFC2544 start_ip, stop_ip, drop_rate,
96 # IMIX = {"10K": 0.1, "100M": 0.5}
97 self.params = tp_config
98 self.config = TrafficProfileConfig(tp_config)
103 def execute_traffic(self, traffic_generator, **kawrgs):
104 """ This methods defines the behavior of the traffic generator.
105 It will be called in a loop until the traffic generator exits.
107 :param traffic_generator: TrafficGen instance
110 raise NotImplementedError()
113 class DummyProfile(TrafficProfile):
115 This is an empty TrafficProfile implementation - if it is used,
116 the traffic will be completely handled by the Traffic Generator
117 implementation with no regard for the Traffic Profile.
119 def execute(self, traffic_generator):