Merge "ubuntu-server-cloudimg-modify.sh: shellcheck fixes"
[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         host_user = host.get('user', 'ubuntu')
61         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
62         host_ip = host.get('ip', None)
63         host_pwd = host.get('password', 'root')
64         LOG.debug("user:%s, host:%s", host_user, host_ip)
65
66         self.client = ssh.SSH(host_user, host_ip, password=host_pwd,
67                               port=ssh_port)
68         self.client.wait(timeout=600)
69
70         # copy script to host
71         self.client._put_file_shell(self.target_script, '~/storagecapacity.sh')
72
73         self.setup_done = True
74
75     def _get_disk_utilization(self):
76         """Get disk utilization using iostat."""
77         options = self.scenario_cfg["options"]
78         interval = options.get('interval', 1)
79         count = options.get('count', 15)
80
81         cmd = "sudo iostat -dx %d %d | awk 'NF==14 && \
82                $1 !~ /Device/ {print $1,$14}'" % (interval, count)
83
84         LOG.debug("Executing command: %s", cmd)
85         status, stdout, stderr = self.client.execute(cmd)
86         if status:
87             raise RuntimeError(stderr)
88
89         device_name_arr = []
90         min_util_arr = []
91         max_util_arr = []
92         avg_util_arr = []
93         for row in stdout.split('\n'):
94             kv = row.split(' ')
95             if len(kv) != 2:
96                 continue
97             name = kv[0]
98             util = float(kv[1])
99             if name not in device_name_arr:
100                 device_name_arr.append(name)
101                 min_util_arr.append(util)
102                 max_util_arr.append(util)
103                 avg_util_arr.append(util)
104             else:
105                 i = device_name_arr.index(name)
106                 min_util_arr[i] = min_util_arr[i] \
107                     if min_util_arr[i] < util else util
108                 max_util_arr[i] = max_util_arr[i] \
109                     if max_util_arr[i] > util else util
110                 avg_util_arr[i] += util
111         r = {}
112         for i in range(len(device_name_arr)):
113             r[device_name_arr[i]] = {"min_util": min_util_arr[i],
114                                      "max_util": max_util_arr[i],
115                                      "avg_util": avg_util_arr[i] / count}
116         return r
117
118     def run(self, result):
119         """execute the benchmark"""
120
121         if not self.setup_done:
122             self.setup()
123
124         options = self.scenario_cfg["options"]
125         test_type = options.get('test_type', 'disk_size')
126
127         if test_type == "disk_utilization":
128             r = self._get_disk_utilization()
129             result.update(r)
130         else:
131             cmd = "sudo bash storagecapacity.sh " + test_type
132
133             LOG.debug("Executing command: %s", cmd)
134             status, stdout, stderr = self.client.execute(cmd)
135             if status:
136                 raise RuntimeError(stderr)
137
138             result.update(jsonutils.loads(stdout))