4f71fbb36f5aba5a081c1c91f94e8dc69cb169ed
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / compute / test_ramspeed.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
5 #
6 # All rights reserved. This program and the accompanying materials
7 # are made available under the terms of the Apache License, Version 2.0
8 # which accompanies this distribution, and is available at
9 # http://www.apache.org/licenses/LICENSE-2.0
10 ##############################################################################
11
12 # Unittest for yardstick.benchmark.scenarios.compute.ramspeed.Ramspeed
13
14 from __future__ import absolute_import
15
16 import unittest
17
18 import mock
19 from oslo_serialization import jsonutils
20
21 from yardstick.common import utils
22 from yardstick.benchmark.scenarios.compute import ramspeed
23
24
25 @mock.patch('yardstick.benchmark.scenarios.compute.ramspeed.ssh')
26 class RamspeedTestCase(unittest.TestCase):
27
28     def setUp(self):
29         self.ctx = {
30             'host': {
31                 'ip': '172.16.0.137',
32                 'user': 'root',
33                 'key_filename': "mykey.key"
34             }
35         }
36
37         self.result = {}
38
39     def test_ramspeed_successful_setup(self, mock_ssh):
40
41         r = ramspeed.Ramspeed({}, self.ctx)
42         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
43
44         r.setup()
45         self.assertIsNotNone(r.client)
46         self.assertTrue(r.setup_done, True)
47
48     def test_ramspeed_successful__run_no_sla(self, mock_ssh):
49
50         options = {
51             "test_id": 1,
52             "load": 16,
53             "block_size": 32
54         }
55         args = {"options": options}
56         r = ramspeed.Ramspeed(args, self.ctx)
57
58         sample_output = '{"Result": [{"Test_type": "INTEGER & WRITING",\
59  "Block_size(kb)": 1, "Bandwidth(MBps)": 19909.18}, {"Test_type":\
60  "INTEGER & WRITING", "Block_size(kb)": 2, "Bandwidth(MBps)": 19873.89},\
61  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 4, "Bandwidth(MBps)":\
62  19907.56}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 8,\
63  "Bandwidth(MBps)": 19906.94}, {"Test_type": "INTEGER & WRITING",\
64  "Block_size(kb)": 16, "Bandwidth(MBps)": 19881.74}, {"Test_type":\
65  "INTEGER & WRITING", "Block_size(kb)": 32, "Bandwidth(MBps)": 19395.65},\
66  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 64, "Bandwidth(MBps)":\
67  17623.14}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 128,\
68  "Bandwidth(MBps)": 17677.36}, {"Test_type": "INTEGER & WRITING",\
69  "Block_size(kb)": 256, "Bandwidth(MBps)": 16113.49}, {"Test_type":\
70  "INTEGER & WRITING", "Block_size(kb)": 512, "Bandwidth(MBps)": 14659.19},\
71  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 1024, "Bandwidth(MBps)":\
72  14680.75}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 2048,\
73  "Bandwidth(MBps)": 14756.45}, {"Test_type": "INTEGER & WRITING",\
74  "Block_size(kb)": 4096, "Bandwidth(MBps)": 14604.44}, {"Test_type":\
75  "INTEGER & WRITING", "Block_size(kb)": 8192, "Bandwidth(MBps)": 14159.86},\
76  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 16384,\
77  "Bandwidth(MBps)": 14128.94}, {"Test_type": "INTEGER & WRITING",\
78  "Block_size(kb)": 32768, "Bandwidth(MBps)": 8340.85}]}'
79         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
80         r.run(self.result)
81         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
82         self.assertEqual(self.result, expected_result)
83
84     def test_ramspeed_successful_run_sla(self, mock_ssh):
85
86         options = {
87             "test_id": 1,
88             "load": 16,
89             "block_size": 32
90         }
91         args = {"options": options, "sla": {"min_bandwidth": 6000}}
92         r = ramspeed.Ramspeed(args, self.ctx)
93
94         sample_output = '{"Result": [{"Test_type": "INTEGER & WRITING",\
95  "Block_size(kb)": 1, "Bandwidth(MBps)": 19909.18}, {"Test_type":\
96  "INTEGER & WRITING", "Block_size(kb)": 2, "Bandwidth(MBps)": 19873.89},\
97  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 4, "Bandwidth(MBps)":\
98  19907.56}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 8,\
99  "Bandwidth(MBps)": 19906.94}, {"Test_type": "INTEGER & WRITING",\
100  "Block_size(kb)": 16, "Bandwidth(MBps)": 19881.74}, {"Test_type":\
101  "INTEGER & WRITING", "Block_size(kb)": 32, "Bandwidth(MBps)": 19395.65},\
102  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 64, "Bandwidth(MBps)":\
103  17623.14}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 128,\
104  "Bandwidth(MBps)": 17677.36}, {"Test_type": "INTEGER & WRITING",\
105  "Block_size(kb)": 256, "Bandwidth(MBps)": 16113.49}, {"Test_type":\
106  "INTEGER & WRITING", "Block_size(kb)": 512, "Bandwidth(MBps)": 14659.19},\
107  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 1024, "Bandwidth(MBps)":\
108  14680.75}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 2048,\
109  "Bandwidth(MBps)": 14756.45}, {"Test_type": "INTEGER & WRITING",\
110  "Block_size(kb)": 4096, "Bandwidth(MBps)": 14604.44}, {"Test_type":\
111  "INTEGER & WRITING", "Block_size(kb)": 8192, "Bandwidth(MBps)": 14159.86},\
112  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 16384,\
113  "Bandwidth(MBps)": 14128.94}, {"Test_type": "INTEGER & WRITING",\
114  "Block_size(kb)": 32768, "Bandwidth(MBps)": 8340.85}]}'
115         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
116         r.run(self.result)
117         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
118         self.assertEqual(self.result, expected_result)
119
120     def test_ramspeed_unsuccessful_run_sla(self, mock_ssh):
121         options = {
122             "test_id": 1,
123             "load": 8,
124             "block_size": 64
125         }
126         args = {"options": options, "sla": {"min_bandwidth": 100000}}
127         r = ramspeed.Ramspeed(args, self.ctx)
128
129         sample_output = '{"Result": [{"Test_type": "INTEGER & WRITING",\
130  "Block_size(kb)": 1, "Bandwidth(MBps)": 5000.18}, {"Test_type":\
131  "INTEGER & WRITING", "Block_size(kb)": 2, "Bandwidth(MBps)": 5000.89},\
132  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 4,\
133  "Bandwidth(MBps)": 5000.56}, {"Test_type": "INTEGER & WRITING",\
134  "Block_size(kb)": 8, "Bandwidth(MBps)": 19906.94}, {"Test_type":\
135  "INTEGER & WRITING", "Block_size(kb)": 16, "Bandwidth(MBps)": 19881.74},\
136  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 32,\
137  "Bandwidth(MBps)": 19395.65}, {"Test_type": "INTEGER & WRITING",\
138  "Block_size(kb)": 64, "Bandwidth(MBps)": 17623.14}, {"Test_type":\
139  "INTEGER & WRITING", "Block_size(kb)": 128, "Bandwidth(MBps)": 17677.36},\
140  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 256, "Bandwidth(MBps)":\
141  16113.49}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 512,\
142  "Bandwidth(MBps)": 14659.19}, {"Test_type": "INTEGER & WRITING",\
143  "Block_size(kb)": 1024, "Bandwidth(MBps)": 14680.75}, {"Test_type":\
144  "INTEGER & WRITING", "Block_size(kb)": 2048, "Bandwidth(MBps)": 14756.45},\
145  {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 4096, "Bandwidth(MBps)":\
146  14604.44}, {"Test_type": "INTEGER & WRITING", "Block_size(kb)": 8192,\
147  "Bandwidth(MBps)": 14159.86}, {"Test_type": "INTEGER & WRITING",\
148  "Block_size(kb)": 16384, "Bandwidth(MBps)": 14128.94}, {"Test_type":\
149  "INTEGER & WRITING", "Block_size(kb)": 32768, "Bandwidth(MBps)": 8340.85}]}'
150         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
151         self.assertRaises(AssertionError, r.run, self.result)
152
153     def test_ramspeed_unsuccessful_script_error(self, mock_ssh):
154         options = {
155             "test_id": 1,
156             "load": 16,
157             "block_size": 32
158         }
159         args = {"options": options}
160         r = ramspeed.Ramspeed(args, self.ctx)
161
162         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
163         self.assertRaises(RuntimeError, r.run, self.result)
164
165     def test_ramspeed_mem_successful_run_no_sla(self, mock_ssh):
166         options = {
167             "test_id": 3,
168             "load": 16,
169             "block_size": 32,
170             "iteration": 1
171         }
172         args = {"options": options}
173         r = ramspeed.Ramspeed(args, self.ctx)
174
175         sample_output = '{"Result": [{"Test_type": "INTEGER Copy:",\
176  "Bandwidth(MBps)": 8353.97}, {"Test_type": "INTEGER Scale:",\
177  "Bandwidth(MBps)": 9078.59}, {"Test_type": "INTEGER Add:",\
178  "Bandwidth(MBps)": 10057.48}, {"Test_type": "INTEGER Triad:",\
179  "Bandwidth(MBps)": 10116.27}, {"Test_type": "INTEGER AVERAGE:",\
180  "Bandwidth(MBps)": 9401.58}]}'
181         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
182         r.run(self.result)
183         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
184         self.assertEqual(self.result, expected_result)
185
186     def test_ramspeed_mem_successful_run_sla(self, mock_ssh):
187         options = {
188             "test_id": 3,
189             "load": 16,
190             "block_size": 32,
191             "iteration": 1
192         }
193         args = {"options": options, "sla": {"min_bandwidth": 6000}}
194         r = ramspeed.Ramspeed(args, self.ctx)
195
196         sample_output = '{"Result": [{"Test_type": "INTEGER Copy:",\
197  "Bandwidth(MBps)": 8353.97}, {"Test_type": "INTEGER Scale:",\
198  "Bandwidth(MBps)": 9078.59}, {"Test_type": "INTEGER Add:",\
199  "Bandwidth(MBps)": 10057.48}, {"Test_type": "INTEGER Triad:",\
200  "Bandwidth(MBps)": 10116.27}, {"Test_type": "INTEGER AVERAGE:",\
201  "Bandwidth(MBps)": 9401.58}]}'
202         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
203         r.run(self.result)
204         expected_result = utils.flatten_dict_key(jsonutils.loads(sample_output))
205         self.assertEqual(self.result, expected_result)
206
207     def test_ramspeed_mem_unsuccessful_run_sla(self, mock_ssh):
208         options = {
209             "test_id": 3,
210             "load": 16,
211             "block_size": 32,
212             "iteration": 1
213         }
214         args = {"options": options, "sla": {"min_bandwidth": 86000}}
215         r = ramspeed.Ramspeed(args, self.ctx)
216
217         sample_output = '{"Result": [{"Test_type": "INTEGER Copy:",\
218  "Bandwidth(MBps)": 4000.97}, {"Test_type": "INTEGER Scale:",\
219  "Bandwidth(MBps)": 4400.59}, {"Test_type": "INTEGER Add:",\
220  "Bandwidth(MBps)": 4300.48}, {"Test_type": "INTEGER Triad:",\
221  "Bandwidth(MBps)": 1300.27}, {"Test_type": "INTEGER AVERAGE:",\
222  "Bandwidth(MBps)": 2401.58}]}'
223         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
224         self.assertRaises(AssertionError, r.run, self.result)
225
226     def test_ramspeed_unsuccessful_unknown_type_run(self, mock_ssh):
227         options = {
228             "test_id": 30,
229             "load": 16,
230             "block_size": 32
231         }
232         args = {'options': options}
233         r = ramspeed.Ramspeed(args, self.ctx)
234
235         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'No such type_id: 30 for \
236                                                Ramspeed scenario')
237         self.assertRaises(RuntimeError, r.run, self.result)
238
239
240 def main():
241     unittest.main()
242
243 if __name__ == '__main__':
244     main()