1 from __future__ import absolute_import
2 from __future__ import print_function
11 from oslo_serialization import jsonutils
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
16 LOG = logging.getLogger(__name__)
17 LOG.setLevel(logging.DEBUG)
20 class QemuMigrate(base.Scenario):
22 Execute a live migration for two host using qemu
26 __scenario_type__ = "QemuMigrate"
28 TARGET_SCRIPT = "qemu_migrate_benchmark.bash"
29 WORKSPACE = "/root/workspace/"
30 REBOOT_CMD_PATTERN = r";\s*reboot\b"
32 def __init__(self, scenario_cfg, context_cfg):
33 self.scenario_cfg = scenario_cfg
34 self.context_cfg = context_cfg
35 self.setup_done = False
37 def _connect_host(self):
38 host = self.context_cfg["host"]
39 self.host = ssh.SSH.from_node(host, defaults={"user": "root"})
40 self.host.wait(timeout=600)
42 def _put_files(self, client):
43 setup_options = self.scenario_cfg["setup_options"]
44 rpm_dir = setup_options["rpm_dir"]
45 script_dir = setup_options["script_dir"]
46 image_dir = setup_options["image_dir"]
47 LOG.debug("Send RPMs from %s to workspace %s",
48 rpm_dir, self.WORKSPACE)
49 client.put(rpm_dir, self.WORKSPACE, recursive=True)
50 LOG.debug("Send scripts from %s to workspace %s",
51 script_dir, self.WORKSPACE)
52 client.put(script_dir, self.WORKSPACE, recursive=True)
53 LOG.debug("Send guest image from %s to workspace %s",
54 image_dir, self.WORKSPACE)
55 client.put(image_dir, self.WORKSPACE, recursive=True)
57 def _run_setup_cmd(self, client, cmd):
58 LOG.debug("Run cmd: %s", cmd)
59 status, _, stderr = client.execute(cmd)
61 if re.search(self.REBOOT_CMD_PATTERN, cmd):
62 LOG.debug("Error on reboot")
64 raise RuntimeError(stderr)
66 def _run_host_setup_scripts(self, scripts):
67 setup_options = self.scenario_cfg["setup_options"]
68 script_dir = os.path.basename(setup_options["script_dir"])
70 for script in scripts:
71 cmd = "cd %s/%s; export PATH=./:$PATH; %s" %\
72 (self.WORKSPACE, script_dir, script)
73 self._run_setup_cmd(self.host, cmd)
75 if re.search(self.REBOOT_CMD_PATTERN, cmd):
81 setup_options = self.scenario_cfg["setup_options"]
82 host_setup_seqs = setup_options["host_setup_seqs"]
85 self._put_files(self.host)
86 self._run_host_setup_scripts(host_setup_seqs)
89 self.target_script = pkg_resources.resource_filename(
90 "yardstick.benchmark.scenarios.compute",
91 QemuMigrate.TARGET_SCRIPT)
92 self.host._put_file_shell(
93 self.target_script, "~/qemu_migrate_benchmark.sh")
95 self.setup_done = True
97 def run(self, result):
98 """execute the benchmark"""
100 options = self.scenario_cfg["options"]
101 smp = options.get("smp", 2)
102 qmp_sock_src = options.get("qmp_src_path", "/tmp/qmp-sock-src")
103 qmp_sock_dst = options.get("qmp_dst_path", "/tmp/qmp-sock-dst")
104 incoming_ip = options.get("incoming_ip", 0)
105 migrate_to_port = options.get("migrate_to_port", 4444)
106 max_down_time = options.get("max_down_time", 0.10)
107 cmd_args = " %s %s %s %s %s %s" %\
108 (smp, qmp_sock_src, qmp_sock_dst, incoming_ip,
109 migrate_to_port, max_down_time)
110 cmd = "bash qemu_migrate_benchmark.sh %s" % (cmd_args)
111 LOG.debug("Executing command: %s", cmd)
112 status, stdout, stderr = self.host.execute(cmd)
114 raise RuntimeError(stderr)
116 result.update(jsonutils.loads(stdout))
118 if "sla" in self.scenario_cfg:
120 for t, timevalue in result.items():
121 if 'max_%s' % t not in self.scenario_cfg['sla']:
124 sla_time = int(self.scenario_cfg['sla'][
126 timevalue = int(timevalue)
127 if timevalue > sla_time:
128 sla_error += "%s timevalue %d > sla:max_%s(%d); " % \
129 (t, timevalue, t, sla_time)
130 self.verify_SLA(sla_error == "", sla_error)
133 def _test(): # pragma: no cover
134 """internal test function"""
135 key_filename = pkg_resources.resource_filename("yardstick.resources",
136 "files/yardstick_key")
139 "ip": "10.229.47.137",
141 "key_filename": key_filename
145 logger = logging.getLogger("yardstick")
146 logger.setLevel(logging.DEBUG)
149 "migrate_to_port": 4444,
151 "qmp_sock_src": "/tmp/qmp-sock-src",
152 "qmp_sock_dst": "/tmp/qmp-sock-dst",
153 "max_down_time": 0.10
157 "max_downtime": 0.10,
158 "max_setuptime": 0.50,
166 migrate = QemuMigrate(args, ctx)
170 if __name__ == '__main__': # pragma: no cover