Fix lmbench memory read latency stop size
[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
61     def __init__(self, scenario_cfg, context_cfg):
62         self.scenario_cfg = scenario_cfg
63         self.context_cfg = context_cfg
64         self.setup_done = False
65
66     def setup(self):
67         """scenario setup"""
68         self.bandwidth_target_script = pkg_resources.resource_filename(
69             "yardstick.benchmark.scenarios.compute",
70             Lmbench.BANDWIDTH_BENCHMARK_SCRIPT)
71         self.latency_target_script = pkg_resources.resource_filename(
72             "yardstick.benchmark.scenarios.compute",
73             Lmbench.LATENCY_BENCHMARK_SCRIPT)
74         host = self.context_cfg["host"]
75         user = host.get("user", "ubuntu")
76         ip = host.get("ip", None)
77         key_filename = host.get('key_filename', "~/.ssh/id_rsa")
78
79         LOG.info("user:%s, host:%s", user, ip)
80         self.client = ssh.SSH(user, ip, key_filename=key_filename)
81         self.client.wait(timeout=600)
82
83         # copy scripts to host
84         self.client.run("cat > ~/lmbench_latency.sh",
85                         stdin=open(self.latency_target_script, 'rb'))
86         self.client.run("cat > ~/lmbench_bandwidth.sh",
87                         stdin=open(self.bandwidth_target_script, 'rb'))
88         self.setup_done = True
89
90     def run(self, result):
91         """execute the benchmark"""
92
93         if not self.setup_done:
94             self.setup()
95
96         options = self.scenario_cfg['options']
97         test_type = options.get('test_type', 'latency')
98
99         if test_type == 'latency':
100             stride = options.get('stride', 128)
101             stop_size = options.get('stop_size', 16.0)
102             cmd = "sudo bash lmbench_latency.sh %f %d" % (stop_size, stride)
103         elif test_type == 'bandwidth':
104             size = options.get('size', 128)
105             benchmark = options.get('benchmark', 'rd')
106             warmup_repetitions = options.get('warmup', 0)
107             cmd = "sudo bash lmbench_bandwidth.sh %d %s %d" % \
108                   (size, benchmark, warmup_repetitions)
109         else:
110             raise RuntimeError("No such test_type: %s for Lmbench scenario",
111                                test_type)
112
113         LOG.debug("Executing command: %s", cmd)
114         status, stdout, stderr = self.client.execute(cmd)
115
116         if status:
117             raise RuntimeError(stderr)
118
119         if test_type == 'latency':
120             result.update({"latencies": json.loads(stdout)})
121         else:
122             result.update(json.loads(stdout))
123
124         if "sla" in self.scenario_cfg:
125             sla_error = ""
126             if test_type == 'latency':
127                 sla_max_latency = int(self.scenario_cfg['sla']['max_latency'])
128                 for t_latency in result["latencies"]:
129                     latency = t_latency['latency']
130                     if latency > sla_max_latency:
131                         sla_error += "latency %f > sla:max_latency(%f); " \
132                             % (latency, sla_max_latency)
133             else:
134                 sla_min_bw = int(self.scenario_cfg['sla']['min_bandwidth'])
135                 bw = result["bandwidth(MBps)"]
136                 if bw < sla_min_bw:
137                     sla_error += "bandwidth %f < " \
138                                  "sla:min_bandwidth(%f)" % (bw, sla_min_bw)
139             assert sla_error == "", sla_error
140
141
142 def _test():
143     """internal test function"""
144     key_filename = pkg_resources.resource_filename('yardstick.resources',
145                                                    'files/yardstick_key')
146     ctx = {
147         'host': {
148             'ip': '10.229.47.137',
149             'user': 'root',
150             'key_filename': key_filename
151         }
152     }
153
154     logger = logging.getLogger('yardstick')
155     logger.setLevel(logging.DEBUG)
156
157     options = {
158         'test_type': 'latency',
159         'stride': 128,
160         'stop_size': 16
161     }
162
163     sla = {'max_latency': 35, 'action': 'monitor'}
164     args = {'options': options, 'sla': sla}
165     result = {}
166
167     p = Lmbench(args, ctx)
168     p.run(result)
169     print result
170
171 if __name__ == '__main__':
172     _test()