standardize ssh auth
[yardstick.git] / yardstick / benchmark / scenarios / storage / storagecapacity.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and other.
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 from six.moves import range
16
17 import yardstick.ssh as ssh
18 from yardstick.benchmark.scenarios import base
19
20 LOG = logging.getLogger(__name__)
21
22
23 class StorageCapacity(base.Scenario):
24     """Measure storage capacity and scale.
25
26     Parameters:
27         test_type - specified whether to measure.
28         valid test type are disk_size, block_size, disk_utilization
29             type: string
30             unit: na
31             default: "disk_size"
32         interval - specified how ofter to stat disk utilization
33             type: int
34             unit: seconds
35             default: 1
36         count - specified how many times to stat disk utilization
37             type: int
38             unit: na
39             default: 15
40
41     This scenario reads hardware specification,
42     disk size, block size and disk utilization.
43     """
44     __scenario_type__ = "StorageCapacity"
45     TARGET_SCRIPT = "storagecapacity.bash"
46
47     def __init__(self, scenario_cfg, context_cfg):
48         self.scenario_cfg = scenario_cfg
49         self.context_cfg = context_cfg
50         self.setup_done = False
51
52     def setup(self):
53         """scenario setup"""
54         self.target_script = pkg_resources.resource_filename(
55             "yardstick.benchmark.scenarios.storage",
56             StorageCapacity.TARGET_SCRIPT)
57         host = self.context_cfg['host']
58         if host is None:
59             raise RuntimeError('No right node.Please check the configuration')
60
61         self.client = ssh.SSH.from_node(host, defaults={
62             "user": "ubuntu", "password": "root"
63         })
64         self.client.wait(timeout=600)
65
66         # copy script to host
67         self.client._put_file_shell(self.target_script, '~/storagecapacity.sh')
68
69         self.setup_done = True
70
71     def _get_disk_utilization(self):
72         """Get disk utilization using iostat."""
73         options = self.scenario_cfg["options"]
74         interval = options.get('interval', 1)
75         count = options.get('count', 15)
76
77         cmd = "sudo iostat -dx %d %d | awk 'NF==14 && \
78                $1 !~ /Device/ {print $1,$14}'" % (interval, count)
79
80         LOG.debug("Executing command: %s", cmd)
81         status, stdout, stderr = self.client.execute(cmd)
82         if status:
83             raise RuntimeError(stderr)
84
85         device_name_arr = []
86         min_util_arr = []
87         max_util_arr = []
88         avg_util_arr = []
89         for row in stdout.split('\n'):
90             kv = row.split(' ')
91             if len(kv) != 2:
92                 continue
93             name = kv[0]
94             util = float(kv[1])
95             if name not in device_name_arr:
96                 device_name_arr.append(name)
97                 min_util_arr.append(util)
98                 max_util_arr.append(util)
99                 avg_util_arr.append(util)
100             else:
101                 i = device_name_arr.index(name)
102                 min_util_arr[i] = min_util_arr[i] \
103                     if min_util_arr[i] < util else util
104                 max_util_arr[i] = max_util_arr[i] \
105                     if max_util_arr[i] > util else util
106                 avg_util_arr[i] += util
107         r = {}
108         for i in range(len(device_name_arr)):
109             r[device_name_arr[i]] = {"min_util": min_util_arr[i],
110                                      "max_util": max_util_arr[i],
111                                      "avg_util": avg_util_arr[i] / count}
112         return r
113
114     def run(self, result):
115         """execute the benchmark"""
116
117         if not self.setup_done:
118             self.setup()
119
120         options = self.scenario_cfg["options"]
121         test_type = options.get('test_type', 'disk_size')
122
123         if test_type == "disk_utilization":
124             r = self._get_disk_utilization()
125             result.update(r)
126         else:
127             cmd = "sudo bash storagecapacity.sh " + test_type
128
129             LOG.debug("Executing command: %s", cmd)
130             status, stdout, stderr = self.client.execute(cmd)
131             if status:
132                 raise RuntimeError(stderr)
133
134             result.update(jsonutils.loads(stdout))