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."""
13 import yardstick.ssh as ssh
15 from yardstick.benchmark.scenarios import base
17 LOG = logging.getLogger(__name__)
20 class MEMLoad(base.Scenario):
21 """Collect memory statistics and system load.
23 This scenario reads memory usage statistics on a Linux host.
25 memory usage statistics are read using the utility 'free'.
28 interval - Time interval to measure memory usage.
29 Continuously display the result delay interval seconds apart.
34 count - specifies a # of measurments for each test
39 __scenario_type__ = "MEMORYload"
41 def __init__(self, scenario_cfg, context_cfg):
42 """Scenario construction."""
43 self.scenario_cfg = scenario_cfg
44 self.context_cfg = context_cfg
45 self.setup_done = False
49 host = self.context_cfg['host']
50 user = host.get('user', 'ubuntu')
51 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
52 ip = host.get('ip', None)
53 key_filename = host.get('key_filename', '~/.ssh/id_rsa')
55 LOG.info("user:%s, host:%s", user, ip)
56 self.client = ssh.SSH(user, ip, key_filename=key_filename,
58 self.client.wait(timeout=600)
60 self.setup_done = True
62 def _execute_command(self, cmd):
63 """Execute a command on server."""
64 LOG.info("Executing: %s", cmd)
65 status, stdout, stderr = self.client.execute(cmd)
67 raise RuntimeError("Failed executing command: ",
71 def _filtrate_result(self, result):
75 average = {'total': 0, 'used': 0, 'free': 0, 'cached': 0, 'shared': 0,
77 maximum = {'total': 0, 'used': 0, 'free': 0, 'cached': 0, 'shared': 0,
80 for row in result.split('\n'):
83 if line and line[0] == 'total':
86 elif line and line[0] == 'Mem:':
87 memory = 'memory' + str(ite)
90 if values and len(values) == len(fields):
91 free[memory] = dict(zip(fields, values))
95 average[item] += int(free[entry][item])
98 if int(free[entry][item]) > maximum[item]:
99 maximum[item] = int(free[entry][item])
102 average[item] = average[item] / len(free)
104 return {'free': free, 'average': average, 'max': maximum}
106 def _get_mem_usage(self):
107 """Get memory usage using free."""
108 options = self.scenario_cfg['options']
109 interval = options.get("interval", 1)
110 count = options.get("count", 1)
112 cmd = "free -s %s -c %s" % (interval, count)
114 result = self._execute_command(cmd)
115 filtrated_result = self._filtrate_result(result)
117 return filtrated_result
119 def run(self, result):
120 """Read processor statistics."""
121 if not self.setup_done:
124 result.update(self._get_mem_usage())