aa1a500cfeeed01b34179f959b67ea97ea407d08
[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', '~/.ssh/id_rsa')
44         password = host.get('password', 'root')
45
46         LOG.info("user:%s, host:%s", user, ip)
47         self.connection = ssh.SSH(user, ip, key_filename=key_filename,
48                                   password=password)
49         self.connection.wait()
50
51     def run(self, result):
52         """execute the benchmark"""
53
54         if "options" in self.scenario_cfg:
55             options = "-s %s" % \
56                 self.scenario_cfg['options'].get("packetsize", '56')
57         else:
58             options = ""
59
60         destination = self.context_cfg['target'].get("ipaddr", '127.0.0.1')
61
62         LOG.debug("ping '%s' '%s'", options, destination)
63
64         exit_status, stdout, stderr = self.connection.execute(
65             "/bin/sh -s {0} {1}".format(destination, options),
66             stdin=open(self.target_script, "r"))
67
68         if exit_status != 0:
69             raise RuntimeError(stderr)
70
71         if stdout:
72             result["rtt"] = float(stdout)
73
74             if "sla" in self.scenario_cfg:
75                 sla_max_rtt = int(self.scenario_cfg["sla"]["max_rtt"])
76                 assert result["rtt"] <= sla_max_rtt, \
77                     "rtt %f > sla:max_rtt(%f); " % (result["rtt"], sla_max_rtt)
78         else:
79             LOG.error("ping '%s' '%s' timeout", options, destination)
80
81
82 def _test():
83     '''internal test function'''
84     key_filename = pkg_resources.resource_filename("yardstick.resources",
85                                                    "files/yardstick_key")
86     ctx = {
87         "host": {
88             "ip": "10.229.47.137",
89             "user": "root",
90             "key_filename": key_filename
91         },
92         "target": {
93             "ipaddr": "10.229.17.105",
94         }
95     }
96
97     logger = logging.getLogger("yardstick")
98     logger.setLevel(logging.DEBUG)
99
100     args = {}
101     result = {}
102
103     p = Ping(args, ctx)
104     p.run(result)
105     print result
106
107 if __name__ == '__main__':
108     _test()