1 # Copyright 2015-2016 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 tools.pkt_gen import trafficgen
29 from core.results.results_constants import ResultsConstants
31 def _get_user_traffic_stat(stat_type):
33 Request user input for traffic.
35 :param stat_type: Name of statistic required from user
37 :returns: Value of stat provided by user
39 true_vals = ('yes', 'y', 'ye', None)
40 false_vals = ('no', 'n')
43 result = input('What was the result for \'%s\'? ' % stat_type)
48 print('That was not a valid integer result. Try again.')
52 choice = input('Is \'%d\' correct? ' % result).lower()
53 if not choice or choice in true_vals:
55 elif choice and choice in false_vals:
58 print('Please respond with \'yes\' or \'no\' ', end='')
61 def get_user_traffic(traffic_type, traffic_conf, flow_conf, traffic_stats):
63 Request user input for traffic.
65 :param traffic_type: Name of traffic type.
66 :param traffic_conf: Configuration of traffic to be sent.
67 :param traffic_conf: Configuration of flow to be sent.
68 :param traffic_stats: Required output statistics (i.e. what's needed)
70 :returns: List of stats corresponding to those in traffic_stats
74 print('Please send \'%s\' traffic with the following stream config:\n%s\n'
75 'and the following flow config:\n%s'
76 % (traffic_type, traffic_conf, json.dumps(flow_conf, indent=4)))
78 for stat in traffic_stats:
79 results.append(_get_user_traffic_stat(stat))
84 class Dummy(trafficgen.ITrafficGenerator):
86 A dummy traffic generator whose data is generated by the user.
88 This traffic generator is useful when a user does not wish to write
89 a wrapper for a given type of traffic generator. By using this
90 "traffic generator", the user is asked to send traffic when
91 required and enter the results manually. The user controls the
92 real traffic generator and is responsible for ensuring the flows
101 def disconnect(self):
107 def send_burst_traffic(self, traffic=None, numpkts=100, duration=20):
109 Send a burst of traffic.
111 traffic_ = self.traffic_defaults.copy()
115 traffic_ = trafficgen.merge_spec(traffic_, traffic)
117 results = get_user_traffic(
119 '%dpkts, %dmS' % (numpkts, duration),
121 ('frames rx', 'payload errors', 'sequence errors'))
123 # builds results by using user-supplied values where possible
124 # and guessing remainder using available info
125 result[ResultsConstants.TX_FRAMES] = numpkts
126 result[ResultsConstants.RX_FRAMES] = results[0]
127 result[ResultsConstants.TX_BYTES] = traffic_['l2']['framesize'] \
129 result[ResultsConstants.RX_BYTES] = traffic_['l2']['framesize'] \
131 result[ResultsConstants.PAYLOAD_ERR] = results[1]
132 result[ResultsConstants.SEQ_ERR] = results[2]
134 return trafficgen.BurstResult(*results)
136 def send_cont_traffic(self, traffic=None, duration=30):
138 Send a continuous flow of traffic.
140 traffic_ = self.traffic_defaults.copy()
144 traffic_ = trafficgen.merge_spec(traffic_, traffic)
146 results = get_user_traffic(
148 '%dmpps, multistream %s, duration %d' % (traffic['frame_rate'],
149 traffic['multistream'],
151 ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
152 'max latency', 'avg latency', 'frameloss %'))
154 framesize = traffic_['l2']['framesize']
156 # builds results by using user-supplied values
157 # and guessing remainder using available info
158 result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
159 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
160 result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
161 * (framesize * 8 / 1000000)
162 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
163 * (framesize * 8 / 1000000)
164 result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
165 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
166 result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
167 result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
168 result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
169 result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
172 def send_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
175 Send traffic per RFC2544 throughput test specifications.
177 traffic_ = self.traffic_defaults.copy()
181 traffic_ = trafficgen.merge_spec(traffic_, traffic)
183 results = get_user_traffic(
185 '%d tests, %d seconds iterations, %f packet loss, multistream '
186 '%s' % (tests, duration, lossrate, traffic['multistream']),
188 ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
189 'max latency', 'avg latency', 'frameloss %'))
191 framesize = traffic_['l2']['framesize']
193 # builds results by using user-supplied values
194 # and guessing remainder using available info
195 result[ResultsConstants.TX_RATE_FPS] = float(results[0]) / duration
196 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / duration
197 result[ResultsConstants.TX_RATE_MBPS] = (float(results[0]) / duration) \
198 * (framesize * 8 / 1000000)
199 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) / duration) \
200 * (framesize * 8 / 1000000)
201 result[ResultsConstants.TX_RATE_PERCENT] = float(results[2])
202 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = float(results[3])
203 result[ResultsConstants.MIN_LATENCY_NS] = float(results[4])
204 result[ResultsConstants.MAX_LATENCY_NS] = float(results[5])
205 result[ResultsConstants.AVG_LATENCY_NS] = float(results[6])
206 result[ResultsConstants.FRAME_LOSS_PERCENT] = float(results[7])
210 if __name__ == '__main__':
215 'dstip': '90.90.90.90',
220 print(dev.send_burst_traffic(traffic=TRAFFIC))
221 print(dev.send_cont_traffic(traffic=TRAFFIC))
222 print(dev.send_rfc(traffic=TRAFFIC))