Merge "prox: fix TG KPIs"
[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
21 import unittest
22 import mock
23
24 from tests.unit import STL_MOCKS
25
26
27 STLClient = mock.MagicMock()
28 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
29 stl_patch.start()
30
31 if stl_patch:
32     from yardstick.network_services.traffic_profile.traffic_profile \
33         import TrexProfile
34     from yardstick.network_services.traffic_profile.rfc2544 import \
35         RFC2544Profile
36
37
38 class TestRFC2544Profile(unittest.TestCase):
39     TRAFFIC_PROFILE = {
40         "schema": "isb:traffic_profile:0.1",
41         "name": "fixed",
42         "description": "Fixed traffic profile to run UDP traffic",
43         "traffic_profile": {
44             "traffic_type": "FixedTraffic",
45             "frame_rate": 100,  # pps
46             "flow_number": 10,
47             "frame_size": 64}}
48
49     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
50                'name': 'rfc2544',
51                'traffic_profile': {'traffic_type': 'RFC2544Profile',
52                                    'frame_rate': 100},
53                'downlink_0': {'ipv4':
54                           {'outer_l2': {'framesize':
55                                         {'64B': '100', '1518B': '0',
56                                          '128B': '0', '1400B': '0',
57                                          '256B': '0', '373b': '0',
58                                          '570B': '0'}},
59                            'outer_l3v4': {'dstip4': '1.1.1.1-1.15.255.255',
60                                           'proto': 'udp',
61                                           'srcip4': '90.90.1.1-90.105.255.255',
62                                           'dscp': 0, 'ttl': 32, 'count': 1},
63                            'outer_l4': {'srcport': '2001',
64                                'dsrport': '1234', 'count': 1}}},
65                'uplink_0': {'ipv4':
66                            {'outer_l2': {'framesize':
67                                          {'64B': '100', '1518B': '0',
68                                           '128B': '0', '1400B': '0',
69                                           '256B': '0', '373b': '0',
70                                           '570B': '0'}},
71                             'outer_l3v4': {'dstip4': '9.9.1.1-90.105.255.255',
72                                            'proto': 'udp',
73                                            'srcip4': '1.1.1.1-1.15.255.255',
74                                            'dscp': 0, 'ttl': 32, 'count': 1},
75                             'outer_l4': {'dstport': '2001',
76                                 'srcport': '1234', 'count': 1}}},
77                'schema': 'isb:traffic_profile:0.1'}
78
79     def test___init__(self):
80         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
81         assert r_f_c2544_profile.rate
82
83     def test_execute(self):
84         traffic_generator = mock.Mock(autospec=TrexProfile)
85         traffic_generator.networks = {
86             "uplink_0": ["xe0"],
87             "downlink_0": ["xe1"],
88         }
89         traffic_generator.client = \
90             mock.Mock(return_value=True)
91         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
92         r_f_c2544_profile.params = self.PROFILE
93         r_f_c2544_profile.first_run = True
94         self.assertEqual(None, r_f_c2544_profile.execute_traffic(traffic_generator))
95
96     def test_get_drop_percentage(self):
97         traffic_generator = mock.Mock(autospec=TrexProfile)
98         traffic_generator.networks = {
99             "uplink_0": ["xe0"],
100             "downlink_0": ["xe1"],
101         }
102         traffic_generator.client = mock.Mock(return_value=True)
103
104         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
105         r_f_c2544_profile.params = self.PROFILE
106         r_f_c2544_profile.register_generator(traffic_generator)
107         self.assertIsNone(r_f_c2544_profile.execute_traffic(traffic_generator))
108
109         samples = {}
110         for ifname in range(1):
111             name = "xe{}".format(ifname)
112             samples[name] = {
113                 "rx_throughput_fps": 20,
114                 "tx_throughput_fps": 20,
115                 "rx_throughput_mbps": 10,
116                 "tx_throughput_mbps": 10,
117                 "in_packets": 1000,
118                 "out_packets": 1000,
119             }
120
121         expected = {
122             'DropPercentage': 0.0,
123             'RxThroughput': 100 / 3.0,
124             'TxThroughput': 100 / 3.0,
125             'CurrentDropPercentage': 0.0,
126             'Throughput': 66.66666666666667,
127             'xe0': {
128                 'tx_throughput_fps': 20,
129                 'in_packets': 1000,
130                 'out_packets': 1000,
131                 'rx_throughput_mbps': 10,
132                 'tx_throughput_mbps': 10,
133                 'rx_throughput_fps': 20,
134             },
135         }
136         traffic_generator.generate_samples = mock.MagicMock(return_value=samples)
137         traffic_generator.RUN_DURATION = 30
138         traffic_generator.rfc2544_helper.tolerance_low = 0.0001
139         traffic_generator.rfc2544_helper.tolerance_high = 0.0001
140         result = r_f_c2544_profile.get_drop_percentage(traffic_generator)
141         self.assertDictEqual(result, expected)
142
143     def test_get_drop_percentage_update(self):
144         traffic_generator = mock.Mock(autospec=RFC2544Profile)
145         traffic_generator.networks = {
146             "uplink_0": ["xe0"],
147             "downlink_0": ["xe1"],
148         }
149         traffic_generator.client = mock.Mock(return_value=True)
150
151         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
152         r_f_c2544_profile.params = self.PROFILE
153         r_f_c2544_profile.register_generator(traffic_generator)
154         self.assertIsNone(r_f_c2544_profile.execute_traffic())
155
156         samples = {}
157         for ifname in range(1):
158             name = "xe{}".format(ifname)
159             samples[name] = {
160                 "rx_throughput_fps": 20,
161                 "tx_throughput_fps": 20,
162                 "rx_throughput_mbps": 10,
163                 "tx_throughput_mbps": 10,
164                 "in_packets": 1000,
165                 "out_packets": 1002,
166             }
167         tol_min = 0.0
168         tolerance = 1.0
169         expected = {
170             'DropPercentage': 0.1996,
171             'RxThroughput': 33.333333333333336,
172             'TxThroughput': 33.4,
173             'CurrentDropPercentage': 0.1996,
174             'Throughput': 66.66666666666667,
175             'xe0': {
176                 'tx_throughput_fps': 20,
177                 'in_packets': 1000,
178                 'out_packets': 1002,
179                 'rx_throughput_mbps': 10,
180                 'tx_throughput_mbps': 10,
181                 'rx_throughput_fps': 20,
182             },
183         }
184         traffic_generator.generate_samples = mock.MagicMock(return_value=samples)
185         traffic_generator.RUN_DURATION = 30
186         traffic_generator.rfc2544_helper.tolerance_low = 0.0001
187         traffic_generator.rfc2544_helper.tolerance_high = 0.0001
188         result = r_f_c2544_profile.get_drop_percentage(traffic_generator)
189         self.assertDictEqual(expected, result)
190
191     def test_get_drop_percentage_div_zero(self):
192         traffic_generator = mock.Mock(autospec=TrexProfile)
193         traffic_generator.networks = {
194             "uplink_0": ["xe0"],
195             "downlink_0": ["xe1"],
196         }
197         traffic_generator.client = \
198             mock.Mock(return_value=True)
199         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
200         r_f_c2544_profile.params = self.PROFILE
201         self.assertEqual(None, r_f_c2544_profile.execute_traffic(traffic_generator))
202         samples = {}
203         for ifname in range(1):
204             name = "xe{}".format(ifname)
205             samples[name] = {"rx_throughput_fps": 20,
206                              "tx_throughput_fps": 20,
207                              "rx_throughput_mbps": 10,
208                              "tx_throughput_mbps": 10,
209                              "in_packets": 1000,
210                              "out_packets": 0}
211         tol_min = 0.0
212         tolerance = 0.0
213         r_f_c2544_profile.throughput_max = 0
214         expected = {
215             'DropPercentage': 100.0, 'RxThroughput': 100 / 3.0,
216             'TxThroughput': 0.0, 'CurrentDropPercentage': 100.0,
217             'Throughput': 66.66666666666667,
218             'xe0': {
219                 'tx_throughput_fps': 20, 'in_packets': 1000,
220                 'out_packets': 0, 'rx_throughput_mbps': 10,
221                 'tx_throughput_mbps': 10, 'rx_throughput_fps': 20
222             }
223         }
224         traffic_generator.generate_samples = mock.MagicMock(return_value=samples)
225         traffic_generator.RUN_DURATION = 30
226         traffic_generator.rfc2544_helper.tolerance_low = 0.0001
227         traffic_generator.rfc2544_helper.tolerance_high = 0.0001
228         self.assertDictEqual(expected,
229                              r_f_c2544_profile.get_drop_percentage(traffic_generator))
230
231     def test_get_multiplier(self):
232         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
233         r_f_c2544_profile.max_rate = 100
234         r_f_c2544_profile.min_rate = 100
235         self.assertEqual("1.0", r_f_c2544_profile.get_multiplier())
236
237     def test_calculate_pps(self):
238         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
239         r_f_c2544_profile.rate = 100
240         r_f_c2544_profile.pps = 100
241         samples = {'Throughput': 4549093.33}
242         self.assertEqual((2274546.67, 1.0),
243                          r_f_c2544_profile.calculate_pps(samples))
244
245     def test_create_single_stream(self):
246         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
247         r_f_c2544_profile._create_single_packet = mock.MagicMock()
248         r_f_c2544_profile.pg_id = 1
249         self.assertIsNotNone(
250             r_f_c2544_profile.create_single_stream(64, 2274546.67))
251
252     def test_create_single_stream_no_pg_id(self):
253         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
254         r_f_c2544_profile._create_single_packet = mock.MagicMock()
255         r_f_c2544_profile.pg_id = 0
256         self.assertIsNotNone(
257             r_f_c2544_profile.create_single_stream(64, 2274546.67))
258
259     def test_execute_latency(self):
260         traffic_generator = mock.Mock(autospec=TrexProfile)
261         traffic_generator.networks = {
262             "private_0": ["xe0"],
263             "public_0": ["xe1"],
264         }
265         traffic_generator.client = \
266             mock.Mock(return_value=True)
267         r_f_c2544_profile = RFC2544Profile(self.TRAFFIC_PROFILE)
268         r_f_c2544_profile.params = self.PROFILE
269         r_f_c2544_profile.first_run = True
270         samples = {}
271         for ifname in range(1):
272             name = "xe{}".format(ifname)
273             samples[name] = {"rx_throughput_fps": 20,
274                              "tx_throughput_fps": 20,
275                              "rx_throughput_mbps": 10,
276                              "tx_throughput_mbps": 10,
277                              "in_packets": 1000,
278                              "out_packets": 0}
279
280         samples['Throughput'] = 4549093.33
281         r_f_c2544_profile.calculate_pps = mock.Mock(return_value=[2274546.67,
282                                                                   1.0])
283
284         self.assertEqual(None,
285                          r_f_c2544_profile.execute_latency(traffic_generator,
286                                                            samples))
287
288
289 if __name__ == '__main__':
290     unittest.main()