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