b3152d12c769fc4d38241dbd132aaeae7a40b33a
[yardstick.git] / yardstick / tests / unit / benchmark / scenarios / compute / test_lmbench.py
1 #!/usr/bin/env python
2
3 ##############################################################################
4 # Copyright (c) 2015 Ericsson AB 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.lmbench.Lmbench
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.benchmark.scenarios.compute import lmbench
22
23
24 # pylint: disable=unused-argument
25 # disable this for now because I keep forgetting mock patch arg ordering
26
27
28 @mock.patch('yardstick.benchmark.scenarios.compute.lmbench.ssh')
29 class LmbenchTestCase(unittest.TestCase):
30
31     def setUp(self):
32         self.ctx = {
33             'host': {
34                 'ip': '172.16.0.137',
35                 'user': 'cirros',
36                 'key_filename': "mykey.key"
37             }
38         }
39
40         self.result = {}
41
42     def test_successful_setup(self, mock_ssh):
43
44         l = lmbench.Lmbench({}, self.ctx)
45         mock_ssh.SSH.from_node().execute.return_value = (0, '', '')
46
47         l.setup()
48         self.assertIsNotNone(l.client)
49         self.assertTrue(l.setup_done)
50
51     def test_unsuccessful_unknown_type_run(self, mock_ssh):
52
53         options = {
54             "test_type": "foo"
55         }
56         args = {'options': options}
57
58         l = lmbench.Lmbench(args, self.ctx)
59
60         self.assertRaises(RuntimeError, l.run, self.result)
61
62     def test_successful_latency_run_no_sla(self, mock_ssh):
63
64         options = {
65             "test_type": "latency",
66             "stride": 64,
67             "stop_size": 16
68         }
69         args = {'options': options}
70         l = lmbench.Lmbench(args, self.ctx)
71
72         sample_output = '[{"latency": 4.944, "size": 0.00049}]'
73         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
74         l.run(self.result)
75         expected_result = {"latencies0.latency": 4.944, "latencies0.size": 0.00049}
76         self.assertEqual(self.result, expected_result)
77
78     def test_successful_bandwidth_run_no_sla(self, mock_ssh):
79
80         options = {
81             "test_type": "bandwidth",
82             "size": 500,
83             "benchmark": "rd",
84             "warmup": 0
85         }
86         args = {"options": options}
87         l = lmbench.Lmbench(args, self.ctx)
88
89         sample_output = '{"size(MB)": 0.262144, "bandwidth(MBps)": 11025.5}'
90         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
91         l.run(self.result)
92         expected_result = jsonutils.loads(sample_output)
93         self.assertEqual(self.result, expected_result)
94
95     def test_successful_latency_run_sla(self, mock_ssh):
96
97         options = {
98             "test_type": "latency",
99             "stride": 64,
100             "stop_size": 16
101         }
102         args = {
103             "options": options,
104             "sla": {"max_latency": 35}
105         }
106         l = lmbench.Lmbench(args, self.ctx)
107
108         sample_output = '[{"latency": 4.944, "size": 0.00049}]'
109         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
110         l.run(self.result)
111         expected_result = {"latencies0.latency": 4.944, "latencies0.size": 0.00049}
112         self.assertEqual(self.result, expected_result)
113
114     def test_successful_bandwidth_run_sla(self, mock_ssh):
115
116         options = {
117             "test_type": "bandwidth",
118             "size": 500,
119             "benchmark": "rd",
120             "warmup": 0
121         }
122         args = {
123             "options": options,
124             "sla": {"min_bandwidth": 10000}
125         }
126         l = lmbench.Lmbench(args, self.ctx)
127
128         sample_output = '{"size(MB)": 0.262144, "bandwidth(MBps)": 11025.5}'
129         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
130         l.run(self.result)
131         expected_result = jsonutils.loads(sample_output)
132         self.assertEqual(self.result, expected_result)
133
134     def test_unsuccessful_latency_run_sla(self, mock_ssh):
135
136         options = {
137             "test_type": "latency",
138             "stride": 64,
139             "stop_size": 16
140         }
141         args = {
142             "options": options,
143             "sla": {"max_latency": 35}
144         }
145         l = lmbench.Lmbench(args, self.ctx)
146
147         sample_output = '[{"latency": 37.5, "size": 0.00049}]'
148         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
149         self.assertRaises(AssertionError, l.run, self.result)
150
151     def test_unsuccessful_bandwidth_run_sla(self, mock_ssh):
152
153         options = {
154             "test_type": "bandwidth",
155             "size": 500,
156             "benchmark": "rd",
157             "warmup": 0
158         }
159         args = {
160             "options": options,
161             "sla": {"min_bandwidth": 10000}
162         }
163         l = lmbench.Lmbench(args, self.ctx)
164
165         sample_output = '{"size(MB)": 0.262144, "bandwidth(MBps)": 9925.5}'
166         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
167         self.assertRaises(AssertionError, l.run, self.result)
168
169     def test_successful_latency_for_cache_run_sla(self, mock_ssh):
170
171         options = {
172             "test_type": "latency_for_cache",
173             "repetition": 1,
174             "warmup": 0
175         }
176         args = {
177             "options": options,
178             "sla": {"max_latency": 35}
179         }
180         l = lmbench.Lmbench(args, self.ctx)
181
182         sample_output = "{\"L1cache\": 1.6}"
183         mock_ssh.SSH.from_node().execute.return_value = (0, sample_output, '')
184         l.run(self.result)
185         expected_result = jsonutils.loads(sample_output)
186         self.assertEqual(self.result, expected_result)
187
188     def test_unsuccessful_script_error(self, mock_ssh):
189
190         options = {"test_type": "bandwidth"}
191         args = {"options": options}
192         l = lmbench.Lmbench(args, self.ctx)
193
194         mock_ssh.SSH.from_node().execute.return_value = (1, '', 'FOOBAR')
195         self.assertRaises(RuntimeError, l.run, self.result)
196
197
198 def main():
199     unittest.main()
200
201 if __name__ == '__main__':
202     main()