1 # Copyright 2015 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, time=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, time),
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, time=20, multistream=False):
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 '%dmS, %dmpps, multistream %s' % (time, traffic['frame_rate'],
149 multistream), traffic_,
150 ('frames tx', 'frames rx', 'min latency', 'max latency',
153 framesize = traffic_['l2']['framesize']
155 # builds results by using user-supplied values where possible
156 # and guessing remainder using available info
157 result[ResultsConstants.THROUGHPUT_TX_FPS] = float(results[0]) / time
158 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) / time
159 result[ResultsConstants.THROUGHPUT_TX_MBPS] = (float(results[0]) \
161 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) \
163 result[ResultsConstants.THROUGHPUT_TX_PERCENT] = 0.0
164 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = 0.0
165 result[ResultsConstants.MIN_LATENCY_NS] = float(results[2])
166 result[ResultsConstants.MAX_LATENCY_NS] = float(results[3])
167 result[ResultsConstants.AVG_LATENCY_NS] = float(results[4])
171 def send_rfc2544_throughput(self, traffic=None, trials=3, duration=20,
172 lossrate=0.0, multistream=False):
174 Send traffic per RFC2544 throughput test specifications.
176 traffic_ = self.traffic_defaults.copy()
180 traffic_ = trafficgen.merge_spec(traffic_, traffic)
182 results = get_user_traffic(
184 '%d trials, %d seconds iterations, %f packet loss, multistream '
185 '%s' % (trials, duration, lossrate,
186 'enabled' if multistream else 'disabled'),
188 ('frames tx', 'frames rx', 'min latency', 'max latency',
191 framesize = traffic_['l2']['framesize']
193 # builds results by using user-supplied values where possible
194 # and guessing remainder using available info
195 result[ResultsConstants.THROUGHPUT_TX_FPS] = float(results[0]) \
197 result[ResultsConstants.THROUGHPUT_RX_FPS] = float(results[1]) \
199 result[ResultsConstants.THROUGHPUT_TX_MBPS] = (float(results[0]) \
200 * framesize) / duration
201 result[ResultsConstants.THROUGHPUT_RX_MBPS] = (float(results[1]) \
202 * framesize) / duration
203 result[ResultsConstants.THROUGHPUT_TX_PERCENT] = 0.0
204 result[ResultsConstants.THROUGHPUT_RX_PERCENT] = 0.0
205 result[ResultsConstants.MIN_LATENCY_NS] = float(results[2])
206 result[ResultsConstants.MAX_LATENCY_NS] = float(results[3])
207 result[ResultsConstants.AVG_LATENCY_NS] = float(results[4])
211 if __name__ == '__main__':
216 'dstip': '90.90.90.90',
221 print(dev.send_burst_traffic(traffic=TRAFFIC))
222 print(dev.send_cont_traffic(traffic=TRAFFIC))
223 print(dev.send_rfc(traffic=TRAFFIC))