Merge "Add support for restarting a service"
[yardstick.git] / yardstick / benchmark / scenarios / compute / computecapacity.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9 from __future__ import absolute_import
10
11 import logging
12
13 import pkg_resources
14 from oslo_serialization import jsonutils
15
16 import yardstick.ssh as ssh
17 from yardstick.benchmark.scenarios import base
18
19 LOG = logging.getLogger(__name__)
20
21
22 class ComputeCapacity(base.Scenario):
23     """Measure compute capacity and scale.
24
25     This scenario reads hardware specification, including number of cpus,
26     number of cores, number of threads, available memory size and total cache
27     size of a host.
28     """
29     __scenario_type__ = "ComputeCapacity"
30     TARGET_SCRIPT = "computecapacity.bash"
31
32     def __init__(self, scenario_cfg, context_cfg):
33         self.scenario_cfg = scenario_cfg
34         self.context_cfg = context_cfg
35         self.setup_done = False
36
37     def setup(self):
38         """scenario setup"""
39         self.target_script = pkg_resources.resource_filename(
40             "yardstick.benchmark.scenarios.compute",
41             ComputeCapacity.TARGET_SCRIPT)
42
43         nodes = self.context_cfg['nodes']
44         node = nodes.get('host', None)
45         self.client = ssh.SSH.from_node(node, defaults={
46             "user": "ubuntu", "password": "root"
47         })
48         self.client.wait(timeout=600)
49
50         # copy script to host
51         self.client._put_file_shell(self.target_script, '~/computecapacity.sh')
52
53         self.setup_done = True
54
55     def run(self, result):
56         """execute the benchmark"""
57
58         if not self.setup_done:
59             self.setup()
60
61         cmd = "sudo bash computecapacity.sh"
62
63         LOG.debug("Executing command: %s", cmd)
64         status, stdout, stderr = self.client.execute(cmd)
65         if status:
66             raise RuntimeError(stderr)
67
68         result.update(jsonutils.loads(stdout))