Merge "PROX: [WIP] Added scale up TCs."
[yardstick.git] / yardstick / tests / unit / network_services / traffic_profile / test_prox_binsearch.py
1 # Copyright (c) 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 STLClient = mock.MagicMock()
22 stl_patch = mock.patch.dict("sys.modules", STL_MOCKS)
23 stl_patch.start()
24
25 if stl_patch:
26     from yardstick.network_services.vnf_generic.vnf.prox_helpers import ProxTestDataTuple
27     from yardstick.network_services.traffic_profile.prox_binsearch import ProxBinSearchProfile
28
29
30 class TestProxBinSearchProfile(unittest.TestCase):
31
32     def test_execute_1(self):
33         def target(*args, **_):
34             runs.append(args[2])
35             if args[2] < 0 or args[2] > 100:
36                 raise RuntimeError(' '.join([str(args), str(runs)]))
37             if args[2] > 75.0:
38                 return fail_tuple, {}
39             return success_tuple, {}
40
41         tp_config = {
42             'traffic_profile': {
43                 'packet_sizes': [200],
44                 'test_precision': 2.0,
45                 'tolerated_loss': 0.001,
46             },
47         }
48
49         runs = []
50         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
51         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
52
53         traffic_generator = mock.MagicMock()
54
55         profile_helper = mock.MagicMock()
56         profile_helper.run_test = target
57
58         profile = ProxBinSearchProfile(tp_config)
59         profile.init(mock.MagicMock())
60         profile._profile_helper = profile_helper
61
62         profile.execute_traffic(traffic_generator)
63         self.assertEqual(round(profile.current_lower, 2), 74.69)
64         self.assertEqual(round(profile.current_upper, 2), 76.09)
65         self.assertEqual(len(runs), 7)
66
67         # Result Samples inc theor_max
68         result_tuple = {"Result_Actual_throughput": 7.5e-07,
69                         "Result_theor_max_throughput": 1.234e-10,
70                         "Result_pktSize": 200}
71         profile.queue.put.assert_called_with(result_tuple)
72
73         success_result_tuple = {"Success_CurrentDropPackets": 0.5,
74                                 "Success_DropPackets": 0.5,
75                                 "Success_LatencyAvg": 5.3,
76                                 "Success_LatencyMax": 5.2,
77                                 "Success_LatencyMin": 5.1,
78                                 "Success_PktSize": 200,
79                                 "Success_RxThroughput": 7.5e-07,
80                                 "Success_Throughput": 7.5e-07,
81                                 "Success_TxThroughput": 0.00012340000000000002}
82
83         calls = profile.queue.put(success_result_tuple)
84         profile.queue.put.assert_has_calls(calls)
85
86         success_result_tuple2 = {"Success_CurrentDropPackets": 0.5,
87                                 "Success_DropPackets": 0.5,
88                                 "Success_LatencyAvg": 5.3,
89                                 "Success_LatencyMax": 5.2,
90                                 "Success_LatencyMin": 5.1,
91                                 "Success_PktSize": 200,
92                                 "Success_RxThroughput": 7.5e-07,
93                                 "Success_Throughput": 7.5e-07,
94                                 "Success_TxThroughput": 123.4,
95                                 "Success_can_be_lost": 409600,
96                                 "Success_drop_total": 20480,
97                                 "Success_rx_total": 4075520,
98                                 "Success_tx_total": 4096000}
99
100         calls = profile.queue.put(success_result_tuple2)
101         profile.queue.put.assert_has_calls(calls)
102
103     def test_execute_2(self):
104         def target(*args, **_):
105             runs.append(args[2])
106             if args[2] < 0 or args[2] > 100:
107                 raise RuntimeError(' '.join([str(args), str(runs)]))
108             if args[2] > 25.0:
109                 return fail_tuple, {}
110             return success_tuple, {}
111
112         tp_config = {
113             'traffic_profile': {
114                 'packet_sizes': [200],
115                 'test_precision': 2.0,
116                 'tolerated_loss': 0.001,
117             },
118         }
119
120         runs = []
121         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
122         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
123
124         traffic_generator = mock.MagicMock()
125
126         profile_helper = mock.MagicMock()
127         profile_helper.run_test = target
128
129         profile = ProxBinSearchProfile(tp_config)
130         profile.init(mock.MagicMock())
131         profile._profile_helper = profile_helper
132
133         profile.execute_traffic(traffic_generator)
134         self.assertEqual(round(profile.current_lower, 2), 24.06)
135         self.assertEqual(round(profile.current_upper, 2), 25.47)
136         self.assertEqual(len(runs), 7)
137
138     def test_execute_3(self):
139         def target(*args, **_):
140             runs.append(args[2])
141             if args[2] < 0 or args[2] > 100:
142                 raise RuntimeError(' '.join([str(args), str(runs)]))
143             if args[2] > 75.0:
144                 return fail_tuple, {}
145             return success_tuple, {}
146
147         tp_config = {
148             'traffic_profile': {
149                 'packet_sizes': [200],
150                 'test_precision': 2.0,
151                 'tolerated_loss': 0.001,
152             },
153         }
154
155         runs = []
156         success_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.1, 5.2, 5.3], 995, 1000, 123.4)
157         fail_tuple = ProxTestDataTuple(10.0, 1, 2, 3, 4, [5.6, 5.7, 5.8], 850, 1000, 123.4)
158
159         traffic_generator = mock.MagicMock()
160
161         profile_helper = mock.MagicMock()
162         profile_helper.run_test = target
163
164         profile = ProxBinSearchProfile(tp_config)
165         profile.init(mock.MagicMock())
166         profile._profile_helper = profile_helper
167
168         profile.upper_bound = 100.0
169         profile.lower_bound = 99.0
170         profile.execute_traffic(traffic_generator)
171
172
173         # Result Samples
174         result_tuple = {"Result_theor_max_throughput": 0, "Result_pktSize": 200}
175         profile.queue.put.assert_called_with(result_tuple)
176
177         # Check for success_ tuple (None expected)
178         calls = profile.queue.put.mock_calls
179         for call in calls:
180             for call_detail in call[1]:
181                 for k in call_detail:
182                     if "Success_" in k:
183                         self.assertRaises(AttributeError)