summaryrefslogtreecommitdiffstats
path: root/yardstick/vTC/apexlake/tests/benchmarking_unit_test.py
blob: b0f800a4d352af2de01e82d8711f4cbd97a19966 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
# Copyright (c) 2015 Intel Research and Development Ireland Ltd.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__ = 'vmriccox'


import unittest
import mock
from experimental_framework.benchmarking_unit import BenchmarkingUnit
from experimental_framework.data_manager import DataManager
from experimental_framework.deployment_unit import DeploymentUnit
import experimental_framework.common as common
from experimental_framework.benchmarks.rfc2544_throughput_benchmark import \
    RFC2544ThroughputBenchmark


class DummyDataManager(DataManager):

    def __init__(self, experiment_directory):
        self.experiment_directory = experiment_directory
        self.experiments = dict()
        self.new_exp_counter = 0
        self.add_bench_counter = 0
        self.close_experiment_1_counter = 0
        self.close_experiment_2_counter = 0
        self.generate_csv_counter = 0

    def create_new_experiment(self, experiment_name, get_counter=None):
        if not get_counter:
            self.new_exp_counter += 1
        else:
            return self.new_exp_counter

    def add_benchmark(self, experiment_name, benchmark_name, get_counter=None):
        if not get_counter:
            self.add_bench_counter += 1
        else:
            return self.add_bench_counter

    def close_experiment(self, experiment, get_counter=None):
        if get_counter:
            return [self.close_experiment_1_counter,
                    self.close_experiment_2_counter]
        if experiment == 'VTC_base_single_vm_wait_1':
            self.close_experiment_1_counter += 1
        if experiment == 'VTC_base_single_vm_wait_2':
            self.close_experiment_2_counter += 1

    def generate_result_csv_file(self, get_counter=None):
        if get_counter:
            return self.generate_csv_counter
        else:
            self.generate_csv_counter += 1

    def add_metadata(self, experiment_name, metadata):
        pass

    def add_configuration(self, experiment_name, configuration):
        pass

    def add_data_points(self, experiment_name, benchmark_name, result):
        pass


class Dummy_2544(RFC2544ThroughputBenchmark):

    def __init__(self, name, params):
        self.name = name
        self.init_counter = 0
        self.finalize_counter = 0
        self.run_counter = 0
        self.params = params

    def init(self, get_counter=None):
        if get_counter:
            return self.init_counter
        else:
            self.init_counter += 1

    def finalize(self, get_counter=None):
        if get_counter:
            return self.finalize_counter
        else:
            self.finalize_counter += 1

    def run(self, get_counter=None):
        if get_counter:
            return self.run_counter
        else:
            self.run_counter += 1
        return {'throughput': 10}


class DummyDeploymentUnit(DeploymentUnit):

    def __init__(self, openstack_credentials):
        pass

    def deploy_heat_template(self, template_file, stack_name, parameters,
                             attempt=0):
        return False


