1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB and others.
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 ##############################################################################
12 from __future__ import print_function
13 from __future__ import absolute_import
17 import yardstick.ssh as ssh
18 from yardstick.common import utils
19 from yardstick.benchmark.scenarios import base
21 LOG = logging.getLogger(__name__)
24 class Ping(base.Scenario):
25 """Execute ping between two hosts
28 packetsize - number of data bytes to send
34 __scenario_type__ = "Ping"
36 TARGET_SCRIPT = 'ping_benchmark.bash'
38 def __init__(self, scenario_cfg, context_cfg):
39 self.scenario_cfg = scenario_cfg
40 self.context_cfg = context_cfg
41 self.target_script = pkg_resources.resource_filename(
42 'yardstick.benchmark.scenarios.networking', Ping.TARGET_SCRIPT)
43 host = self.context_cfg['host']
45 self.connection = ssh.SSH.from_node(host, defaults={"user": "ubuntu"})
47 self.connection.wait(timeout=600)
49 def run(self, result):
50 """execute the benchmark"""
52 if "options" in self.scenario_cfg:
54 self.scenario_cfg['options'].get("packetsize", '56')
58 destination = self.context_cfg['target'].get('ipaddr', '127.0.0.1')
59 dest_list = [s.strip() for s in destination.split(',')]
62 ping_result = {"rtt": rtt_result}
64 for pos, dest in enumerate(dest_list):
65 if 'targets' in self.scenario_cfg:
66 target_vm = self.scenario_cfg['targets'][pos]
68 target_vm = self.scenario_cfg['target']
70 LOG.debug("ping %s %s", options, dest)
71 with open(self.target_script, "r") as stdin_file:
72 exit_status, stdout, stderr = self.connection.execute(
73 "/bin/sh -s {0} {1}".format(dest, options),
77 raise RuntimeError(stderr)
80 if isinstance(target_vm, dict):
81 target_vm_name = target_vm.get("name")
83 target_vm_name = target_vm.split('.')[0]
84 rtt_result[target_vm_name] = float(stdout)
85 if "sla" in self.scenario_cfg:
86 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
87 assert rtt_result[target_vm_name] <= sla_max_rtt,\
88 "rtt %f > sla: max_rtt(%f); " % \
89 (rtt_result[target_vm_name], sla_max_rtt)
91 LOG.error("ping '%s' '%s' timeout", options, target_vm)
92 result.update(utils.flatten_dict_key(ping_result))
95 def _test(): # pragma: no cover
96 """internal test function"""
97 key_filename = pkg_resources.resource_filename("yardstick.resources",
98 "files/yardstick_key")
101 "ip": "10.229.47.137",
103 "key_filename": key_filename
106 "ipaddr": "10.229.17.105",
110 logger = logging.getLogger("yardstick")
111 logger.setLevel(logging.DEBUG)
121 if __name__ == '__main__': # pragma: no cover