Merge "Tools: Improve Stability."
[vswitchperf.git] / tools / pkt_gen / dummy / dummy.py
1 # Copyright 2015-2017 Intel Corporation.
2 #
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
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
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
15 """
16 Dummy traffic generator, designed for user intput.
17
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
23 own.
24 """
25
26 import json
27
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
33
34 def _get_user_traffic_stat(stat_type):
35     """
36     Request user input for traffic.
37
38     :param stat_type: Name of statistic required from user
39
40     :returns: Value of stat provided by user
41     """
42     true_vals = ('yes', 'y', 'ye', None)
43     false_vals = ('no', 'n')
44
45     while True:
46         result = input('What was the result for \'%s\'? ' % stat_type)
47
48         try:
49             result = int(result)
50         except ValueError:
51             print('That was not a valid integer result. Try again.')
52             continue
53
54         while True:
55             choice = input('Is \'%d\' correct? ' % result).lower()
56             if not choice or choice in true_vals:
57                 return result
58             elif choice and choice in false_vals:
59                 break
60             else:
61                 print('Please respond with \'yes\' or \'no\' ', end='')
62
63 def get_user_traffic(traffic_type, traffic_conf, flow_conf, traffic_stats):
64     """
65     Request user input for traffic.
66
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)
71
72     :returns: List of stats corresponding to those in traffic_stats
73     """
74     results = []
75
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)))
79
80     for stat in traffic_stats:
81         if stat in settings.getValue('TRAFFICGEN_DUMMY_RESULTS'):
82             results.append(settings.getValue('TRAFFICGEN_DUMMY_RESULTS')[stat])
83         else:
84             results.append(_get_user_traffic_stat(stat))
85
86     return results
87
88
89 class Dummy(trafficgen.ITrafficGenerator):
90     """
91     A dummy traffic generator whose data is generated by the user.
92
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
98     are setup correctly.
99     """
100     def connect(self):
101         """
102         Do nothing.
103         """
104         return self
105
106     def disconnect(self):
107         """
108         Do nothing.
109         """
110         pass
111
112     def send_burst_traffic(self, traffic=None, duration=20):
113         """
114         Send a burst of traffic.
115         """
116         traffic_ = self.traffic_defaults.copy()
117         result = OrderedDict()
118
119         if traffic:
120             traffic_ = merge_spec(traffic_, traffic)
121
122         results = get_user_traffic(
123             'burst',
124             '%dpkts, %dmS' % (traffic['burst_size'], duration),
125             traffic_,
126             ('frames rx', 'payload errors', 'sequence errors'))
127
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'] \
135                                             * results[0]
136         result[ResultsConstants.PAYLOAD_ERR] = results[1]
137         result[ResultsConstants.SEQ_ERR] = results[2]
138
139         return result
140
141     def send_cont_traffic(self, traffic=None, duration=30):
142         """
143         Send a continuous flow of traffic.
144         """
145         traffic_ = self.traffic_defaults.copy()
146         result = OrderedDict()
147
148         if traffic:
149             traffic_ = merge_spec(traffic_, traffic)
150
151         results = get_user_traffic(
152             'continuous',
153             '%dmpps, multistream %s, duration %d' % (traffic['frame_rate'],
154                                                      traffic['multistream'],
155                                                      duration), traffic_,
156             ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
157              'max latency', 'avg latency', 'frameloss %'))
158
159         framesize = traffic_['l2']['framesize']
160
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])
175         return result
176
177     def send_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
178                                 lossrate=0.0):
179         """
180         Send traffic per RFC2544 throughput test specifications.
181         """
182         traffic_ = self.traffic_defaults.copy()
183         result = OrderedDict()
184
185         if traffic:
186             traffic_ = merge_spec(traffic_, traffic)
187
188         results = get_user_traffic(
189             'throughput',
190             '%d tests, %d seconds iterations, %f packet loss, multistream '
191             '%s' % (tests, duration, lossrate, traffic['multistream']),
192             traffic_,
193             ('frames tx', 'frames rx', 'tx rate %', 'rx rate %', 'min latency',
194              'max latency', 'avg latency', 'frameloss %'))
195
196         framesize = traffic_['l2']['framesize']
197
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])
212         return result
213
214     def send_rfc2544_back2back(self, traffic=None, tests=1, duration=2,
215                                lossrate=0.0):
216         """
217         Send traffic per RFC2544 back2back test specifications.
218         """
219         traffic_ = self.traffic_defaults.copy()
220         result = OrderedDict()
221
222         if traffic:
223             traffic_ = merge_spec(traffic_, traffic)
224
225         results = get_user_traffic(
226             'back2back',
227             '%d tests, %d seconds iterations, %f packet loss, multistream '
228             '%s' % (tests, duration, lossrate, traffic['multistream']),
229             traffic_,
230             ('b2b frames', 'b2b frame loss %'))
231
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])
236         return result
237
238     def start_cont_traffic(self, traffic=None, duration=20):
239         return NotImplementedError('Dummy does not implement start_cont_traffic')
240
241     def stop_cont_traffic(self):
242         return NotImplementedError(
243             'Dummy does not implement stop_cont_traffic')
244
245     def start_rfc2544_back2back(self, traffic=None, tests=1, duration=20,
246                                 lossrate=0.0):
247         return NotImplementedError(
248             'Dummy does not implement start_rfc2544_back2back')
249
250     def wait_rfc2544_back2back(self):
251         return NotImplementedError(
252             'Dummy does not implement stop_cont_traffic')
253
254     def start_rfc2544_throughput(self, traffic=None, tests=1, duration=20,
255                                  lossrate=0.0):
256         return NotImplementedError(
257             'Dummy does not implement start_rfc2544_throughput')
258
259     def wait_rfc2544_throughput(self):
260         return NotImplementedError(
261             'Dummy does not implement wait_rfc2544_throughput')
262
263 if __name__ == '__main__':
264     TRAFFIC = {
265         'l3': {
266             'proto': 'tcp',
267             'srcip': '1.1.1.1',
268             'dstip': '90.90.90.90',
269         },
270     }
271
272     with Dummy() as dev:
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))