class TestBenchmarkingUnit(unittest.TestCase):

    def setUp(self):
        pass

    def tearDown(self):
        pass

    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.heat.'
                'get_all_heat_templates')
    def test___init__(self, mock_heat, mock_dep_unit, mock_data_manager,
                      mock_temp_dir, mock_time):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/results/'
        common.TEMPLATE_FILE_EXTENSION = '.ext'
        common.RESULT_DIR = 'tests/data/results/'
        heat_template_name = 'name'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = ['bench_1', 'bench_2']
        bu = BenchmarkingUnit(heat_template_name,
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)
        self.assertEqual(bu.required_benchmarks, benchmarks)
        bu.heat_template_parameters = heat_template_parameters
        mock_data_manager.assert_called_once_with('tests/data/results/12345')
        mock_dep_unit.assert_called_once_with(openstack_credentials)
        mock_heat.assert_called_once_with('tests/data/results/', '.ext')

    @mock.patch('experimental_framework.benchmarks.'
                'rfc2544_throughput_benchmark', side_effect=Dummy_2544)
    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.'
                'heat.get_all_heat_templates')
    def test_initialize_for_success(self, mock_heat, mock_dep_unit,
                                    mock_data_manager, mock_temp_dir,
                                    mock_time, mock_rfc2544):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/test_templates/'
        common.TEMPLATE_FILE_EXTENSION = '.yaml'
        common.RESULT_DIR = 'tests/data/results/'

        heat_template_name = 'VTC_base_single_vm_wait_'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = [
            {
                'name':
                    'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark',
                'params': dict()
            }
        ]
        bu = BenchmarkingUnit(heat_template_name,
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)
        self.assertEqual(bu.required_benchmarks, benchmarks)
        bu.heat_template_parameters = heat_template_parameters
        bu.template_files = ['VTC_base_single_vm_wait_1.yaml',
                             'VTC_base_single_vm_wait_2.yaml']
        bu.initialize()
        self.assertTrue(len(bu.benchmarks) == 1)
        self.assertEqual(bu.benchmarks[0].__class__,
                         Dummy_2544)
        self.assertEqual(bu.data_manager.create_new_experiment('', True), 2)
        self.assertEqual(bu.data_manager.add_benchmark('', '', True), 2)

    @mock.patch('experimental_framework.benchmarks.'
                'rfc2544_throughput_benchmark', side_effect=Dummy_2544)
    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.'
                'heat.get_all_heat_templates')
    def test_finalize_for_success(
            self, mock_heat, mock_dep_unit, mock_data_manager, mock_temp_dir,
            mock_time, mock_rfc2544):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/test_templates/'
        common.TEMPLATE_FILE_EXTENSION = '.yaml'
        common.RESULT_DIR = 'tests/data/results/'

        heat_template_name = 'VTC_base_single_vm_wait_'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = [
            {
                'name':
                    'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark',
                'params': dict()
            }
        ]
        bu = BenchmarkingUnit(heat_template_name,
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)
        bu.heat_template_parameters = heat_template_parameters
        bu.template_files = ['VTC_base_single_vm_wait_1.yaml',
                             'VTC_base_single_vm_wait_2.yaml']
        bu.finalize()
        # self.assertEqual(bu.data_manager.close_experiment('', True), [1, 1])
        self.assertEqual(bu.data_manager.generate_result_csv_file(True), 1)

    @mock.patch('experimental_framework.common.push_data_influxdb')
    @mock.patch('experimental_framework.common.LOG')
    @mock.patch('experimental_framework.benchmarks.'
                'rfc2544_throughput_benchmark', side_effect=Dummy_2544)
    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.common.DEPLOYMENT_UNIT')
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.'
                'heat.get_all_heat_templates')
    def test_run_benchmarks_for_success(self, mock_heat, mock_common_dep_unit,
                                        mock_dep_unit, mock_data_manager,
                                        mock_temp_dir, mock_time,
                                        mock_rfc2544, mock_log, mock_influx):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/test_templates/'
        common.TEMPLATE_FILE_EXTENSION = '.yaml'
        common.RESULT_DIR = 'tests/data/results/'
        common.INFLUXDB_IP = 'InfluxIP'
        common.INFLUXDB_PORT = '8086'
        common.INFLUXDB_DB_NAME = 'test_db'

        heat_template_name = 'VTC_base_single_vm_wait_'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = [
            {
                'name':
                    'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark',
                'params': dict()
            }
        ]
        bu = BenchmarkingUnit(heat_template_name,
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)
        bu.data_manager = DummyDataManager('tests/data/results/12345')
        bu.template_files = ['VTC_base_single_vm_wait_1.yaml',
                             'VTC_base_single_vm_wait_2.yaml']
        bu.benchmarks = [Dummy_2544('dummy', {'param1': 'val1'})]
        bu.run_benchmarks()
        self.assertEqual(bu.benchmarks[0].init(True), 2)
        self.assertEqual(bu.benchmarks[0].finalize(True), 2)
        self.assertEqual(bu.benchmarks[0].run(True), 2)
        # expected_metric = \
        #     'throughput,vnic_type=direct,ram=1024,benchmark=dummy,' \
        #     'vcpus=2,experiment_name=VTC_base_single_vm_wait_2,' \
        #     'param1=val1 value=10 12345000000000'
        # mock_influx.assert_called_with(expected_metric)

    @mock.patch('experimental_framework.common.LOG')
    @mock.patch('experimental_framework.benchmarks.'
                'rfc2544_throughput_benchmark', side_effect=Dummy_2544)
    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.common.DEPLOYMENT_UNIT')
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.'
                'heat.get_all_heat_templates')
    def test_run_benchmarks_2_for_success(
            self, mock_heat, mock_common_dep_unit, mock_dep_unit,
            mock_data_manager, mock_temp_dir, mock_time, mock_rfc2544,
            mock_log):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/test_templates/'
        common.TEMPLATE_FILE_EXTENSION = '.yaml'
        common.RESULT_DIR = 'tests/data/results/'

        heat_template_name = 'VTC_base_single_vm_wait_'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = [
            {
                'name':
                    'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark',
                'params': dict()
            }
        ]
        bu = BenchmarkingUnit(heat_template_name,
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)
        bu.data_manager = DummyDataManager('tests/data/results/12345')
        bu.template_files = ['VTC_base_single_vm_wait_1.yaml',
                             'VTC_base_single_vm_wait_2.yaml']
        bu.benchmarks = [Dummy_2544('dummy', dict())]
        common.DEPLOYMENT_UNIT = DummyDeploymentUnit(dict())
        bu.run_benchmarks()
        self.assertEqual(bu.benchmarks[0].init(True), 2)
        self.assertEqual(bu.benchmarks[0].finalize(True), 0)
        self.assertEqual(bu.benchmarks[0].run(True), 0)

    @mock.patch('experimental_framework.common.LOG')
    @mock.patch('experimental_framework.benchmarks.'
                'rfc2544_throughput_benchmark', side_effect=Dummy_2544)
    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.common.DEPLOYMENT_UNIT')
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.'
                'heat.get_all_heat_templates')
    def test_get_benchmark_name_for_success(
            self, mock_heat, mock_common_dep_unit, mock_dep_unit,
            mock_data_manager, mock_temp_dir, mock_time, mock_rfc2544,
            mock_log):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/test_templates/'
        common.TEMPLATE_FILE_EXTENSION = '.yaml'
        common.RESULT_DIR = 'tests/data/results/'

        heat_template_name = 'VTC_base_single_vm_wait_'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = [
            {
                'name':
                    'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark',
                'params': dict()
            }
        ]
        bu = BenchmarkingUnit(heat_template_name,
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)

        expected = 'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark_0'
        output = bu.get_benchmark_name(
            'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark')
        self.assertEqual(expected, output)

        expected = 'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark_1'
        output = bu.get_benchmark_name(
            'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark')
        self.assertEqual(expected, output)

    @mock.patch('experimental_framework.common.LOG')
    @mock.patch('experimental_framework.benchmarks.'
                'rfc2544_throughput_benchmark', side_effect=Dummy_2544)
    @mock.patch('time.time')
    @mock.patch('experimental_framework.common.get_template_dir')
    @mock.patch('experimental_framework.data_manager.DataManager',
                side_effect=DummyDataManager)
    @mock.patch('experimental_framework.common.DEPLOYMENT_UNIT')
    @mock.patch('experimental_framework.deployment_unit.DeploymentUnit')
    @mock.patch('experimental_framework.benchmarking_unit.'
                'heat.get_all_heat_templates')
    def test_get_required_benchmarks_for_success(
            self, mock_heat, mock_common_dep_unit, mock_dep_unit,
            mock_data_manager, mock_temp_dir, mock_time, mock_rfc2544,
            mock_log):
        mock_heat.return_value = list()
        mock_time.return_value = '12345'
        mock_temp_dir.return_value = 'tests/data/test_templates/'
        common.TEMPLATE_FILE_EXTENSION = '.yaml'
        common.RESULT_DIR = 'tests/data/results/'
        openstack_credentials = {
            'name': 'aaa',
            'surname': 'bbb'
        }
        heat_template_parameters = {
            'param_1': 'name_1',
            'param_2': 'name_2'
        }
        iterations = 1
        benchmarks = [
            {
                'name':
                    'rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark',
                'params': dict()
            }
        ]
        bu = BenchmarkingUnit('',
                              openstack_credentials,
                              heat_template_parameters,
                              iterations,
                              benchmarks)
        req_benchs = \
            ['rfc2544_throughput_benchmark.RFC2544ThroughputBenchmark']
        output = bu.get_required_benchmarks(req_benchs)
        self.assertEqual(len(req_benchs), 1)
        self.assertEqual(output[0].__class__, Dummy_2544)