1 # Copyright 2015-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.
16 Dummy traffic generator, designed for user intput.
18 Provides a model for the Dummy traffic generator - a psuedo "traffic
19 generator" that doesn't actually generate any traffic. Instead the
20 user is required to send traffic using their own choice of traffic
21 generator *outside of the framework*. The Dummy traffic generator
22 then returns the results - manually entered by the user - as its
28 from collections import OrderedDict
29 from conf import settings
30 from conf import merge_spec
31 from tools.pkt_gen import trafficgen
32 from core.results.results_constants import ResultsConstants
34 def _get_user_traffic_stat(stat_type):
36 Request user input for traffic.
38 :param stat_type: Name of statistic required from user
40 :returns: Value of stat provided by user
42 true_vals = ('yes', 'y', 'ye', None)
43 false_vals = ('no', 'n')
46 result = input('What was the result for \'%s\'? ' % stat_type)
51 print('That was not a valid integer result. Try again.')
55 choice = input('Is \'%d\' correct? ' % result).lower()
56 if not choice or choice in true_vals:
58 elif choice and choice in false_vals:
61 print('Please respond with \'yes\' or \'no\' ', end='')
63 def get_user_traffic(traffic_type, traffic_conf, flow_conf, traffic_stats):
65 Request user input for traffic.
67 :param traffic_type: Name of traffic type.
68 :param traffic_conf: Configuration of traffic to be sent.
69 :param traffic_conf: Configuration of flow to be sent.
70 :param traffic_stats: Required output statistics (i.e. what's needed)
72 :returns: List of stats corresponding to those in traffic_stats
76 print('Please send \'%s\' traffic with the following stream config:\n%s\n'
77 'and the following flow config:\n%s'
78 % (traffic_type, traffic_conf, json.dumps(flow_conf, indent=4)))
80 for stat in traffic_stats:
81 if stat in settings.getValue('TRAFFICGEN_DUMMY_RESULTS'):
82 results.append(settings.getValue('TRAFFICGEN_DUMMY_RESULTS')[stat])
84 results.append(_get_user_traffic_stat(stat))
89 class Dummy(trafficgen.ITrafficGenerator):
91 A dummy traffic generator whose data is generated by the user.
93 This traffic generator is useful when a user does not wish to write
94 a wrapper for a given type of traffic generator. By using this
95 "traffic generator", the user is asked to send traffic when
96 required and enter the results manually. The user controls the
97 real traffic generator and is responsible for ensuring the flows
106 def disconnect(self):
112 def send_burst_traffic(self, traffic=None, duration=20):
114 Send a burst of traffic.
116 traffic_ = self.traffic_defaults.copy()
117 result = OrderedDict()
120 traffic_ = merge_spec(traffic_, traffic)
122 results = get_user_traffic(
124 '%dpkts, %dmS' % (traffic['burst_size'], duration),
126 ('frames rx', 'payload errors', 'sequence errors'))
128 # builds results by using user-supplied values where possible
129 # and guessing remainder using available info
130 result[ResultsConstants.TX_FRAMES] = traffic['burst_size']
131 result[ResultsConstants.RX_FRAMES] = results[0]
132 result[ResultsConstants.TX_BYTES] = traffic_['l2']['framesize'] \
133 * traffic['burst_size']
134 result[ResultsConstants.RX_BYTES] = traffic_['l2']['framesize'] \
136 result[ResultsConstants.PAYLOAD_ERR] = results[1]
137 result[ResultsConstants.SEQ_ERR] = results[2]
141 def send_cont_traffic(self, traffic=None, duration=30):
143 Send a continuous flow of traffic.
145 traffic_ = self.traffic_defaults.copy()
146 result = OrderedDict()
149 traffic_ = merge_spec(traffic_, traffic)
151 results = get_user_traffic(
153 '%dmpps, multistream %s, duration %d' % (traffic['frame_rate'],
154 traffic['multistream'],
156 ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
157 'max latency', 'avg latency', 'frameloss %'))
159 framesize = traffic_['l2']['framesize']
161 # builds results by using user-supplied values
162 # and guessing remainder using available info
163 result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
164 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
165 result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
166 * (framesize * 8 / 1000000)
167 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
168 * (framesize * 8 / 1000000)
169 result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
170 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
171 result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
172 result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
173 result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
174 result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
177 def send_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
180 Send traffic per RFC2544 throughput test specifications.
182 traffic_ = self.traffic_defaults.copy()
183 result = OrderedDict()
186 traffic_ = merge_spec(traffic_, traffic)
188 results = get_user_traffic(
190 '%d tests, %d seconds iterations, %f packet loss, multistream '
191 '%s' % (tests, duration, lossrate, traffic['multistream']),
193 ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
194 'max latency', 'avg latency', 'frameloss %'))
196 framesize = traffic_['l2']['framesize']
198 # builds results by using user-supplied values
199 # and guessing remainder using available info
200 result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
201 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
202 result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
203 * (framesize * 8 / 1000000)
204 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
205 * (framesize * 8 / 1000000)
206 result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
207 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
208 result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
209 result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
210 result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
211 result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
214 def send_rfc2544_back2back(self, traffic=None, tests=1, duration=2,
217 Send traffic per RFC2544 back2back test specifications.
219 traffic_ = self.traffic_defaults.copy()
220 result = OrderedDict()
223 traffic_ = merge_spec(traffic_, traffic)
225 results = get_user_traffic(
227 '%d tests, %d seconds iterations, %f packet loss, multistream '
228 '%s' % (tests, duration, lossrate, traffic['multistream']),
230 ('b2b frames', 'b2b frame loss %'))
232 # builds results by using user-supplied values
233 # and guessing remainder using available info
234 result[ResultsConstants.B2B_FRAMES] = float(results[0])
235 result[ResultsConstants.B2B_FRAME_LOSS_PERCENT] = float(results[1])
238 def start_cont_traffic(self, traffic=None, duration=20):
239 return NotImplementedError('Dummy does not implement start_cont_traffic')
241 def stop_cont_traffic(self):
242 return NotImplementedError(
243 'Dummy does not implement stop_cont_traffic')
245 def start_rfc2544_back2back(self, traffic=None, tests=1, duration=20,
247 return NotImplementedError(
248 'Dummy does not implement start_rfc2544_back2back')
250 def wait_rfc2544_back2back(self):
251 return NotImplementedError(
252 'Dummy does not implement stop_cont_traffic')
254 def start_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
256 return NotImplementedError(
257 'Dummy does not implement start_rfc2544_throughput')
259 def wait_rfc2544_throughput(self):
260 return NotImplementedError(
261 'Dummy does not implement wait_rfc2544_throughput')
263 if __name__ == '__main__':
268 'dstip': '90.90.90.90',
273 print(dev.send_burst_traffic(traffic=TRAFFIC))
274 print(dev.send_cont_traffic(traffic=TRAFFIC))
275 print(dev.send_rfc2544_throughput(traffic=TRAFFIC))
276 print(dev.send_rfc2544_back2back(traffic=TRAFFIC))
277 # pylint: disable=no-member
278 print(dev.send_rfc(traffic=TRAFFIC))