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