Merge "add memory_load scenario"
[yardstick.git] / yardstick / benchmark / scenarios / compute / ramspeed.py
1 ##############################################################################
2 # Copyright (c) 2016 Huawei Technologies Co.,Ltd 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 Ramspeed(base.Scenario):
20     """Execute ramspeed benchmark in a host
21     The ramspeed script takes a number of options which you can use to
22     customise a test.  The full run time usage
23     is:
24
25     ramspeed -b ID [-g size] [-m size] [-l runs] [-r speed-format]
26
27     -b  runs a specified benchmark (by an ID number):
28        1 -- INTmark [writing]          4 -- FLOATmark [writing]
29        2 -- INTmark [reading]          5 -- FLOATmark [reading]
30        3 -- INTmem                     6 -- FLOATmem
31        7 -- MMXmark [writing]          10 -- SSEmark [writing]
32        8 -- MMXmark [reading]          11 -- SSEmark [reading]
33        9 -- MMXmem                     12 -- SSEmem
34        13 -- MMXmark (nt) [writing]    16 -- SSEmark (nt) [writing]
35        14 -- MMXmark (nt) [reading]    17 -- SSEmark (nt) [reading]
36        15 -- MMXmem (nt)               18 -- SSEmem (nt)
37     In this scenario, only the first 6 test type will be used for testing.
38
39     -g  specifies a # of Gbytes per pass (default is 8)
40     -m  specifies a # of Mbytes per array (default is 32)
41     -l  enables the BatchRun mode (for *mem benchmarks only),
42        and specifies a # of runs (suggested is 5)
43     -r  displays speeds in real megabytes per second (default: decimal)
44
45     The -b option is required, others are recommended.
46
47     Parameters
48         type_id - specifies whether to run *mark benchmark or *mem benchmark
49                   the type_id can be any number from 1 to 19
50             type:       int
51             unit:       n/a
52             default:    1
53         load - specifies a # of Gbytes per pass
54             type:       int
55             unit:       gigabyte
56             default:    8
57         block_size - specifies a # of Mbytes per array
58             type:       int
59             unit:       megabyte
60             default:    32
61
62     Parameters for *mem benchmark
63         iteration - specifies a # of runs for each test
64             type:       int
65             unit:       n/a
66             default:    1
67     more info http://alasir.com/software/ramspeed
68     """
69     __scenario_type__ = "Ramspeed"
70
71     RAMSPEED_MARK_SCRIPT = "ramspeed_mark_benchmark.bash"
72     RAMSPEED_MEM_SCRIPT = "ramspeed_mem_benchmark.bash"
73
74     def __init__(self, scenario_cfg, context_cfg):
75         self.scenario_cfg = scenario_cfg
76         self.context_cfg = context_cfg
77         self.setup_done = False
78
79     def setup(self):
80         """scenario setup"""
81         self.mark_target_script = pkg_resources.resource_filename(
82             "yardstick.benchmark.scenarios.compute",
83             Ramspeed.RAMSPEED_MARK_SCRIPT)
84         self.mem_target_script = pkg_resources.resource_filename(
85             "yardstick.benchmark.scenarios.compute",
86             Ramspeed.RAMSPEED_MEM_SCRIPT)
87
88         host = self.context_cfg["host"]
89         user = host.get("user", "ubuntu")
90         ip = host.get("ip", None)
91         key_filename = host.get('key_filename', "~/.ssh/id_rsa")
92
93         LOG.info("user:%s, host:%s", user, ip)
94         self.client = ssh.SSH(user, ip, key_filename=key_filename)
95         self.client.wait(timeout=600)
96
97         # copy scripts to host
98         self.client.run("cat > ~/ramspeed_mark_benchmark.sh",
99                         stdin=open(self.mark_target_script, 'rb'))
100         self.client.run("cat > ~/ramspeed_mem_benchmark.sh",
101                         stdin=open(self.mem_target_script, 'rb'))
102         self.setup_done = True
103
104     def run(self, result):
105         """execute the benchmark"""
106
107         if not self.setup_done:
108             self.setup()
109
110         options = self.scenario_cfg['options']
111         test_id = options.get('type_id', 1)
112         load = options.get('load', 8)
113         block_size = options.get('block_size', 32)
114
115         if test_id == 3 or test_id == 6:
116             iteration = options.get('iteration', 1)
117             cmd = "sudo bash ramspeed_mem_benchmark.sh %d %d %d %d" % \
118                   (test_id, load, block_size, iteration)
119         elif 0 < test_id <= 5:
120             cmd = "sudo bash ramspeed_mark_benchmark.sh %d %d %d" % \
121                   (test_id, load, block_size)
122         # only the test_id 1-6 will be used in this scenario
123         else:
124             raise RuntimeError("No such type_id: %s for Ramspeed scenario",
125                                test_id)
126
127         LOG.debug("Executing command: %s", cmd)
128         status, stdout, stderr = self.client.execute(cmd)
129         if status:
130             raise RuntimeError(stderr)
131
132         result.update(json.loads(stdout))
133
134         if "sla" in self.scenario_cfg:
135             sla_error = ""
136             sla_min_bw = int(self.scenario_cfg['sla']['min_bandwidth'])
137             for i in result["Result"]:
138                 bw = i["Bandwidth(MBps)"]
139                 if bw < sla_min_bw:
140                     sla_error += "Bandwidth %f < " \
141                         "sla:min_bandwidth(%f)" % (bw, sla_min_bw)
142             assert sla_error == "", sla_error