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 conf import settings
29 from conf import merge_spec
30 from tools.pkt_gen import trafficgen
31 from core.results.results_constants import ResultsConstants
33 def _get_user_traffic_stat(stat_type):
35 Request user input for traffic.
37 :param stat_type: Name of statistic required from user
39 :returns: Value of stat provided by user
41 true_vals = ('yes', 'y', 'ye', None)
42 false_vals = ('no', 'n')
45 result = input('What was the result for \'%s\'? ' % stat_type)
50 print('That was not a valid integer result. Try again.')
54 choice = input('Is \'%d\' correct? ' % result).lower()
55 if not choice or choice in true_vals:
57 elif choice and choice in false_vals:
60 print('Please respond with \'yes\' or \'no\' ', end='')
62 def get_user_traffic(traffic_type, traffic_conf, flow_conf, traffic_stats):
64 Request user input for traffic.
66 :param traffic_type: Name of traffic type.
67 :param traffic_conf: Configuration of traffic to be sent.
68 :param traffic_conf: Configuration of flow to be sent.
69 :param traffic_stats: Required output statistics (i.e. what's needed)
71 :returns: List of stats corresponding to those in traffic_stats
75 print('Please send \'%s\' traffic with the following stream config:\n%s\n'
76 'and the following flow config:\n%s'
77 % (traffic_type, traffic_conf, json.dumps(flow_conf, indent=4)))
79 for stat in traffic_stats:
80 if stat in settings.getValue('TRAFFICGEN_DUMMY_RESULTS'):
81 results.append(settings.getValue('TRAFFICGEN_DUMMY_RESULTS')[stat])
83 results.append(_get_user_traffic_stat(stat))
88 class Dummy(trafficgen.ITrafficGenerator):
90 A dummy traffic generator whose data is generated by the user.
92 This traffic generator is useful when a user does not wish to write
93 a wrapper for a given type of traffic generator. By using this
94 "traffic generator", the user is asked to send traffic when
95 required and enter the results manually. The user controls the
96 real traffic generator and is responsible for ensuring the flows
105 def disconnect(self):
111 def send_burst_traffic(self, traffic=None, numpkts=100, duration=20):
113 Send a burst of traffic.
115 traffic_ = self.traffic_defaults.copy()
119 traffic_ = merge_spec(traffic_, traffic)
121 results = get_user_traffic(
123 '%dpkts, %dmS' % (numpkts, duration),
125 ('frames rx', 'payload errors', 'sequence errors'))
127 # builds results by using user-supplied values where possible
128 # and guessing remainder using available info
129 result[ResultsConstants.TX_FRAMES] = numpkts
130 result[ResultsConstants.RX_FRAMES] = results[0]
131 result[ResultsConstants.TX_BYTES] = traffic_['l2']['framesize'] \
133 result[ResultsConstants.RX_BYTES] = traffic_['l2']['framesize'] \
135 result[ResultsConstants.PAYLOAD_ERR] = results[1]
136 result[ResultsConstants.SEQ_ERR] = results[2]
140 def send_cont_traffic(self, traffic=None, duration=30):
142 Send a continuous flow of traffic.
144 traffic_ = self.traffic_defaults.copy()
148 traffic_ = merge_spec(traffic_, traffic)
150 results = get_user_traffic(
152 '%dmpps, multistream %s, duration %d' % (traffic['frame_rate'],
153 traffic['multistream'],
155 ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
156 'max latency', 'avg latency', 'frameloss %'))
158 framesize = traffic_['l2']['framesize']
160 # builds results by using user-supplied values
161 # and guessing remainder using available info
162 result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
163 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
164 result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
165 * (framesize * 8 / 1000000)
166 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
167 * (framesize * 8 / 1000000)
168 result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
169 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
170 result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
171 result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
172 result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
173 result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
176 def send_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
179 Send traffic per RFC2544 throughput test specifications.
181 traffic_ = self.traffic_defaults.copy()
185 traffic_ = merge_spec(traffic_, traffic)
187 results = get_user_traffic(
189 '%d tests, %d seconds iterations, %f packet loss, multistream '
190 '%s' % (tests, duration, lossrate, traffic['multistream']),
192 ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
193 'max latency', 'avg latency', 'frameloss %'))
195 framesize = traffic_['l2']['framesize']
197 # builds results by using user-supplied values
198 # and guessing remainder using available info
199 result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
200 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
201 result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
202 * (framesize * 8 / 1000000)
203 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
204 * (framesize * 8 / 1000000)
205 result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
206 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
207 result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
208 result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
209 result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
210 result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
213 def send_rfc2544_back2back(self, traffic=None, tests=1, duration=2,
216 Send traffic per RFC2544 back2back test specifications.
218 traffic_ = self.traffic_defaults.copy()
222 traffic_ = merge_spec(traffic_, traffic)
224 results = get_user_traffic(
226 '%d tests, %d seconds iterations, %f packet loss, multistream '
227 '%s' % (tests, duration, lossrate, traffic['multistream']),
229 ('b2b frames', 'b2b frame loss %'))
231 # builds results by using user-supplied values
232 # and guessing remainder using available info
233 result[ResultsConstants.B2B_FRAMES] = float(results[0])
234 result[ResultsConstants.B2B_FRAME_LOSS_PERCENT] = float(results[1])
237 def start_cont_traffic(self, traffic=None, duration=20):
238 return NotImplementedError('Dummy does not implement start_cont_traffic')
240 def stop_cont_traffic(self):
241 return NotImplementedError(
242 'Dummy does not implement stop_cont_traffic')
244 def start_rfc2544_back2back(self, traffic=None, tests=1, duration=20,
246 return NotImplementedError(
247 'Dummy does not implement start_rfc2544_back2back')
249 def wait_rfc2544_back2back(self):
250 return NotImplementedError(
251 'Dummy does not implement stop_cont_traffic')
253 def start_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
255 return NotImplementedError(
256 'Dummy does not implement start_rfc2544_throughput')
258 def wait_rfc2544_throughput(self):
259 return NotImplementedError(
260 'Dummy does not implement wait_rfc2544_throughput')
262 if __name__ == '__main__':
267 'dstip': '90.90.90.90',
272 print(dev.send_burst_traffic(traffic=TRAFFIC))
273 print(dev.send_cont_traffic(traffic=TRAFFIC))
274 print(dev.send_rfc2544_throughput(traffic=TRAFFIC))
275 print(dev.send_rfc2544_back2back(traffic=TRAFFIC))
276 print(dev.send_rfc(traffic=TRAFFIC))