518840c09cd820ecc133ff88f83729b68476d7c0
[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 or memory bandwidth benchmark in a host
21
22     Parameters
23         test_type - specifies whether to measure memory latency or bandwidth
24             type:       string
25             unit:       na
26             default:    "latency"
27
28     Parameters for memory read latency benchmark
29         stride - number of locations in memory between starts of array elements
30             type:       int
31             unit:       bytes
32             default:    128
33         stop_size - maximum array size to test (minimum value is 0.000512)
34             type:       float
35             unit:       megabytes
36             default:    16.0
37
38         Results are accurate to the ~2-5 nanosecond range.
39
40     Parameters for memory bandwidth benchmark
41         size - the amount of memory to test
42             type:       int
43             unit:       kilobyte
44             default:    128
45         benchmark - the name of the memory bandwidth benchmark test to execute.
46         Valid test names are rd, wr, rdwr, cp, frd, fwr, fcp, bzero, bcopy
47             type:       string
48             unit:       na
49             default:    "rd"
50         warmup - the number of repetitons to perform before taking measurements
51             type:       int
52             unit:       na
53             default:    0
54     more info http://manpages.ubuntu.com/manpages/trusty/lmbench.8.html
55     """
56     __scenario_type__ = "Lmbench"
57
58     LATENCY_BENCHMARK_SCRIPT = "lmbench_latency_benchmark.bash"
59     BANDWIDTH_BENCHMARK_SCRIPT = "lmbench_bandwidth_benchmark.bash"
60     LATENCY_CACHE_SCRIPT = "lmbench_latency_for_cache.bash"
61
62     def __init__(self, scenario_cfg, context_cfg):
63         self.scenario_cfg = scenario_cfg
64         self.context_cfg = context_cfg
65         self.setup_done = False
66
67     def setup(self):
68         """scenario setup"""
69         self.bandwidth_target_script = pkg_resources.resource_filename(
70             "yardstick.benchmark.scenarios.compute",
71             Lmbench.BANDWIDTH_BENCHMARK_SCRIPT)
72         self.latency_target_script = pkg_resources.resource_filename(
73             "yardstick.benchmark.scenarios.compute",
74             Lmbench.LATENCY_BENCHMARK_SCRIPT)
75         self.latency_for_cache_script = pkg_resources.resource_filename(
76             "yardstick.benchmark.scenarios.compute",
77             Lmbench.LATENCY_CACHE_SCRIPT)
78         host = self.context_cfg["host"]
79         user = host.get("user", "ubuntu")
80         ssh_port = host.get("ssh_port", ssh.DEFAULT_PORT)
81         ip = host.get("ip", None)
82         key_filename = host.get('key_filename', "~/.ssh/id_rsa")
83
84         LOG.info("user:%s, host:%s", user, ip)
85         self.client = ssh.SSH(user, ip, key_filename=key_filename,
86                               port=ssh_port)
87         self.client.wait(timeout=600)
88
89         # copy scripts to host
90         self.client._put_file_shell(
91             self.latency_target_script, '~/lmbench_latency.sh')
92         self.client._put_file_shell(
93             self.bandwidth_target_script, '~/lmbench_bandwidth.sh')
94         self.client._put_file_shell(
95             self.latency_for_cache_script, '~/lmbench_latency_for_cache.sh')
96         self.setup_done = True
97
98     def run(self, result):
99         """execute the benchmark"""
100
101         if not self.setup_done:
102             self.setup()
103
104         options = self.scenario_cfg['options']
105         test_type = options.get('test_type', 'latency')
106
107         if test_type == 'latency':
108             stride = options.get('stride', 128)
109             stop_size = options.get('stop_size', 16.0)
110             cmd = "sudo bash lmbench_latency.sh %f %d" % (stop_size, stride)
111         elif test_type == 'bandwidth':
112             size = options.get('size', 128)
113             benchmark = options.get('benchmark', 'rd')
114             warmup_repetitions = options.get('warmup', 0)
115             cmd = "sudo bash lmbench_bandwidth.sh %d %s %d" % \
116                   (size, benchmark, warmup_repetitions)
117         elif test_type == 'latency_for_cache':
118             repetition = options.get('repetition', 1)
119             warmup = options.get('warmup', 0)
120             cmd = "sudo bash lmbench_latency_for_cache.sh %d %d" % \
121                   (repetition, warmup)
122         else:
123             raise RuntimeError("No such test_type: %s for Lmbench scenario",
124                                test_type)
125
126         LOG.debug("Executing command: %s", cmd)
127         status, stdout, stderr = self.client.execute(cmd)
128
129         if status:
130             raise RuntimeError(stderr)
131
132         if test_type == 'latency':
133             result.update({"latencies": json.loads(stdout)})
134         else:
135             result.update(json.loads(stdout))
136
137         if "sla" in self.scenario_cfg:
138             sla_error = ""
139             if test_type == 'latency':
140                 sla_max_latency = int(self.scenario_cfg['sla']['max_latency'])
141                 for t_latency in result["latencies"]:
142                     latency = t_latency['latency']
143                     if latency > sla_max_latency:
144                         sla_error += "latency %f > sla:max_latency(%f); " \
145                             % (latency, sla_max_latency)
146             elif test_type == 'bandwidth':
147                 sla_min_bw = int(self.scenario_cfg['sla']['min_bandwidth'])
148                 bw = result["bandwidth(MBps)"]
149                 if bw < sla_min_bw:
150                     sla_error += "bandwidth %f < " \
151                                  "sla:min_bandwidth(%f)" % (bw, sla_min_bw)
152             elif test_type == 'latency_for_cache':
153                 sla_latency = float(self.scenario_cfg['sla']['max_latency'])
154                 cache_latency = float(result['L1cache'])
155                 if sla_latency < cache_latency:
156                     sla_error += "latency %f > sla:max_latency(%f); " \
157                         % (cache_latency, sla_latency)
158             assert sla_error == "", sla_error
159
160
161 def _test():
162     """internal test function"""
163     key_filename = pkg_resources.resource_filename('yardstick.resources',
164                                                    'files/yardstick_key')
165     ctx = {
166         'host': {
167             'ip': '10.229.47.137',
168             'user': 'root',
169             'key_filename': key_filename
170         }
171     }
172
173     logger = logging.getLogger('yardstick')
174     logger.setLevel(logging.DEBUG)
175
176     options = {
177         'test_type': 'latency',
178         'stride': 128,
179         'stop_size': 16
180     }
181
182     sla = {'max_latency': 35, 'action': 'monitor'}
183     args = {'options': options, 'sla': sla}
184     result = {}
185
186     p = Lmbench(args, ctx)
187     p.run(result)
188     print result
189
190 if __name__ == '__main__':
191     _test()