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, context):
40 self.context = context
41 self.setup_done = False
45 self.target_script = pkg_resources.resource_filename(
46 'yardstick.benchmark.scenarios.compute', Perf.TARGET_SCRIPT)
47 user = self.context.get('user', 'ubuntu')
48 host = self.context.get('host', None)
49 key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
51 LOG.info("user:%s, host:%s", user, host)
52 self.client = ssh.SSH(user, host, key_filename=key_filename)
53 self.client.wait(timeout=600)
56 self.client.run("cat > ~/perf_benchmark.sh",
57 stdin=open(self.target_script, "rb"))
59 self.setup_done = True
62 """execute the benchmark"""
64 if not self.setup_done:
67 options = args['options']
68 events = options.get('events', ['task-clock'])
72 events_string += event + " "
74 # if run by a duration runner
75 duration_time = self.context.get("duration", None)
76 # if run by an arithmetic runner
77 arithmetic_time = options.get("duration", None)
79 duration = duration_time
81 duration = arithmetic_time
86 load = "dd if=/dev/urandom of=/dev/null"
88 load = "sleep %d" % duration
90 cmd = "sudo bash perf_benchmark.sh '%s' %d %s" \
91 % (load, duration, events_string)
93 LOG.debug("Executing command: %s", cmd)
94 status, stdout, stderr = self.client.execute(cmd)
97 raise RuntimeError(stdout)
99 output = json.loads(stdout)
102 metric = args['sla']['metric']
103 exp_val = args['sla']['expected_value']
104 smaller_than_exp = 'smaller_than_expected' in args['sla']
106 if metric not in output:
107 assert False, "Metric (%s) not found." % metric
110 assert output[metric] < exp_val, "%s %d >= %d (sla)" \
111 % (metric, output[metric], exp_val)
113 assert output[metric] >= exp_val, "%s %d < %d (sla)" \
114 % (metric, output[metric], exp_val)
119 """internal test function"""
120 key_filename = pkg_resources.resource_filename('yardstick.resources',
121 'files/yardstick_key')
122 ctx = {'host': '172.16.0.137',
124 'key_filename': key_filename
127 logger = logging.getLogger('yardstick')
128 logger.setLevel(logging.DEBUG)
132 options = {'load': True}
133 args = {'options': options}
138 if __name__ == '__main__':