08755a08b9226bb7c579c3461acfc43f8e0f1a15
[yardstick.git] / yardstick / benchmark / scenarios / networking / ping.py
1 ##############################################################################
2 # Copyright (c) 2015 Ericsson AB 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
10 # ping scenario
11
12 import pkg_resources
13 import logging
14
15 import yardstick.ssh as ssh
16 from yardstick.benchmark.scenarios import base
17
18 LOG = logging.getLogger(__name__)
19
20
21 class Ping(base.Scenario):
22     """Execute ping between two hosts
23
24   Parameters
25     packetsize - number of data bytes to send
26         type:    int
27         unit:    bytes
28         default: 56
29     """
30
31     __scenario_type__ = "Ping"
32
33     TARGET_SCRIPT = 'ping_benchmark.bash'
34
35     def __init__(self, scenario_cfg, context_cfg):
36         self.scenario_cfg = scenario_cfg
37         self.context_cfg = context_cfg
38         self.target_script = pkg_resources.resource_filename(
39             'yardstick.benchmark.scenarios.networking', Ping.TARGET_SCRIPT)
40         host = self.context_cfg['host']
41         user = host.get('user', 'ubuntu')
42         ip = host.get('ip', None)
43         key_filename = host.get('key_filename', '/root/.ssh/id_rsa')
44         password = host.get('password', None)
45
46         if password is not None:
47             LOG.info("Log in via pw, user:%s, host:%s, pw:%s",
48                      user, ip, password)
49             self.connection = ssh.SSH(user, ip, password=password)
50         else:
51             LOG.info("Log in via key, user:%s, host:%s, key_filename:%s",
52                      user, ip, key_filename)
53             self.connection = ssh.SSH(user, ip, key_filename=key_filename)
54
55         self.connection.wait()
56
57     def run(self, result):
58         """execute the benchmark"""
59
60         if "options" in self.scenario_cfg:
61             options = "-s %s" % \
62                 self.scenario_cfg['options'].get("packetsize", '56')
63         else:
64             options = ""
65
66         destination = self.context_cfg['target'].get('ipaddr', '127.0.0.1')
67         dest_list = [s.strip() for s in destination.split(',')]
68
69         result["rtt"] = {}
70         rtt_result = result["rtt"]
71
72         for dest in dest_list:
73             LOG.debug("ping '%s' '%s'", options, dest)
74             exit_status, stdout, stderr = self.connection.execute(
75                 "/bin/sh -s {0} {1}".format(dest, options),
76                 stdin=open(self.target_script, "r"))
77
78             if exit_status != 0:
79                 raise RuntimeError(stderr)
80
81             if stdout:
82                 rtt_result[dest] = float(stdout)
83                 if "sla" in self.scenario_cfg:
84                     sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
85                     assert rtt_result[dest] <= sla_max_rtt, "rtt %f > sla:\
86                     max_rtt(%f); " % (rtt_result[dest], sla_max_rtt)
87             else:
88                 LOG.error("ping '%s' '%s' timeout", options, dest)
89
90
91 def _test():    # pragma: no cover
92     '''internal test function'''
93     key_filename = pkg_resources.resource_filename("yardstick.resources",
94                                                    "files/yardstick_key")
95     ctx = {
96         "host": {
97             "ip": "10.229.47.137",
98             "user": "root",
99             "key_filename": key_filename
100         },
101         "target": {
102             "ipaddr": "10.229.17.105",
103         }
104     }
105
106     logger = logging.getLogger("yardstick")
107     logger.setLevel(logging.DEBUG)
108
109     args = {}
110     result = {}
111
112     p = Ping(args, ctx)
113     p.run(result)
114     print result
115
116 if __name__ == '__main__':    # pragma: no cover
117     _test()