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 ##############################################################################
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
16 LOG = logging.getLogger(__name__)
17 LOG.setLevel(logging.DEBUG)
20 class Pktgen(base.Scenario):
21 """Execute pktgen between two hosts
26 __scenario_type__ = "Pktgen"
28 TARGET_SCRIPT = 'pktgen_benchmark.bash'
30 def __init__(self, context):
31 self.context = context
32 self.setup_done = False
36 self.target_script = pkg_resources.resource_filename(
37 'yardstick.benchmark.scenarios.networking',
39 user = self.context.get('user', 'ubuntu')
40 host = self.context.get('host', None)
41 target = self.context.get('target', None)
42 key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
44 LOG.debug("user:%s, target:%s", user, target)
45 self.server = ssh.SSH(user, target, key_filename=key_filename)
46 self.server.wait(timeout=600)
48 LOG.debug("user:%s, host:%s", user, host)
49 self.client = ssh.SSH(user, host, key_filename=key_filename)
50 self.client.wait(timeout=600)
53 self.client.run("cat > ~/pktgen.sh",
54 stdin=open(self.target_script, "rb"))
56 self.setup_done = True
58 def _iptables_setup(self):
59 """Setup iptables on server to monitor for received packets"""
60 cmd = "sudo iptables -F; " \
61 "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP" \
62 % (1000 + self.number_of_ports)
63 LOG.debug("Executing command: %s", cmd)
64 status, _, stderr = self.server.execute(cmd)
66 raise RuntimeError(stderr)
68 def _iptables_get_result(self):
69 """Get packet statistics from server"""
70 cmd = "sudo iptables -L INPUT -vnx |" \
71 "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'" \
72 % (1000 + self.number_of_ports)
73 LOG.debug("Executing command: %s", cmd)
74 status, stdout, stderr = self.server.execute(cmd)
76 raise RuntimeError(stderr)
80 """execute the benchmark"""
82 if not self.setup_done:
85 ipaddr = args.get("ipaddr", '127.0.0.1')
87 options = args['options']
88 packetsize = options.get("packetsize", 60)
89 self.number_of_ports = options.get("number_of_ports", 10)
91 self._iptables_setup()
93 cmd = "sudo bash pktgen.sh %s %s %s" \
94 % (ipaddr, self.number_of_ports, packetsize)
95 LOG.debug("Executing command: %s", cmd)
96 status, stdout, stderr = self.client.execute(cmd)
99 raise RuntimeError(stderr)
101 data = json.loads(stdout)
103 data['packets_received'] = self._iptables_get_result()
106 sent = data['packets_sent']
107 received = data['packets_received']
108 ppm = 1000000 * (sent - received) / sent
109 sla_max_ppm = int(args["sla"]["max_ppm"])
110 assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
117 '''internal test function'''
118 key_filename = pkg_resources.resource_filename('yardstick.resources',
119 'files/yardstick_key')
120 ctx = {'host': '172.16.0.137',
121 'target': '172.16.0.138',
123 'key_filename': key_filename
126 logger = logging.getLogger('yardstick')
127 logger.setLevel(logging.DEBUG)
131 options = {'packetsize': 120}
133 args = {'options': options,
134 'ipaddr': '192.168.111.31'}
138 if __name__ == '__main__':