a027c817ab83dc72c9ca8c766e53d660dd952280
[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     """Executes a ping benchmark between two hosts"""
23     __scenario_type__ = "Ping"
24
25     TARGET_SCRIPT = 'ping_benchmark.bash'
26
27     def __init__(self, context):
28         self.context = context
29         self.target_script = pkg_resources.resource_filename(
30             'yardstick.benchmark.scenarios.networking', Ping.TARGET_SCRIPT)
31         user = self.context.get('user', 'ubuntu')
32         host = self.context.get('host', None)
33         key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
34
35         LOG.debug("user:%s, host:%s", user, host)
36
37         self.connection = ssh.SSH(user, host, key_filename=key_filename)
38         self.connection.wait()
39
40     def run(self, args):
41         """execute the benchmark"""
42
43         self.options = "-s %s" % args['options'].get("packetsize", '56')
44         self.ipaddr = args.get("ipaddr", '127.0.0.1')
45
46         LOG.debug("ping %s %s", self.options, self.ipaddr)
47
48         exit_status, stdout, stderr = self.connection.execute(
49             "/bin/sh -s {0} {1}".format(self.ipaddr, self.options),
50             stdin=open(self.target_script, "r"))
51
52         if exit_status != 0:
53             raise RuntimeError(stderr)
54
55         rtt = float(stdout)
56
57         if "sla" in args:
58             sla_max_rtt = int(args["sla"]["max_rtt"])
59             assert rtt <= sla_max_rtt, "rtt %f > sla_max_rtt" % rtt
60
61         return rtt