Update sla check for scenarios
[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, result):
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         result.update(json.loads(stdout))
79
80         if "sla" in args:
81             sla_error = ""
82             sla_max_latency = int(args['sla']['max_latency'])
83             for t_latency in result:
84                 latency = t_latency['latency']
85                 if latency > sla_max_latency:
86                     sla_error += "latency %f > sla:max_latency(%f); " \
87                         % (latency, sla_max_latency)
88             assert sla_error == "", sla_error
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()