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']
53 self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
54 self.client.wait(timeout=600)
56 self.setup_done = True
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)
63 raise RuntimeError("Failed executing command: ",
67 def _filtrate_result(self, result):
71 average = {'total': 0, 'used': 0, 'free': 0, 'buff/cache': 0,
73 maximum = {'total': 0, 'used': 0, 'free': 0, 'buff/cache': 0,
76 for row in result.split('\n'):
79 if line and line[0] == 'total':
82 elif line and line[0] == 'Mem:':
83 memory = 'memory' + str(ite)
86 if values and len(values) == len(fields):
87 free[memory] = dict(list(zip(fields, values)))
91 average[item] += int(free[entry][item])
94 if int(free[entry][item]) > maximum[item]:
95 maximum[item] = int(free[entry][item])
98 average[item] = average[item] / len(free)
100 return {'free': free, 'average': average, 'max': maximum}
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)
108 cmd = "free -c '%s' -s '%s'" % (count, interval)
110 result = self._execute_command(cmd)
111 filtrated_result = self._filtrate_result(result)
113 return filtrated_result
115 def run(self, result):
116 """Read processor statistics."""
117 if not self.setup_done:
120 result.update(self._get_mem_usage())