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