standardize ssh auth
[yardstick.git] / yardstick / benchmark / scenarios / compute / memload.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
10 """Memory load and statistics."""
11
12 from __future__ import absolute_import
13 import logging
14 import yardstick.ssh as ssh
15
16 from yardstick.benchmark.scenarios import base
17 from six.moves import zip
18
19 LOG = logging.getLogger(__name__)
20
21
22 class MEMLoad(base.Scenario):
23     """Collect memory statistics and system load.
24
25     This scenario reads memory usage statistics on a Linux host.
26
27     memory usage statistics are read using the utility 'free'.
28
29     Parameters
30         interval - Time interval to measure memory usage.
31         Continuously display the result delay interval seconds apart.
32         type:       [int]
33         unit:       seconds
34         default:    1
35
36         count - specifies a # of measurments for each test
37         type:       [int]
38         unit:       N/A
39         default:    1
40     """
41     __scenario_type__ = "MEMORYload"
42
43     def __init__(self, scenario_cfg, context_cfg):
44         """Scenario construction."""
45         self.scenario_cfg = scenario_cfg
46         self.context_cfg = context_cfg
47         self.setup_done = False
48
49     def setup(self):
50         """Scenario setup."""
51         host = self.context_cfg['host']
52
53         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
54         self.client.wait(timeout=600)
55
56         self.setup_done = True
57
58     def _execute_command(self, cmd):
59         """Execute a command on server."""
60         LOG.info("Executing: %s", cmd)
61         status, stdout, stderr = self.client.execute(cmd)
62         if status:
63             raise RuntimeError("Failed executing command: ",
64                                cmd, stderr)
65         return stdout
66
67     def _filtrate_result(self, result):
68         fields = []
69         free = {}
70         ite = 0
71         average = {'total': 0, 'used': 0, 'free': 0, 'buff/cache': 0,
72                    'shared': 0}
73         maximum = {'total': 0, 'used': 0, 'free': 0, 'buff/cache': 0,
74                    'shared': 0}
75
76         for row in result.split('\n'):
77             line = row.split()
78
79             if line and line[0] == 'total':
80                 # header fields
81                 fields = line[:]
82             elif line and line[0] == 'Mem:':
83                 memory = 'memory' + str(ite)
84                 ite += 1
85                 values = line[1:]
86                 if values and len(values) == len(fields):
87                     free[memory] = dict(list(zip(fields, values)))
88
89         for entry in free:
90             for item in average:
91                 average[item] += int(free[entry][item])
92
93             for item in maximum:
94                 if int(free[entry][item]) > maximum[item]:
95                     maximum[item] = int(free[entry][item])
96
97         for item in average:
98             average[item] = average[item] / len(free)
99
100         return {'free': free, 'average': average, 'max': maximum}
101
102     def _get_mem_usage(self):
103         """Get memory usage using free."""
104         options = self.scenario_cfg['options']
105         interval = options.get("interval", 1)
106         count = options.get("count", 1)
107
108         cmd = "free -c '%s' -s '%s'" % (count, interval)
109
110         result = self._execute_command(cmd)
111         filtrated_result = self._filtrate_result(result)
112
113         return filtrated_result
114
115     def run(self, result):
116         """Read processor statistics."""
117         if not self.setup_done:
118             self.setup()
119
120         result.update(self._get_mem_usage())