Re-define the framesize and flow dynamic define in testcase
[yardstick.git] / tests / unit / network_services / traffic_profile / test_rfc2544.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2016-2017 Intel Corporation
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 from __future__ import absolute_import
19 from __future__ import division
20 import unittest
21 import mock
22
23 STL_MOCKS = {
24     'stl': mock.MagicMock(),
25     'stl.trex_stl_lib': mock.MagicMock(),
26     'stl.trex_stl_lib.base64': mock.MagicMock(),
27     'stl.trex_stl_lib.binascii': mock.MagicMock(),
28     'stl.trex_stl_lib.collections': mock.MagicMock(),
29     'stl.trex_stl_lib.copy': mock.MagicMock(),
30     'stl.trex_stl_lib.datetime': mock.MagicMock(),
31     'stl.trex_stl_lib.functools': mock.MagicMock(),
32     'stl.trex_stl_lib.imp': mock.MagicMock(),
33     'stl.trex_stl_lib.inspect': mock.MagicMock(),
34     'stl.trex_stl_lib.json': mock.MagicMock(),
35     'stl.trex_stl_lib.linecache': mock.MagicMock(),
36     'stl.trex_stl_lib.math': mock.MagicMock(),
37     'stl.trex_stl_lib.os': mock.MagicMock(),
38     'stl.trex_stl_lib.platform': mock.MagicMock(),
39     'stl.trex_stl_lib.pprint': mock.MagicMock(),
40     'stl.trex_stl_lib.random': mock.MagicMock(),
41     'stl.trex_stl_lib.re': mock.MagicMock(),
42     'stl.trex_stl_lib.scapy': mock.MagicMock(),
43     'stl.trex_stl_lib.socket': mock.MagicMock(),
44     'stl.trex_stl_lib.string': mock.MagicMock(),
45     'stl.trex_stl_lib.struct': mock.MagicMock(),
46     'stl.trex_stl_lib.sys': mock.MagicMock(),
47     'stl.trex_stl_lib.threading': mock.MagicMock(),
48     'stl.trex_stl_lib.time': mock.MagicMock(),
49     'stl.trex_stl_lib.traceback': mock.MagicMock(),
50     'stl.trex_stl_lib.trex_stl_async_client': mock.MagicMock(),
51     'stl.trex_stl_lib.trex_stl_client': mock.MagicMock(),
52     'stl.trex_stl_lib.trex_stl_exceptions': mock.MagicMock(),
53     'stl.trex_stl_lib.trex_stl_ext': mock.MagicMock(),
54     'stl.trex_stl_lib.trex_stl_jsonrpc_client': mock.MagicMock(),
55     'stl.trex_stl_lib.trex_stl_packet_builder_interface': mock.MagicMock(),
56     'stl.trex_stl_lib.trex_stl_packet_builder_scapy': mock.MagicMock(),
57     'stl.trex_stl_lib.trex_stl_port': mock.MagicMock(),
58     'stl.trex_stl_lib.trex_stl_stats': mock.MagicMock(),
59     'stl.trex_stl_lib.trex_stl_streams': mock.MagicMock(),
60     'stl.trex_stl_lib.trex_stl_types': mock.MagicMock(),
61     'stl.trex_stl_lib.types': mock.MagicMock(),
62     'stl.trex_stl_lib.utils': mock.MagicMock(),
63     'stl.trex_stl_lib.utils.argparse': mock.MagicMock(),
64     'stl.trex_stl_lib.utils.collections': mock.MagicMock(),
65     'stl.trex_stl_lib.utils.common': mock.MagicMock(),
66     'stl.trex_stl_lib.utils.json': mock.MagicMock(),
67     'stl.trex_stl_lib.utils.os': mock.MagicMock(),
68     'stl.trex_stl_lib.utils.parsing_opts': mock.MagicMock(),
69     'stl.trex_stl_lib.utils.pwd': mock.MagicMock(),
70     'stl.trex_stl_lib.utils.random': mock.MagicMock(),
71     'stl.trex_stl_lib.utils.re': mock.MagicMock(),
72     'stl.trex_stl_lib.utils.string': mock.MagicMock(),
73     'stl.trex_stl_lib.utils.sys': mock.MagicMock(),
74     'stl.trex_stl_lib.utils.text_opts': mock.MagicMock(),
75     'stl.trex_stl_lib.utils.text_tables': mock.MagicMock(),
76     'stl.trex_stl_lib.utils.texttable': mock.MagicMock(),
77     'stl.trex_stl_lib.warnings': mock.MagicMock(),
78     'stl.trex_stl_lib.yaml': mock.MagicMock(),
79     'stl.trex_stl_lib.zlib': mock.MagicMock(),
80     'stl.trex_stl_lib.zmq': mock.MagicMock(),
81 }
82
83 STLClient = mock.MagicMock()
84 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
85 stl_patch.start()
86
87 if stl_patch:
88     from yardstick.network_services.traffic_profile.traffic_profile \
89         import TrexProfile
90     from yardstick.network_services.traffic_profile.rfc2544 import \
91         RFC2544Profile
92
93
94 class TestRFC2544Profile(unittest.TestCase):
95     TRAFFIC_PROFILE = {
96         "schema": "isb:traffic_profile:0.1",
97         "name": "fixed",
98         "description": "Fixed traffic profile to run UDP traffic",
99         "traffic_profile": {
100             "traffic_type": "FixedTraffic",
101             "frame_rate": 100,  # pps
102             "flow_number": 10,
103             "frame_size": 64}}
104
105     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
106                'name': 'rfc2544',
107                'traffic_profile': {'traffic_type': 'RFC2544Profile',
108                                    'frame_rate': 100},
109                'public_1': {'ipv4':
110                           {'outer_l2': {'framesize':
111                                         {'64B': '100', '1518B': '0',
112                                          '128B': '0', '1400B': '0',
113                                          '256B': '0', '373b': '0',
114                                          '570B': '0'}},
115                            'outer_l3v4': {'dstip4': '1.1.1.1-1.15.255.255',
116                                           'proto': 'udp',
117                                           'srcip4': '90.90.1.1-90.105.255.255',
118                                           'dscp': 0, 'ttl': 32, 'count': 1},
119                            'outer_l4': {'srcport': '2001',
120                                'dsrport': '1234', 'count': 1}}},
121                'private_1': {'ipv4':
122                            {'outer_l2': {'framesize':
123                                          {'64B': '100', '1518B': '0',
124                                           '128B': '0', '1400B': '0',
125                                           '256B': '0', '373b': '0',
126                                           '570B': '0'}},
127                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
128                                            'proto': 'udp',
129                                            'srcip4': '1.1.1.1-1.15.255.255',
130                                            'dscp': 0, 'ttl': 32, 'count': 1},
131                             'outer_l4': {'dstport': '2001',
132                                 'srcport': '1234', 'count': 1}}},
133                'schema': 'isb:traffic_profile:0.1'}
134
135     def test___init__(self):
136         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
137         assert r_f_c2544_profile.rate
138
139     def test_execute(self):
140         traffic_generator = mock.Mock(autospec=TrexProfile)
141         traffic_generator.my_ports = [0, 1]
142         traffic_generator.priv_ports = [-1]
143         traffic_generator.pub_ports = [1]
144         traffic_generator.client = \
145             mock.Mock(return_value=True)
146         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
147         r_f_c2544_profile.params = self.PROFILE
148         r_f_c2544_profile.first_run = True
149         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
150
151     def test_get_drop_percentage(self):
152         traffic_generator = mock.Mock(autospec=TrexProfile)
153         traffic_generator.my_ports = [0, 1]
154         traffic_generator.priv_ports = [0]
155         traffic_generator.pub_ports = [1]
156         traffic_generator.client = mock.Mock(return_value=True)
157
158         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
159         r_f_c2544_profile.params = self.PROFILE
160         r_f_c2544_profile.register_generator(traffic_generator)
161         self.assertIsNone(r_f_c2544_profile.execute(traffic_generator))
162
163         samples = {}
164         for ifname in range(1):
165             name = "xe{}".format(ifname)
166             samples[name] = {
167                 "rx_throughput_fps": 20,
168                 "tx_throughput_fps": 20,
169                 "rx_throughput_mbps": 10,
170                 "tx_throughput_mbps": 10,
171                 "in_packets": 1000,
172                 "out_packets": 1000,
173             }
174
175         expected = {
176             'DropPercentage': 0.0,
177             'RxThroughput': 100 / 3.0,
178             'TxThroughput': 100 / 3.0,
179             'CurrentDropPercentage': 0.0,
180             'Throughput': 66.66666666666667,
181             'xe0': {
182                 'tx_throughput_fps': 20,
183                 'in_packets': 1000,
184                 'out_packets': 1000,
185                 'rx_throughput_mbps': 10,
186                 'tx_throughput_mbps': 10,
187                 'rx_throughput_fps': 20,
188             },
189         }
190         traffic_generator.generate_samples = mock.MagicMock(return_value=samples)
191         traffic_generator.RUN_DURATION = 30
192         traffic_generator.rfc2544_helper.tolerance_low = 0.0001
193         traffic_generator.rfc2544_helper.tolerance_high = 0.0001
194         result = r_f_c2544_profile.get_drop_percentage(traffic_generator)
195         self.assertDictEqual(result, expected)
196
197     def test_get_drop_percentage_update(self):
198         traffic_generator = mock.Mock(autospec=RFC2544Profile)
199         traffic_generator.my_ports = [0, 1]
200         traffic_generator.priv_ports = [0]
201         traffic_generator.pub_ports = [1]
202         traffic_generator.client = mock.Mock(return_value=True)
203
204         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
205         r_f_c2544_profile.params = self.PROFILE
206         r_f_c2544_profile.register_generator(traffic_generator)
207         self.assertIsNone(r_f_c2544_profile.execute())
208
209         samples = {}
210         for ifname in range(1):
211             name = "xe{}".format(ifname)
212             samples[name] = {
213                 "rx_throughput_fps": 20,
214                 "tx_throughput_fps": 20,
215                 "rx_throughput_mbps": 10,
216                 "tx_throughput_mbps": 10,
217                 "in_packets": 1000,
218                 "out_packets": 1002,
219             }
220         tol_min = 0.0
221         tolerance = 1.0
222         expected = {
223             'DropPercentage': 0.1996,
224             'RxThroughput': 33.333333333333336,
225             'TxThroughput': 33.4,
226             'CurrentDropPercentage': 0.1996,
227             'Throughput': 66.66666666666667,
228             'xe0': {
229                 'tx_throughput_fps': 20,
230                 'in_packets': 1000,
231                 'out_packets': 1002,
232                 'rx_throughput_mbps': 10,
233                 'tx_throughput_mbps': 10,
234                 'rx_throughput_fps': 20,
235             },
236         }
237         traffic_generator.generate_samples = mock.MagicMock(return_value=samples)
238         traffic_generator.RUN_DURATION = 30
239         traffic_generator.rfc2544_helper.tolerance_low = 0.0001
240         traffic_generator.rfc2544_helper.tolerance_high = 0.0001
241         result = r_f_c2544_profile.get_drop_percentage(traffic_generator)
242         self.assertDictEqual(expected, result)
243
244     def test_get_drop_percentage_div_zero(self):
245         traffic_generator = mock.Mock(autospec=TrexProfile)
246         traffic_generator.my_ports = [0, 1]
247         traffic_generator.priv_ports = [0]
248         traffic_generator.pub_ports = [1]
249         traffic_generator.client = \
250             mock.Mock(return_value=True)
251         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
252         r_f_c2544_profile.params = self.PROFILE
253         self.assertEqual(None, r_f_c2544_profile.execute(traffic_generator))
254         samples = {}
255         for ifname in range(1):
256             name = "xe{}".format(ifname)
257             samples[name] = {"rx_throughput_fps": 20,
258                              "tx_throughput_fps": 20,
259                              "rx_throughput_mbps": 10,
260                              "tx_throughput_mbps": 10,
261                              "in_packets": 1000,
262                              "out_packets": 0}
263         tol_min = 0.0
264         tolerance = 0.0
265         r_f_c2544_profile.throughput_max = 0
266         expected = {
267             'DropPercentage': 100.0, 'RxThroughput': 100 / 3.0,
268             'TxThroughput': 0.0, 'CurrentDropPercentage': 100.0,
269             'Throughput': 66.66666666666667,
270             'xe0': {
271                 'tx_throughput_fps': 20, 'in_packets': 1000,
272                 'out_packets': 0, 'rx_throughput_mbps': 10,
273                 'tx_throughput_mbps': 10, 'rx_throughput_fps': 20
274             }
275         }
276         traffic_generator.generate_samples = mock.MagicMock(return_value=samples)
277         traffic_generator.RUN_DURATION = 30
278         traffic_generator.rfc2544_helper.tolerance_low = 0.0001
279         traffic_generator.rfc2544_helper.tolerance_high = 0.0001
280         self.assertDictEqual(expected,
281                              r_f_c2544_profile.get_drop_percentage(traffic_generator))
282
283     def test_get_multiplier(self):
284         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
285         r_f_c2544_profile.max_rate = 100
286         r_f_c2544_profile.min_rate = 100
287         self.assertEqual("1.0", r_f_c2544_profile.get_multiplier())
288
289     def test_calculate_pps(self):
290         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
291         r_f_c2544_profile.rate = 100
292         r_f_c2544_profile.pps = 100
293         samples = {'Throughput': 4549093.33}
294         self.assertEqual((2274546.67, 1.0),
295                          r_f_c2544_profile.calculate_pps(samples))
296
297     def test_create_single_stream(self):
298         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
299         r_f_c2544_profile._create_single_packet = mock.MagicMock()
300         r_f_c2544_profile.pg_id = 1
301         self.assertIsNotNone(
302             r_f_c2544_profile.create_single_stream(64, 2274546.67))
303
304     def test_create_single_stream_no_pg_id(self):
305         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
306         r_f_c2544_profile._create_single_packet = mock.MagicMock()
307         r_f_c2544_profile.pg_id = 0
308         self.assertIsNotNone(
309             r_f_c2544_profile.create_single_stream(64, 2274546.67))
310
311     def test_execute_latency(self):
312         traffic_generator = mock.Mock(autospec=TrexProfile)
313         traffic_generator.my_ports = [0, 1]
314         traffic_generator.priv_ports = [-1]
315         traffic_generator.pub_ports = [1]
316         traffic_generator.client = \
317             mock.Mock(return_value=True)
318         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
319         r_f_c2544_profile.params = self.PROFILE
320         r_f_c2544_profile.first_run = True
321         samples = {}
322         for ifname in range(1):
323             name = "xe{}".format(ifname)
324             samples[name] = {"rx_throughput_fps": 20,
325                              "tx_throughput_fps": 20,
326                              "rx_throughput_mbps": 10,
327                              "tx_throughput_mbps": 10,
328                              "in_packets": 1000,
329                              "out_packets": 0}
330
331         samples['Throughput'] = 4549093.33
332         r_f_c2544_profile.calculate_pps = mock.Mock(return_value=[2274546.67,
333                                                                   1.0])
334
335         self.assertEqual(None,
336                          r_f_c2544_profile.execute_latency(traffic_generator,
337                                                            samples))
338
339
340 if __name__ == '__main__':
341     unittest.main()