Add send socket commands function
[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.common import utils
18 from yardstick.benchmark.scenarios import base
19
20 LOG = logging.getLogger(__name__)
21
22
23 class Ramspeed(base.Scenario):
24     """Execute ramspeed benchmark in a host
25     The ramspeed script takes a number of options which you can use to
26     customise a test.  The full run time usage
27     is:
28
29     ramspeed -b ID [-g size] [-m size] [-l runs] [-r speed-format]
30
31     -b  runs a specified benchmark (by an ID number):
32        1 -- INTmark [writing]          4 -- FLOATmark [writing]
33        2 -- INTmark [reading]          5 -- FLOATmark [reading]
34        3 -- INTmem                     6 -- FLOATmem
35        7 -- MMXmark [writing]          10 -- SSEmark [writing]
36        8 -- MMXmark [reading]          11 -- SSEmark [reading]
37        9 -- MMXmem                     12 -- SSEmem
38        13 -- MMXmark (nt) [writing]    16 -- SSEmark (nt) [writing]
39        14 -- MMXmark (nt) [reading]    17 -- SSEmark (nt) [reading]
40        15 -- MMXmem (nt)               18 -- SSEmem (nt)
41     In this scenario, only the first 6 test type will be used for testing.
42
43     -g  specifies a # of Gbytes per pass (default is 8)
44     -m  specifies a # of Mbytes per array (default is 32)
45     -l  enables the BatchRun mode (for *mem benchmarks only),
46        and specifies a # of runs (suggested is 5)
47     -r  displays speeds in real megabytes per second (default: decimal)
48
49     The -b option is required, others are recommended.
50
51     Parameters
52         type_id - specifies whether to run *mark benchmark or *mem benchmark
53                   the type_id can be any number from 1 to 19
54             type:       int
55             unit:       n/a
56             default:    1
57         load - specifies a # of Gbytes per pass
58             type:       int
59             unit:       gigabyte
60             default:    8
61         block_size - specifies a # of Mbytes per array
62             type:       int
63             unit:       megabyte
64             default:    32
65
66     Parameters for *mem benchmark
67         iteration - specifies a # of runs for each test
68             type:       int
69             unit:       n/a
70             default:    1
71     more info http://alasir.com/software/ramspeed
72     """
73     __scenario_type__ = "Ramspeed"
74
75     RAMSPEED_MARK_SCRIPT = "ramspeed_mark_benchmark.bash"
76     RAMSPEED_MEM_SCRIPT = "ramspeed_mem_benchmark.bash"
77
78     def __init__(self, scenario_cfg, context_cfg):
79         self.scenario_cfg = scenario_cfg
80         self.context_cfg = context_cfg
81         self.setup_done = False
82
83     def setup(self):
84         """scenario setup"""
85         self.mark_target_script = pkg_resources.resource_filename(
86             "yardstick.benchmark.scenarios.compute",
87             Ramspeed.RAMSPEED_MARK_SCRIPT)
88         self.mem_target_script = pkg_resources.resource_filename(
89             "yardstick.benchmark.scenarios.compute",
90             Ramspeed.RAMSPEED_MEM_SCRIPT)
91
92         host = self.context_cfg["host"]
93
94         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
95         self.client.wait(timeout=600)
96
97         # copy scripts to host
98         self.client._put_file_shell(
99             self.mark_target_script, '~/ramspeed_mark_benchmark.sh')
100         self.client._put_file_shell(
101             self.mem_target_script, '~/ramspeed_mem_benchmark.sh')
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         ramspeed_result = jsonutils.loads(stdout)
133         result.update(utils.flatten_dict_key(ramspeed_result))
134
135         if "sla" in self.scenario_cfg:
136             sla_error = ""
137             sla_min_bw = int(self.scenario_cfg['sla']['min_bandwidth'])
138             for i in ramspeed_result["Result"]:
139                 bw = i["Bandwidth(MBps)"]
140                 if bw < sla_min_bw:
141                     sla_error += "Bandwidth %f < " \
142                         "sla:min_bandwidth(%f)" % (bw, sla_min_bw)
143             assert sla_error == "", sla_error