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