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