Merge "Cleanup process test cases"
[yardstick.git] / yardstick / benchmark / scenarios / compute / spec_cpu_for_vm.py
1 ##############################################################################
2 # Copyright (c) 2017 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 import pkg_resources
13 import os
14
15 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
17 from yardstick.common.constants import YARDSTICK_ROOT_PATH
18
19 LOG = logging.getLogger(__name__)
20
21
22 class SpecCPUforVM(base.Scenario):
23     """Spec CPU2006 benchmark for Virtual Machine
24
25     Parameters
26         benchmark_subset - Specifies a subset of SPEC CPU2006 benchmarks to run
27             type:       string
28             unit:       na
29             default:    na
30
31         SPECint_benchmark - A SPECint benchmark to run
32             type:       string
33             unit:       na
34             default:    na
35
36         SPECfp_benchmark - A SPECfp benchmark to run
37             type:       string
38             unit:       na
39             default:    na
40
41         output_format - Desired report format
42             type:       string
43             unit:       na
44             default:    na
45
46         runspec_config - SPEC CPU2006 config file provided to the runspec binary
47             type:       string
48             unit:       na
49             default:    "Example-linux64-amd64-gcc43+.cfg"
50
51         runspec_iterations - The number of benchmark iterations to execute.
52                              For a reportable run, must be 3.
53             type:       int
54             unit:       na
55             default:    na
56
57         runspec_tune - Tuning to use (base, peak, or all). For a reportable run, must be either
58                        base or all. Reportable runs do base first, then (optionally) peak.
59             type:       string
60             unit:       na
61             default:    na
62
63         runspec_size - Size of input data to run (test, train, or ref). Reportable runs ensure
64                        that your binaries can produce correct results with the test and train
65                        workloads.
66             type:       string
67             unit:       na
68             default:    na
69     """
70     __scenario_type__ = "SpecCPU2006_for_VM"
71     CPU2006_ISO = "cpu2006-1.2.iso"
72     CPU2006_DIR = "~/cpu2006"
73     CPU2006_RESULT_FILE = os.path.join(CPU2006_DIR, "result/CINT2006.001.ref.txt")
74
75     def __init__(self, scenario_cfg, context_cfg):
76         self.scenario_cfg = scenario_cfg
77         self.context_cfg = context_cfg
78         self.setup_done = False
79         self.options = self.scenario_cfg['options']
80
81     def setup(self):
82         """scenario setup"""
83         host = self.context_cfg['host']
84         LOG.info("user:%s, host:%s", host['user'], host['ip'])
85         self.client = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
86         self.client.wait(timeout=600)
87
88         spec_cpu_iso = os.path.join(YARDSTICK_ROOT_PATH,
89                                     "yardstick/resources/files/",
90                                     self.CPU2006_ISO)
91
92         self.client.put(spec_cpu_iso, "~/cpu2006-1.2.iso")
93         self.client.execute("sudo mount -t iso9660 -o ro,exec ~/cpu2006-1.2.iso /mnt")
94         self.client.execute("/mnt/install.sh -fd ~/cpu2006")
95
96         if "runspec_config" in self.options:
97             self.runspec_config = self.options["runspec_config"]
98
99             self.runspec_config_file = pkg_resources.resource_filename(
100                 "yardstick.resources", 'files/' + self.runspec_config)
101
102             # copy SPEC CPU2006 config file to host if given
103             cfg_path = os.path.join(self.CPU2006_DIR,
104                                     'config/yardstick_spec_cpu2006.cfg')
105             self.client._put_file_shell(self.runspec_config_file, cfg_path)
106         else:
107             self.runspec_config = "Example-linux64-amd64-gcc43+.cfg"
108
109         self.setup_done = True
110
111     def run(self, result):
112         """execute the benchmark"""
113
114         if not self.setup_done:
115             self.setup()
116
117         cmd = "cd %s && . ./shrc && runspec --config %s" % (
118             self.CPU2006_DIR, self.runspec_config)
119         cmd_args = ""
120
121         if "rate" in self.options:
122             cmd_args += " --rate %s" % self.options["runspec_rate"]
123
124         if "output_format" in self.options:
125             cmd_args += " --output_format %s" % self.options["output_format"]
126
127         if "runspec_tune" in self.options:
128             cmd_args += " --tune %s" % self.options["runspec_tune"]
129
130         benchmark_subset = self.options.get('benchmark_subset', None)
131         specint_benchmark = self.options.get('SPECint_benchmark', None)
132         specfp_benchmark = self.options.get('SPECfp_benchmark', None)
133
134         if benchmark_subset:
135             cmd_args += " %s" % benchmark_subset
136         else:
137             cmd_args += " --noreportable"
138
139             if "runspec_iterations" in self.options:
140                 cmd_args += " --iterations %s" % self.options["runspec_iterations"]
141
142             if "runspec_size" in self.options:
143                 cmd_args += " --size %s" % self.options["runspec_size"]
144
145             if specint_benchmark:
146                 cmd_args += " %s" % specint_benchmark
147
148             if specfp_benchmark:
149                 cmd_args += " %s" % specfp_benchmark
150
151         cmd += "%s" % cmd_args
152
153         LOG.debug("Executing command: %s", cmd)
154         status, stdout, stderr = self.client.execute(cmd, timeout=86400)
155         if status:
156             raise RuntimeError(stderr)
157
158         cmd = "cat %s" % self.CPU2006_RESULT_FILE
159         LOG.debug("Executing command: %s", cmd)
160         status, stdout, stderr = self.client.execute(cmd, timeout=30)
161         if status:
162             raise RuntimeError(stderr)
163         if stdout:
164             LOG.info("SPEC CPU2006 result is:\n%s", stdout)
165
166         result.update({"SPEC_CPU_result": stdout})
167         # fetch SPEC CPU2006 result files
168         self.client.get('~/cpu2006/result', '/tmp/')
169         LOG.info('SPEC CPU2006 benchmark completed, please find benchmark reports \
170                   at /tmp/result directory')