3677391286a5747cecd2b1f7800a0a159d6a2d2f
[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, context):
39         self.context = context
40         self.setup_done = False
41
42     def setup(self):
43         """scenario setup"""
44         self.target_script = pkg_resources.resource_filename(
45             "yardstick.benchmark.scenarios.compute",
46             Lmbench.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")
50
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)
54
55         # copy script to host
56         self.client.run("cat > ~/lmbench.sh",
57                         stdin=open(self.target_script, 'rb'))
58
59         self.setup_done = True
60
61     def run(self, args):
62         """execute the benchmark"""
63
64         if not self.setup_done:
65             self.setup()
66
67         options = args['options']
68         stride = options.get('stride', 128)
69         stop_size = options.get('stop_size', 16)
70
71         cmd = "sudo bash lmbench.sh %d %d" % (stop_size, stride)
72         LOG.debug("Executing command: %s", cmd)
73         status, stdout, stderr = self.client.execute(cmd)
74
75         if status:
76             raise RuntimeError(stderr)
77
78         data = json.loads(stdout)
79
80         if "sla" in args:
81             sla_max_latency = int(args['sla']['max_latency'])
82             for result in data:
83                 latency = result['latency']
84                 assert latency <= sla_max_latency, "latency %f > " \
85                     "sla:max_latency(%f)" % (latency, sla_max_latency)
86
87         return data
88
89
90 def _test():
91     """internal test function"""
92     key_filename = pkg_resources.resource_filename('yardstick.resources',
93                                                    'files/yardstick_key')
94     ctx = {'host': '172.16.0.137',
95            'user': 'ubuntu',
96            'key_filename': key_filename
97            }
98
99     logger = logging.getLogger('yardstick')
100     logger.setLevel(logging.DEBUG)
101
102     p = Lmbench(ctx)
103
104     options = {'stride': 128, 'stop_size': 16}
105
106     args = {'options': options}
107     result = p.run(args)
108     print result
109
110 if __name__ == '__main__':
111     _test()