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.
14 """ RFC2544 Throughput implemenation """
16 from __future__ import absolute_import
17 from __future__ import division
20 from trex_stl_lib.trex_stl_client import STLStream
21 from trex_stl_lib.trex_stl_streams import STLFlowLatencyStats
22 from trex_stl_lib.trex_stl_streams import STLTXCont
24 from yardstick.network_services.traffic_profile.traffic_profile \
27 LOGGING = logging.getLogger(__name__)
30 class RFC2544Profile(TrexProfile):
31 """ This class handles rfc2544 implemenation. """
33 def __init__(self, traffic_generator):
34 super(RFC2544Profile, self).__init__(traffic_generator)
40 self.drop_percent_at_max_tx = None
41 self.throughput_max = None
43 def register_generator(self, generator):
44 self.generator = generator
46 def execute_traffic(self, traffic_generator=None):
47 """ Generate the stream and run traffic on the given ports """
48 if traffic_generator is not None and self.generator is None:
49 self.generator = traffic_generator
51 if self.ports is not None:
55 for vld_id, intfs in sorted(self.generator.networks.items()):
56 profile_data = self.params.get(vld_id)
57 # no profile for this port
60 # correlated traffic doesn't use public traffic?
61 if vld_id.startswith("public") and self.generator.rfc2544_helper.correlated_traffic:
64 port = self.generator.vnfd_helper.port_num(intf)
65 self.ports.append(port)
66 self.generator.client.add_streams(self.get_streams(profile_data), ports=port)
68 self.max_rate = self.rate
70 self.generator.client.start(ports=self.ports, mult=self.get_multiplier(),
71 duration=30, force=True)
72 self.drop_percent_at_max_tx = 0
73 self.throughput_max = 0
75 def get_multiplier(self):
76 """ Get the rate at which next iteration to run """
77 self.rate = round((self.max_rate + self.min_rate) / 2.0, 2)
78 multiplier = round(self.rate / self.pps, 2)
79 return str(multiplier)
81 def get_drop_percentage(self, generator=None):
82 """ Calculate the drop percentage and run the traffic """
84 generator = self.generator
85 run_duration = self.generator.RUN_DURATION
86 samples = self.generator.generate_samples(self.ports)
88 in_packets = sum([value['in_packets'] for value in samples.values()])
89 out_packets = sum([value['out_packets'] for value in samples.values()])
91 packet_drop = abs(out_packets - in_packets)
94 drop_percent = round((packet_drop / float(out_packets)) * 100, 5)
95 except ZeroDivisionError:
96 LOGGING.info('No traffic is flowing')
98 # TODO(esm): RFC2544 doesn't tolerate packet loss, why do we?
99 tolerance_low = generator.rfc2544_helper.tolerance_low
100 tolerance_high = generator.rfc2544_helper.tolerance_high
102 tx_rate = out_packets / run_duration
103 rx_rate = in_packets / run_duration
105 throughput_max = self.throughput_max
106 drop_percent_at_max_tx = self.drop_percent_at_max_tx
108 if self.drop_percent_at_max_tx is None:
110 self.first_run = False
112 if drop_percent > tolerance_high:
113 # TODO(esm): why don't we discard results that are out of tolerance?
114 self.max_rate = self.rate
115 if throughput_max == 0:
116 throughput_max = rx_rate
117 drop_percent_at_max_tx = drop_percent
119 elif drop_percent >= tolerance_low:
120 # TODO(esm): why do we update the samples dict in this case
121 # and not update our tracking values?
122 throughput_max = rx_rate
123 drop_percent_at_max_tx = drop_percent
125 elif drop_percent >= self.drop_percent_at_max_tx:
126 # TODO(esm): why don't we discard results that are out of tolerance?
127 self.min_rate = self.rate
128 self.drop_percent_at_max_tx = drop_percent_at_max_tx = drop_percent
129 self.throughput_max = throughput_max = rx_rate
132 # TODO(esm): why don't we discard results that are out of tolerance?
133 self.min_rate = self.rate
135 generator.clear_client_stats(self.ports)
136 generator.start_client(self.ports, mult=self.get_multiplier(),
137 duration=run_duration, force=True)
139 # if correlated traffic update the Throughput
140 if generator.rfc2544_helper.correlated_traffic:
144 'TxThroughput': tx_rate,
145 'RxThroughput': rx_rate,
146 'CurrentDropPercentage': drop_percent,
147 'Throughput': throughput_max,
148 'DropPercentage': drop_percent_at_max_tx,
153 def execute_latency(self, generator=None, samples=None):
154 if generator is not None and self.generator is None:
155 self.generator = generator
158 samples = self.generator.generate_samples()
160 self.pps, multiplier = self.calculate_pps(samples)
162 self.pg_id = self.params['traffic_profile'].get('pg_id', 1)
163 for vld_id, intfs in sorted(self.generator.networks.items()):
164 profile_data = self.params.get(vld_id)
167 # correlated traffic doesn't use public traffic?
168 if vld_id.startswith("public") and self.generator.rfc2544_helper.correlated_traffic:
171 port = self.generator.vnfd_helper.port_num(intf)
172 self.ports.append(port)
173 self.generator.client.add_streams(self.get_streams(profile_data), ports=port)
175 self.generator.start_client(ports=self.ports, mult=str(multiplier),
176 duration=120, force=True)
177 self.first_run = False
179 def calculate_pps(self, samples):
180 pps = round(samples['Throughput'] / 2, 2)
181 multiplier = round(self.rate / self.pps, 2)
182 return pps, multiplier
184 def create_single_stream(self, packet_size, pps, isg=0):
185 packet = self._create_single_packet(packet_size)
187 stl_mode = STLTXCont(pps=pps)
189 stl_mode = STLTXCont(pps=self.pps)
191 LOGGING.debug("pg_id: %s", self.pg_id)
192 stl_flow_stats = STLFlowLatencyStats(pg_id=self.pg_id)
193 stream = STLStream(isg=isg, packet=packet, mode=stl_mode,
194 flow_stats=stl_flow_stats)
197 stream = STLStream(isg=isg, packet=packet, mode=stl_mode)