b22be29c95385a2ec04b9b3267b9e17ac9c5350b
[yardstick.git] / yardstick / benchmark / scenarios / compute / unixbench.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and other.
3 #
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 ##############################################################################
9 import pkg_resources
10 import logging
11 import json
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17
18
19 class Unixbench(base.Scenario):
20     """Execute Unixbench cpu benchmark in a host
21     The Run script takes a number of options which you can use to customise a
22     test, and you can specify the names of the tests to run.  The full usage
23     is:
24
25     Run [ -q | -v ] [-i <n> ] [-c <n> [-c <n> ...]] [test ...]
26
27     -i <count>    Run <count> iterations for each test -- slower tests
28                 use <count> / 3, but at least 1.  Defaults to 10 (3 for
29                 slow tests).
30     -c <n>        Run <n> copies of each test in parallel.
31
32     Parameters for setting unixbench
33         run_mode - Run in quiet mode or verbose mode
34             type:       string
35             unit:       None
36             default:    None
37         test_type - The available tests are organised into categories;
38             type:       string
39             unit:       None
40             default:    None
41         iterations - Run <count> iterations for each test -- slower tests
42         use <count> / 3, but at least 1.  Defaults to 10 (3 for slow tests).
43             type:       int
44             unit:       None
45             default:    None
46         copies - Run <n> copies of each test in parallel.
47             type:       int
48             unit:       None
49             default:    None
50
51     more info https://github.com/kdlucas/byte-unixbench/blob/master/UnixBench
52     """
53     __scenario_type__ = "UnixBench"
54
55     TARGET_SCRIPT = "unixbench_benchmark.bash"
56
57     def __init__(self, scenario_cfg, context_cfg):
58         self.scenario_cfg = scenario_cfg
59         self.context_cfg = context_cfg
60         self.setup_done = False
61
62     def setup(self):
63         """scenario setup"""
64         self.target_script = pkg_resources.resource_filename(
65             "yardstick.benchmark.scenarios.compute",
66             Unixbench.TARGET_SCRIPT)
67
68         host = self.context_cfg["host"]
69         user = host.get("user", "ubuntu")
70         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
71         ip = host.get("ip", None)
72         key_filename = host.get('key_filename', "~/.ssh/id_rsa")
73
74         LOG.info("user:%s, host:%s", user, ip)
75         self.client = ssh.SSH(user, ip, key_filename=key_filename,
76                               port=ssh_port)
77         self.client.wait(timeout=600)
78
79         # copy scripts to host
80         self.client._put_file_shell(
81             self.target_script, '~/unixbench_benchmark.sh')
82
83         self.setup_done = True
84
85     def run(self, result):
86         """execute the benchmark"""
87
88         if not self.setup_done:
89             self.setup()
90
91         options = self.scenario_cfg["options"]
92
93         run_mode = options.get("run_mode", None)
94         LOG.debug("Executing run_mode: %s", run_mode)
95         cmd_args = ""
96         if run_mode == "quiet":
97             cmd_args = "-q"
98         elif run_mode == "verbose":
99             cmd_args = "-v"
100
101         option_pair_list = [("iterations", "-i"),
102                             ("copies", "-c")]
103         for option_pair in option_pair_list:
104             if option_pair[0] in options:
105                 cmd_args += " %s %s " % (option_pair[1],
106                                          options[option_pair[0]])
107
108         test_type = options.get("test_type", None)
109         if test_type is not None:
110             cmd_args += " %s " % (test_type)
111
112         cmd = "sudo bash unixbench_benchmark.sh %s" % (cmd_args)
113         LOG.debug("Executing command: %s", cmd)
114         status, stdout, stderr = self.client.execute(cmd)
115         if status:
116             raise RuntimeError(stderr)
117
118         result.update(json.loads(stdout))
119
120         if "sla" in self.scenario_cfg:
121             sla_error = ""
122             for t, score in result.items():
123                 if t not in self.scenario_cfg['sla']:
124                     continue
125                 sla_score = float(self.scenario_cfg['sla'][t])
126                 score = float(score)
127                 if score < sla_score:
128                     sla_error += "%s score %f < sla:%s_score(%f); " % \
129                         (t, score, t, sla_score)
130             assert sla_error == "", sla_error
131
132
133 def _test():  # pragma: no cover
134     """internal test function"""
135     key_filename = pkg_resources.resource_filename('yardstick.resources',
136                                                    'files/yardstick_key')
137     ctx = {
138         'host': {
139             'ip': '10.229.47.137',
140             'user': 'root',
141             'key_filename': key_filename
142         }
143     }
144
145     options = {
146         'test_type': 'dhrystone',
147         'run_mode': 'verbose'
148     }
149
150     args = {'options': options}
151     result = {}
152
153     p = Unixbench(args, ctx)
154     p.run(result)
155     print result
156
157
158 if __name__ == '__main__':
159     _test()