9d518f7a07c815a6b458c2e4210de89bd28d7b9f
[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         host_user = node.get('user', 'ubuntu')
46         ssh_port = node.get('ssh_port', ssh.DEFAULT_PORT)
47         host_ip = node.get('ip', None)
48         host_pwd = node.get('password', 'root')
49         LOG.debug("user:%s, host:%s", host_user, host_ip)
50         self.client = ssh.SSH(host_user, host_ip, password=host_pwd,
51                               port=ssh_port)
52         self.client.wait(timeout=600)
53
54         # copy script to host
55         self.client._put_file_shell(self.target_script, '~/computecapacity.sh')
56
57         self.setup_done = True
58
59     def run(self, result):
60         """execute the benchmark"""
61
62         if not self.setup_done:
63             self.setup()
64
65         cmd = "sudo bash computecapacity.sh"
66
67         LOG.debug("Executing command: %s", cmd)
68         status, stdout, stderr = self.client.execute(cmd)
69         if status:
70             raise RuntimeError(stderr)
71
72         result.update(jsonutils.loads(stdout))