aboutsummaryrefslogtreecommitdiffstats
path: root/yardstick/benchmark/scenarios/storage/storagecapacity.py
blob: c437f22c0ef3f02f6ca618b89c3ec3947c39c435 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
##############################################################################
# Copyright (c) 2016 Huawei Technologies Co.,Ltd and other.
#
# All rights reserved. This program and the accompanying materials
# are made available under the terms of the Apache License, Version 2.0
# which accompanies this distribution, and is available at
# http://www.apache.org/licenses/LICENSE-2.0
##############################################################################
from __future__ import absolute_import

import logging

import pkg_resources
from oslo_serialization import jsonutils
from six.moves import range

import yardstick.ssh as ssh
from yardstick.benchmark.scenarios import base

LOG = logging.getLogger(__name__)


class StorageCapacity(base.Scenario):
    """Measure storage capacity and scale.

    Parameters:
        test_type - specified whether to measure.
        valid test type are disk_size, block_size, disk_utilization
            type: string
            unit: na
            default: "disk_size"
        interval - specified how ofter to stat disk utilization
            type: int
            unit: seconds
            default: 1
        count - specified how many times to stat disk utilization
            type: int
            unit: na
            default: 15

    This scenario reads hardware specification,
    disk size, block size and disk utilization.
    """
    __scenario_type__ = "StorageCapacity"
    TARGET_SCRIPT = "storagecapacity.bash"

    def __init__(self, scenario_cfg, context_cfg):
        self.scenario_cfg = scenario_cfg
        self.context_cfg = context_cfg
        self.setup_done = False

    def setup(self):
        """scenario setup"""
        self.target_script = pkg_resources.resource_filename(
            "yardstick.benchmark.scenarios.storage",
            StorageCapacity.TARGET_SCRIPT)
        host = self.context_cfg['host']
        if host is None:
            raise RuntimeError('No right node.Please check the configuration')
        host_user = host.get('user', 'ubuntu')
        ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
        host_ip = host.get('ip', None)
        host_pwd = host.get('password', 'root')
        LOG.debug("user:%s, host:%s", host_user, host_ip)

        self.client = ssh.SSH(host_user, host_ip, password=host_pwd,
                              port=ssh_port)
        self.client.wait(timeout=600)

        # copy script to host
        self.client._put_file_shell(self.target_script, '~/storagecapacity.sh')

        self.setup_done = True

    def _get_disk_utilization(self):
        """Get disk utilization using iostat."""
        options = self.scenario_cfg["options"]
        interval = options.get('interval', 1)
        count = options.get('count', 15)

        cmd = "sudo iostat -dx %d %d | awk 'NF==14 && \
               $1 !~ /Device/ {print $1,$14}'" % (interval, count)

        LOG.debug("Executing command: %s", cmd)
        status, stdout, stderr = self.client.execute(cmd)
        if status:
            raise RuntimeError(stderr)

        device_name_arr = []
        min_util_arr = []
        max_util_arr = []
        avg_util_arr = []
        for row in stdout.split('\n'):
            kv = row.split(' ')
            if len(kv) != 2:
                continue
            name = kv[0]
            util = float(kv[1])
            if name not in device_name_arr:
                device_name_arr.append(name)
                min_util_arr.append(util)
                max_util_arr.append(util)
                avg_util_arr.append(util)
            else:
                i = device_name_arr.index(name)
                min_util_arr[i] = min_util_arr[i] \
                    if min_util_arr[i] < util else util
                max_util_arr[i] = max_util_arr[i] \
                    if max_util_arr[i] > util else util
                avg_util_arr[i] += util
        r = {}
        for i in range(len(device_name_arr)):
            r[device_name_arr[i]] = {"min_util": min_util_arr[i],
                                     "max_util": max_util_arr[i],
                                     "avg_util": avg_util_arr[i] / count}
        return r

    def run(self, result):
        """execute the benchmark"""

        if not self.setup_done:
            self.setup()

        options = self.scenario_cfg["options"]
        test_type = options.get('test_type', 'disk_size')

        if test_type == "disk_utilization":
            r = self._get_disk_utilization()
            result.update(r)
        else:
            cmd = "sudo bash storagecapacity.sh " + test_type

            LOG.debug("Executing command: %s", cmd)
            status, stdout, stderr = self.client.execute(cmd)
            if status:
                raise RuntimeError(stderr)

            result.update(jsonutils.loads(stdout))