1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
16 LOG = logging.getLogger(__name__)
19 class Lmbench(base.Scenario):
20 """Execute lmbench memory read latency or memory bandwidth benchmark in a host
23 test_type - specifies whether to measure memory latency or bandwidth
28 Parameters for memory read latency benchmark
29 stride - number of locations in memory between starts of array elements
33 stop_size - maximum array size to test (minimum value is 0.000512)
38 Results are accurate to the ~2-5 nanosecond range.
40 Parameters for memory bandwidth benchmark
41 size - the amount of memory to test
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
50 warmup - the number of repetitons to perform before taking measurements
54 more info http://manpages.ubuntu.com/manpages/trusty/lmbench.8.html
56 __scenario_type__ = "Lmbench"
58 LATENCY_BENCHMARK_SCRIPT = "lmbench_latency_benchmark.bash"
59 BANDWIDTH_BENCHMARK_SCRIPT = "lmbench_bandwidth_benchmark.bash"
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
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")
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)
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
90 def run(self, result):
91 """execute the benchmark"""
93 if not self.setup_done:
96 options = self.scenario_cfg['options']
97 test_type = options.get('test_type', 'latency')
99 if test_type == 'latency':
100 stride = options.get('stride', 128)
101 stop_size = options.get('stop_size', 16)
102 cmd = "sudo bash lmbench_latency.sh %d %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)
110 raise RuntimeError("No such test_type: %s for Lmbench scenario",
113 LOG.debug("Executing command: %s", cmd)
114 status, stdout, stderr = self.client.execute(cmd)
117 raise RuntimeError(stderr)
119 if test_type == 'latency':
120 result.update({"latencies": json.loads(stdout)})
122 result.update(json.loads(stdout))
124 if "sla" in self.scenario_cfg:
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)
134 sla_min_bw = int(self.scenario_cfg['sla']['min_bandwidth'])
135 bw = result["bandwidth(MBps)"]
137 sla_error += "bandwidth %f < " \
138 "sla:min_bandwidth(%f)" % (bw, sla_min_bw)
139 assert sla_error == "", sla_error
143 """internal test function"""
144 key_filename = pkg_resources.resource_filename('yardstick.resources',
145 'files/yardstick_key')
148 'ip': '10.229.47.137',
150 'key_filename': key_filename
154 logger = logging.getLogger('yardstick')
155 logger.setLevel(logging.DEBUG)
158 'test_type': 'latency',
163 sla = {'max_latency': 35, 'action': 'monitor'}
164 args = {'options': options, 'sla': sla}
167 p = Lmbench(args, ctx)
171 if __name__ == '__main__':