1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB 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 ##############################################################################
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
16 LOG = logging.getLogger(__name__)
19 class Perf(base.Scenario):
20 """Execute perf benchmark in a host
23 events - perf tool software, hardware or tracepoint events
26 default: ['task-clock']
27 load - simulate load on the host by doing IO operations
32 For more info about perf and perf events see https://perf.wiki.kernel.org
35 __scenario_type__ = "Perf"
37 TARGET_SCRIPT = 'perf_benchmark.bash'
39 def __init__(self, scenario_cfg, context_cfg):
40 self.scenario_cfg = scenario_cfg
41 self.context_cfg = context_cfg
42 self.setup_done = False
46 self.target_script = pkg_resources.resource_filename(
47 'yardstick.benchmark.scenarios.compute', Perf.TARGET_SCRIPT)
48 host = self.context_cfg['host']
49 user = host.get('user', 'ubuntu')
50 ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
51 ip = host.get('ip', None)
52 key_filename = host.get('key_filename', '~/.ssh/id_rsa')
54 LOG.info("user:%s, host:%s", user, ip)
55 self.client = ssh.SSH(user, ip, key_filename=key_filename,
57 self.client.wait(timeout=600)
60 self.client.run("cat > ~/perf_benchmark.sh",
61 stdin=open(self.target_script, "rb"))
63 self.setup_done = True
65 def run(self, result):
66 """execute the benchmark"""
68 if not self.setup_done:
71 options = self.scenario_cfg['options']
72 events = options.get('events', ['task-clock'])
76 events_string += event + " "
78 # if run by a duration runner
79 duration_time = self.scenario_cfg["runner"].get("duration", None) \
80 if "runner" in self.scenario_cfg else None
81 # if run by an arithmetic runner
82 arithmetic_time = options.get("duration", None)
84 duration = duration_time
86 duration = arithmetic_time
91 load = "dd if=/dev/urandom of=/dev/null"
93 load = "sleep %d" % duration
95 cmd = "sudo bash perf_benchmark.sh '%s' %d %s" \
96 % (load, duration, events_string)
98 LOG.debug("Executing command: %s", cmd)
99 status, stdout, stderr = self.client.execute(cmd)
102 raise RuntimeError(stdout)
104 result.update(json.loads(stdout))
106 if "sla" in self.scenario_cfg:
107 metric = self.scenario_cfg['sla']['metric']
108 exp_val = self.scenario_cfg['sla']['expected_value']
109 smaller_than_exp = 'smaller_than_expected' \
110 in self.scenario_cfg['sla']
112 if metric not in result:
113 assert False, "Metric (%s) not found." % metric
116 assert result[metric] < exp_val, "%s %d >= %d (sla); " \
117 % (metric, result[metric], exp_val)
119 assert result[metric] >= exp_val, "%s %d < %d (sla); " \
120 % (metric, result[metric], exp_val)
124 """internal test function"""
125 key_filename = pkg_resources.resource_filename('yardstick.resources',
126 'files/yardstick_key')
129 'ip': '10.229.47.137',
131 'key_filename': key_filename
135 logger = logging.getLogger('yardstick')
136 logger.setLevel(logging.DEBUG)
138 options = {'load': True}
139 args = {'options': options}
146 if __name__ == '__main__':