Improvie TRex RFC2544 throughput calculation
[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 import datetime
16
17 import mock
18 from trex_stl_lib import api as Pkt
19 from trex_stl_lib import trex_stl_client
20 from trex_stl_lib import trex_stl_packet_builder_scapy
21 from trex_stl_lib import trex_stl_streams
22
23 from yardstick.network_services.traffic_profile import rfc2544
24 from yardstick.tests.unit import base
25
26
27 class TestRFC2544Profile(base.BaseUnitTestCase):
28     TRAFFIC_PROFILE = {
29         "schema": "isb:traffic_profile:0.1",
30         "name": "fixed",
31         "description": "Fixed traffic profile to run UDP traffic",
32         "traffic_profile": {
33             "traffic_type": "FixedTraffic",
34             "frame_rate": 100,
35             "flow_number": 10,
36             "frame_size": 64}}
37
38     PROFILE = {'description': 'Traffic profile to run RFC2544 latency',
39                'name': 'rfc2544',
40                'traffic_profile': {'traffic_type': 'RFC2544Profile',
41                                    'frame_rate': 100},
42                'downlink_0':
43                    {'ipv4':
44                         {'outer_l2':
45                              {'framesize':
46                                   {'64B': '100', '1518B': '0',
47                                    '128B': '0', '1400B': '0',
48                                    '256B': '0', '373b': '0',
49                                    '570B': '0'}},
50                          'outer_l3v4':
51                              {'dstip4': '1.1.1.1-1.15.255.255',
52                               'proto': 'udp',
53                               'srcip4': '90.90.1.1-90.105.255.255',
54                               'dscp': 0, 'ttl': 32, 'count': 1},
55                          'outer_l4':
56                              {'srcport': '2001',
57                               'dsrport': '1234', 'count': 1}}},
58                'uplink_0':
59                    {'ipv4':
60                         {'outer_l2':
61                              {'framesize':
62                                   {'64B': '100', '1518B': '0',
63                                    '128B': '0', '1400B': '0',
64                                    '256B': '0', '373b': '0',
65                                    '570B': '0'}},
66                          'outer_l3v4':
67                              {'dstip4': '9.9.1.1-90.105.255.255',
68                               'proto': 'udp',
69                               'srcip4': '1.1.1.1-1.15.255.255',
70                               'dscp': 0, 'ttl': 32, 'count': 1},
71                          'outer_l4':
72                              {'dstport': '2001',
73                               'srcport': '1234', 'count': 1}}},
74                'schema': 'isb:traffic_profile:0.1'}
75
76     def test___init__(self):
77         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
78         self.assertEqual(rfc2544_profile.max_rate, rfc2544_profile.rate)
79         self.assertEqual(0, rfc2544_profile.min_rate)
80
81     def test_stop_traffic(self):
82         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
83         mock_generator = mock.Mock()
84         rfc2544_profile.stop_traffic(traffic_generator=mock_generator)
85         mock_generator.client.stop.assert_called_once()
86         mock_generator.client.reset.assert_called_once()
87         mock_generator.client.remove_all_streams.assert_called_once()
88
89     def test_execute_traffic(self):
90         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
91         mock_generator = mock.Mock()
92         mock_generator.networks = {
93             'downlink_0': ['xe0', 'xe1'],
94             'uplink_0': ['xe2', 'xe3'],
95             'downlink_1': []}
96         mock_generator.port_num.side_effect = [10, 20, 30, 40]
97         mock_generator.rfc2544_helper.correlated_traffic = False
98         rfc2544_profile.params = {
99             'downlink_0': 'profile1',
100             'uplink_0': 'profile2'}
101
102         with mock.patch.object(rfc2544_profile, '_create_profile') as \
103                 mock_create_profile:
104             rfc2544_profile.execute_traffic(traffic_generator=mock_generator)
105         mock_create_profile.assert_has_calls([
106             mock.call('profile1', rfc2544_profile.rate, mock.ANY),
107             mock.call('profile1', rfc2544_profile.rate, mock.ANY),
108             mock.call('profile2', rfc2544_profile.rate, mock.ANY),
109             mock.call('profile2', rfc2544_profile.rate, mock.ANY)])
110         mock_generator.client.add_streams.assert_has_calls([
111             mock.call(mock.ANY, ports=[10]),
112             mock.call(mock.ANY, ports=[20]),
113             mock.call(mock.ANY, ports=[30]),
114             mock.call(mock.ANY, ports=[40])])
115         mock_generator.client.start(ports=[10, 20, 30, 40],
116                                     duration=rfc2544_profile.config.duration,
117                                     force=True)
118
119     @mock.patch.object(trex_stl_streams, 'STLProfile')
120     def test__create_profile(self, mock_stl_profile):
121         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
122         port_pg_id = mock.ANY
123         profile_data = {'packetid_1': {'outer_l2': {'framesize': 'imix_info'}}}
124         rate = 100
125         with mock.patch.object(rfc2544_profile, '_create_imix_data') as \
126                 mock_create_imix, \
127                 mock.patch.object(rfc2544_profile, '_create_vm') as \
128                 mock_create_vm, \
129                 mock.patch.object(rfc2544_profile, '_create_streams') as \
130                 mock_create_streams:
131             mock_create_imix.return_value = 'imix_data'
132             mock_create_streams.return_value = ['stream1']
133             rfc2544_profile._create_profile(profile_data, rate, port_pg_id)
134
135         mock_create_imix.assert_called_once_with('imix_info')
136         mock_create_vm.assert_called_once_with(
137             {'outer_l2': {'framesize': 'imix_info'}})
138         mock_create_streams.assert_called_once_with('imix_data', 100,
139                                                     port_pg_id)
140         mock_stl_profile.assert_called_once_with(['stream1'])
141
142     def test__create_imix_data(self):
143         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
144         data = {'64B': 50, '128B': 50}
145         self.assertEqual({'64': 50.0, '128': 50.0},
146                          rfc2544_profile._create_imix_data(data))
147         data = {'64B': 1, '128b': 3}
148         self.assertEqual({'64': 25.0, '128': 75.0},
149                          rfc2544_profile._create_imix_data(data))
150         data = {}
151         self.assertEqual({}, rfc2544_profile._create_imix_data(data))
152
153     def test__create_vm(self):
154         packet = {'outer_l2': 'l2_definition'}
155         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
156         with mock.patch.object(rfc2544_profile, '_set_outer_l2_fields') as \
157                 mock_l2_fileds:
158             rfc2544_profile._create_vm(packet)
159         mock_l2_fileds.assert_called_once_with('l2_definition')
160
161     @mock.patch.object(trex_stl_packet_builder_scapy, 'STLPktBuilder',
162                        return_value='packet')
163     def test__create_single_packet(self, mock_pktbuilder):
164         size = 128
165         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
166         rfc2544_profile.ether_packet = Pkt.Eth()
167         rfc2544_profile.ip_packet = Pkt.IP()
168         rfc2544_profile.udp_packet = Pkt.UDP()
169         rfc2544_profile.trex_vm = 'trex_vm'
170         base_pkt = (rfc2544_profile.ether_packet / rfc2544_profile.ip_packet /
171                     rfc2544_profile.udp_packet)
172         pad = (size - len(base_pkt)) * 'x'
173         output = rfc2544_profile._create_single_packet(size=size)
174         mock_pktbuilder.assert_called_once_with(pkt=base_pkt / pad,
175                                                 vm='trex_vm')
176         self.assertEqual(output, 'packet')
177
178     @mock.patch.object(trex_stl_packet_builder_scapy, 'STLPktBuilder',
179                        return_value='packet')
180     def test__create_single_packet_qinq(self, mock_pktbuilder):
181         size = 128
182         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
183         rfc2544_profile.ether_packet = Pkt.Eth()
184         rfc2544_profile.ip_packet = Pkt.IP()
185         rfc2544_profile.udp_packet = Pkt.UDP()
186         rfc2544_profile.trex_vm = 'trex_vm'
187         rfc2544_profile.qinq = True
188         rfc2544_profile.qinq_packet = Pkt.Dot1Q(vlan=1) / Pkt.Dot1Q(vlan=2)
189         base_pkt = (rfc2544_profile.ether_packet /
190                     rfc2544_profile.qinq_packet / rfc2544_profile.ip_packet /
191                     rfc2544_profile.udp_packet)
192         pad = (size - len(base_pkt)) * 'x'
193         output = rfc2544_profile._create_single_packet(size=size)
194         mock_pktbuilder.assert_called_once_with(pkt=base_pkt / pad,
195                                                 vm='trex_vm')
196         self.assertEqual(output, 'packet')
197
198     @mock.patch.object(trex_stl_streams, 'STLFlowLatencyStats')
199     @mock.patch.object(trex_stl_streams, 'STLTXCont')
200     @mock.patch.object(trex_stl_client, 'STLStream')
201     def test__create_streams(self, mock_stream, mock_txcont, mock_latency):
202         imix_data = {'64': 25, '512': 75}
203         rate = 35
204         port_pg_id = rfc2544.PortPgIDMap()
205         port_pg_id.add_port(10)
206         mock_stream.side_effect = ['stream1', 'stream2']
207         mock_txcont.side_effect = ['txcont1', 'txcont2']
208         mock_latency.side_effect = ['latency1', 'latency2']
209         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
210         with mock.patch.object(rfc2544_profile, '_create_single_packet'):
211             output = rfc2544_profile._create_streams(imix_data, rate,
212                                                      port_pg_id)
213         self.assertEqual(['stream1', 'stream2'], output)
214         mock_latency.assert_has_calls([
215             mock.call(pg_id=1), mock.call(pg_id=2)])
216         mock_txcont.assert_has_calls([
217             mock.call(percentage=float(25 * 35) / 100),
218             mock.call(percentage=float(75 * 35) / 100)], any_order=True)
219
220     def test_get_drop_percentage(self):
221         rfc2544_profile = rfc2544.RFC2544Profile(self.TRAFFIC_PROFILE)
222         samples = [
223             {'xe1': {'tx_throughput_fps': 110,
224                      'rx_throughput_fps': 101,
225                      'out_packets': 2100,
226                      'in_packets': 2010,
227                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 1)},
228              'xe2': {'tx_throughput_fps': 210,
229                      'rx_throughput_fps': 201,
230                      'out_packets': 4100,
231                      'in_packets': 4010,
232                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 1)}},
233             {'xe1': {'tx_throughput_fps': 156,
234                      'rx_throughput_fps': 108,
235                      'out_packets': 2110,
236                      'in_packets': 2040,
237                      'latency': 'Latency1',
238                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 31)},
239              'xe2': {'tx_throughput_fps': 253,
240                      'rx_throughput_fps': 215,
241                      'out_packets': 4150,
242                      'in_packets': 4010,
243                      'latency': 'Latency2',
244                      'timestamp': datetime.datetime(2000, 1, 1, 1, 1, 1, 31)}}
245         ]
246         completed, output = rfc2544_profile.get_drop_percentage(
247             samples, 0, 0, False)
248         expected = {'DropPercentage': 50.0,
249                     'Latency': {'xe1': 'Latency1', 'xe2': 'Latency2'},
250                     'RxThroughput': 1000000.0,
251                     'TxThroughput': 2000000.0,
252                     'CurrentDropPercentage': 50.0,
253                     'Rate': 100.0,
254                     'Throughput': 1000000.0}
255         self.assertEqual(expected, output)
256         self.assertFalse(completed)
257
258
259 class PortPgIDMapTestCase(base.BaseUnitTestCase):
260
261     def test_add_port(self):
262         port_pg_id_map = rfc2544.PortPgIDMap()
263         port_pg_id_map.add_port(10)
264         self.assertEqual(10, port_pg_id_map._last_port)
265         self.assertEqual([], port_pg_id_map._port_pg_id_map[10])
266
267     def test_get_pg_ids(self):
268         port_pg_id_map = rfc2544.PortPgIDMap()
269         port_pg_id_map.add_port(10)
270         port_pg_id_map.increase_pg_id()
271         port_pg_id_map.increase_pg_id()
272         port_pg_id_map.add_port(20)
273         port_pg_id_map.increase_pg_id()
274         self.assertEqual([1, 2], port_pg_id_map.get_pg_ids(10))
275         self.assertEqual([3], port_pg_id_map.get_pg_ids(20))
276         self.assertEqual([], port_pg_id_map.get_pg_ids(30))
277
278     def test_increase_pg_id_no_port(self):
279         port_pg_id_map = rfc2544.PortPgIDMap()
280         self.assertIsNone(port_pg_id_map.increase_pg_id())
281
282     def test_increase_pg_id_last_port(self):
283         port_pg_id_map = rfc2544.PortPgIDMap()
284         port_pg_id_map.add_port(10)
285         self.assertEqual(1, port_pg_id_map.increase_pg_id())
286         self.assertEqual([1], port_pg_id_map.get_pg_ids(10))
287         self.assertEqual(10, port_pg_id_map._last_port)
288
289     def test_increase_pg_id(self):
290         port_pg_id_map = rfc2544.PortPgIDMap()
291         port_pg_id_map.add_port(10)
292         port_pg_id_map.increase_pg_id()
293         self.assertEqual(2, port_pg_id_map.increase_pg_id(port=20))
294         self.assertEqual([1], port_pg_id_map.get_pg_ids(10))
295         self.assertEqual([2], port_pg_id_map.get_pg_ids(20))
296         self.assertEqual(20, port_pg_id_map._last_port)