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