"""Processor statistics and system load."""
+from __future__ import absolute_import
+
import logging
-import time
import re
-import yardstick.ssh as ssh
+import time
-from yardstick.benchmark.scenarios import base
+from six.moves import map, zip
+import yardstick.ssh as ssh
+from yardstick.benchmark.scenarios import base
LOG = logging.getLogger(__name__)
on the Linux host.
Parameters
- interval - Time interval to measure CPU usage. A value of 0
- indicates that processors statistics are to be
- reported for the time since system startup (boot)
+ interval - Time interval to measure CPU usage.
type: [int]
unit: seconds
- default: 0
+ default: 1
+
+ count (for mpstat only) - Number of CPU usage measurment.
+
+ type: [int]
+ unit: N/A
+ default: 1
"""
self.context_cfg = context_cfg
self.setup_done = False
self.has_mpstat = False
+ self.has_count = False
def setup(self):
"""Scenario setup."""
host = self.context_cfg['host']
user = host.get('user', 'ubuntu')
ip = host.get('ip', None)
+ ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
key_filename = host.get('key_filename', '~/.ssh/id_rsa')
LOG.info("user:%s, host:%s", user, ip)
- self.client = ssh.SSH(user, ip, key_filename=key_filename)
+ self.client = ssh.SSH(user, ip, key_filename=key_filename,
+ port=ssh_port)
self.client.wait(timeout=600)
# Check if mpstat prog is installed
LOG.info("MPSTAT is installed")
self.has_mpstat = True
- if 'options' in self.scenario_cfg:
- self.interval = self.scenario_cfg['options'].get("interval", 0)
+ options = self.scenario_cfg['options']
+ self.interval = options.get("interval", 1)
+ if 'count' in options:
+ self.count = options.get("count", 1)
+ self.has_count = True
else:
- self.interval = 0
+ self.has_count = False
self.setup_done = True
def _execute_command(self, cmd):
"""Execute a command on server."""
- LOG.info("Executing: %s" % cmd)
+ LOG.info("Executing: %s", cmd)
status, stdout, stderr = self.client.execute(cmd)
if status != 0:
raise RuntimeError("Failed executing command: ",
def _get_cpu_usage_mpstat(self):
"""Get processor usage using mpstat."""
- if self.interval > 0:
- cmd = "mpstat -P ON %s 1" % self.interval
+ if self.interval > 0 and self.has_count:
+ cmd = "mpstat -P ON %s %s" % (self.interval, self.count)
else:
- cmd = "mpstat -P ON"
+ cmd = "mpstat -P ON %s 1" % self.interval
result = self._execute_command(cmd)
fields = []
- mpstat = {}
+ maximum = {}
+ minimum = {}
+ average = {}
time_marker = re.compile("^([0-9]+):([0-9]+):([0-9]+)$")
ampm_marker = re.compile("(AM|PM)$")
line = row.split()
if line and re.match(time_marker, line[0]):
-
if re.match(ampm_marker, line[1]):
del line[:2]
else:
cpu = 'cpu' if line[0] == 'all' else 'cpu' + line[0]
values = line[1:]
if values and len(values) == len(fields):
- mpstat[cpu] = dict(zip(fields, values))
+ temp_dict = dict(list(zip(fields, values)))
+ if cpu not in maximum:
+ maximum[cpu] = temp_dict
+ else:
+ for item in temp_dict:
+ if float(maximum[cpu][item]) <\
+ float(temp_dict[item]):
+ maximum[cpu][item] = temp_dict[item]
+
+ if cpu not in minimum:
+ minimum[cpu] = temp_dict
+ else:
+ for item in temp_dict:
+ if float(minimum[cpu][item]) >\
+ float(temp_dict[item]):
+ minimum[cpu][item] = temp_dict[item]
else:
raise RuntimeError("mpstat: parse error", fields, line)
- return {'mpstat': mpstat}
+ elif line and line[0] == 'Average:':
+ del line[:1]
+ if line[0] == 'CPU':
+ # header fields
+ fields = line[1:]
+ if len(fields) != CPULoad.MPSTAT_FIELD_SIZE:
+ raise RuntimeError("mpstat average: unexpected field\
+ size", fields)
+ else:
+ # value fields
+ cpu = 'cpu' if line[0] == 'all' else 'cpu' + line[0]
+ values = line[1:]
+ if values and len(values) == len(fields):
+ average[cpu] = dict(list(zip(fields, values)))
+ else:
+ raise RuntimeError("mpstat average: parse error",
+ fields, line)
+
+ return {'mpstat_maximun': maximum, 'mpstat_minimum': minimum,
+ 'mpstat_average': average}
def _get_cpu_usage(self):
"""Get processor usage from /proc/stat."""
cpu = cur_list[0]
- cur_stats = map(int, cur_list[1:])
+ cur_stats = list(map(int, cur_list[1:]))
if self.interval > 0:
- prev_stats = map(int, prev_list[1:])
+ prev_stats = list(map(int, prev_list[1:]))
else:
prev_stats = [0] * len(cur_stats)
else:
return "%.2f" % (100.0 * (x - y) / samples)
- load = map(_percent, cur_stats, prev_stats)
+ load = list(map(_percent, cur_stats, prev_stats))
- mpstat[cpu] = dict(zip(fields, load))
+ mpstat[cpu] = dict(list(zip(fields, load)))
return {'mpstat': mpstat}
# p = CPULoad(args, ctx)
# p.run(result)
# import json
-# print json.dumps(result)
+# print(oslo_serialization.jsonutils.dump_as_bytes(result))
# if __name__ == '__main__':
# _test()