Heat context code refactor part 2
[yardstick.git] / yardstick / benchmark / scenarios / compute / lmbench.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
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 Lmbench(base.Scenario):
20     """Execute lmbench memory read latency benchmark in a host
21
22     Parameters
23         stride - number of locations in memory between starts of array elements
24             type:       int
25             unit:       bytes
26             default:    128
27         stop_size - maximum array size to test (minimum value is 0.000512)
28             type:       int
29             unit:       megabytes
30             default:    16
31
32     Results are accurate to the ~2-5 nanosecond range.
33     """
34     __scenario_type__ = "Lmbench"
35
36     TARGET_SCRIPT = "lmbench_benchmark.bash"
37
38     def __init__(self, scenario_cfg, context_cfg):
39         self.scenario_cfg = scenario_cfg
40         self.context_cfg = context_cfg
41         self.setup_done = False
42
43     def setup(self):
44         """scenario setup"""
45         self.target_script = pkg_resources.resource_filename(
46             "yardstick.benchmark.scenarios.compute",
47             Lmbench.TARGET_SCRIPT)
48         host = self.context_cfg["host"]
49         user = host.get("user", "ubuntu")
50         ip = host.get("ip", None)
51         key_filename = host.get('key_filename', "~/.ssh/id_rsa")
52
53         LOG.info("user:%s, host:%s", user, ip)
54         self.client = ssh.SSH(user, ip, key_filename=key_filename)
55         self.client.wait(timeout=600)
56
57         # copy script to host
58         self.client.run("cat > ~/lmbench.sh",
59                         stdin=open(self.target_script, 'rb'))
60
61         self.setup_done = True
62
63     def run(self, result):
64         """execute the benchmark"""
65
66         if not self.setup_done:
67             self.setup()
68
69         options = self.scenario_cfg['options']
70         stride = options.get('stride', 128)
71         stop_size = options.get('stop_size', 16)
72
73         cmd = "sudo bash lmbench.sh %d %d" % (stop_size, stride)
74         LOG.debug("Executing command: %s", cmd)
75         status, stdout, stderr = self.client.execute(cmd)
76
77         if status:
78             raise RuntimeError(stderr)
79
80         result.update({"latencies": json.loads(stdout)})
81         if "sla" in self.scenario_cfg:
82             sla_error = ""
83             sla_max_latency = int(self.scenario_cfg['sla']['max_latency'])
84             for t_latency in result:
85                 latency = t_latency['latency']
86                 if latency > sla_max_latency:
87                     sla_error += "latency %f > sla:max_latency(%f); " \
88                         % (latency, sla_max_latency)
89             assert sla_error == "", sla_error
90
91
92 def _test():
93     """internal test function"""
94     key_filename = pkg_resources.resource_filename('yardstick.resources',
95                                                    'files/yardstick_key')
96     ctx = {
97         'host': {
98             'ip': '10.229.47.137',
99             'user': 'root',
100             'key_filename': key_filename
101         }
102     }
103
104     logger = logging.getLogger('yardstick')
105     logger.setLevel(logging.DEBUG)
106
107     options = {'stride': 128, 'stop_size': 16}
108     args = {'options': options}
109     result = {}
110
111     p = Lmbench(args, ctx)
112     p.run(result)
113     print result
114
115 if __name__ == '__main__':
116     _test()