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