7f0c58de1729f702f0bcf1a2cb90413b549b421f
[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 import pkg_resources
10 import logging
11 import json
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17
18
19 class ComputeCapacity(base.Scenario):
20     """Measure compute capacity and scale.
21
22     This scenario reads hardware specification, including number of cpus,
23     number of cores, number of threads, available memory size and total cache
24     size of a host.
25     """
26     __scenario_type__ = "ComputeCapacity"
27     TARGET_SCRIPT = "computecapacity.bash"
28
29     def __init__(self, scenario_cfg, context_cfg):
30         self.scenario_cfg = scenario_cfg
31         self.context_cfg = context_cfg
32         self.setup_done = False
33
34     def setup(self):
35         """scenario setup"""
36         self.target_script = pkg_resources.resource_filename(
37             "yardstick.benchmark.scenarios.compute",
38             ComputeCapacity.TARGET_SCRIPT)
39
40         nodes = self.context_cfg['nodes']
41         node = nodes.get('host', None)
42         host_user = node.get('user', 'ubuntu')
43         ssh_port = node.get('ssh_port', ssh.DEFAULT_PORT)
44         host_ip = node.get('ip', None)
45         host_pwd = node.get('password', 'root')
46         LOG.debug("user:%s, host:%s", host_user, host_ip)
47         self.client = ssh.SSH(host_user, host_ip, password=host_pwd,
48                               port=ssh_port)
49         self.client.wait(timeout=600)
50
51         # copy script to host
52         self.client._put_file_shell(self.target_script, '~/computecapacity.sh')
53
54         self.setup_done = True
55
56     def run(self, result):
57         """execute the benchmark"""
58
59         if not self.setup_done:
60             self.setup()
61
62         cmd = "sudo bash computecapacity.sh"
63
64         LOG.debug("Executing command: %s", cmd)
65         status, stdout, stderr = self.client.execute(cmd)
66         if status:
67             raise RuntimeError(stderr)
68
69         result.update(json.loads(stdout))