Merge "Remove setting logger level to debug in scenarios"
[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, context):
36         self.context = context
37         self.target_script = pkg_resources.resource_filename(
38             'yardstick.benchmark.scenarios.networking', Ping.TARGET_SCRIPT)
39         user = self.context.get('user', 'ubuntu')
40         host = self.context.get('host', None)
41         key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
42
43         LOG.info("user:%s, host:%s", user, host)
44
45         self.connection = ssh.SSH(user, host, key_filename=key_filename)
46         self.connection.wait()
47
48     def run(self, args):
49         """execute the benchmark"""
50
51         if "options" in args:
52             options = "-s %s" % args['options'].get("packetsize", '56')
53         else:
54             options = ""
55
56         destination = args.get("ipaddr", '127.0.0.1')
57
58         LOG.debug("ping '%s' '%s'", options, destination)
59
60         exit_status, stdout, stderr = self.connection.execute(
61             "/bin/sh -s {0} {1}".format(destination, options),
62             stdin=open(self.target_script, "r"))
63
64         if exit_status != 0:
65             raise RuntimeError(stderr)
66
67         rtt = float(stdout)
68
69         if "sla" in args:
70             sla_max_rtt = int(args["sla"]["max_rtt"])
71             assert rtt <= sla_max_rtt, "rtt %f > sla:max_rtt(%f)" % \
72                 (rtt, sla_max_rtt)
73
74         return rtt