1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd and others.
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 ##############################################################################
10 """Memory load and statistics."""
12 from __future__ import absolute_import
14 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
17 from six.moves import zip
19 LOG = logging.getLogger(__name__)
22 class MEMLoad(base.Scenario):
23 """Collect memory statistics and system load.
25 This scenario reads memory usage statistics on a Linux host.
27 memory usage statistics are read using the utility 'free'.
30 interval - Time interval to measure memory usage.
31 Continuously display the result delay interval seconds apart.
36 count - specifies a # of measurments for each test
41 __scenario_type__ = "MEMORYload"
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
51 host = self.context_cfg['host']
52 user = host.get('user', 'ubuntu')
53 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
54 ip = host.get('ip', None)
55 key_filename = host.get('key_filename', '~/.ssh/id_rsa')
57 LOG.info("user:%s, host:%s", user, ip)
58 self.client = ssh.SSH(user, ip, key_filename=key_filename,
60 self.client.wait(timeout=600)
62 self.setup_done = True
64 def _execute_command(self, cmd):
65 """Execute a command on server."""
66 LOG.info("Executing: %s", cmd)
67 status, stdout, stderr = self.client.execute(cmd)
69 raise RuntimeError("Failed executing command: ",
73 def _filtrate_result(self, result):
77 average = {'total': 0, 'used': 0, 'free': 0, 'buff/cache': 0,
79 maximum = {'total': 0, 'used': 0, 'free': 0, 'buff/cache': 0,
82 for row in result.split('\n'):
85 if line and line[0] == 'total':
88 elif line and line[0] == 'Mem:':
89 memory = 'memory' + str(ite)
92 if values and len(values) == len(fields):
93 free[memory] = dict(list(zip(fields, values)))
97 average[item] += int(free[entry][item])
100 if int(free[entry][item]) > maximum[item]:
101 maximum[item] = int(free[entry][item])
104 average[item] = average[item] / len(free)
106 return {'free': free, 'average': average, 'max': maximum}
108 def _get_mem_usage(self):
109 """Get memory usage using free."""
110 options = self.scenario_cfg['options']
111 interval = options.get("interval", 1)
112 count = options.get("count", 1)
114 cmd = "free -c '%s' -s '%s'" % (count, interval)
116 result = self._execute_command(cmd)
117 filtrated_result = self._filtrate_result(result)
119 return filtrated_result
121 def run(self, result):
122 """Read processor statistics."""
123 if not self.setup_done:
126 result.update(self._get_mem_usage())