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
24 packetsize - packet size in bytes without the CRC
28 number_of_ports - number of UDP ports to test
32 duration - duration of the test
37 __scenario_type__ = "Pktgen"
39 TARGET_SCRIPT = 'pktgen_benchmark.bash'
41 def __init__(self, context):
42 self.context = context
43 self.setup_done = False
47 self.target_script = pkg_resources.resource_filename(
48 'yardstick.benchmark.scenarios.networking',
50 user = self.context.get('user', 'ubuntu')
51 host = self.context.get('host', None)
52 target = self.context.get('target', None)
53 key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
55 LOG.debug("user:%s, target:%s", user, target)
56 self.server = ssh.SSH(user, target, key_filename=key_filename)
57 self.server.wait(timeout=600)
59 LOG.debug("user:%s, host:%s", user, host)
60 self.client = ssh.SSH(user, host, key_filename=key_filename)
61 self.client.wait(timeout=600)
64 self.client.run("cat > ~/pktgen.sh",
65 stdin=open(self.target_script, "rb"))
67 self.setup_done = True
69 def _iptables_setup(self):
70 """Setup iptables on server to monitor for received packets"""
71 cmd = "sudo iptables -F; " \
72 "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP" \
73 % (1000 + self.number_of_ports)
74 LOG.debug("Executing command: %s", cmd)
75 status, _, stderr = self.server.execute(cmd)
77 raise RuntimeError(stderr)
79 def _iptables_get_result(self):
80 """Get packet statistics from server"""
81 cmd = "sudo iptables -L INPUT -vnx |" \
82 "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'" \
83 % (1000 + self.number_of_ports)
84 LOG.debug("Executing command: %s", cmd)
85 status, stdout, stderr = self.server.execute(cmd)
87 raise RuntimeError(stderr)
91 """execute the benchmark"""
93 if not self.setup_done:
96 ipaddr = args.get("ipaddr", '127.0.0.1')
98 options = args['options']
99 packetsize = options.get("packetsize", 60)
100 self.number_of_ports = options.get("number_of_ports", 10)
101 # if run by a duration runner
102 duration_time = self.context.get("duration", None)
103 # if run by an arithmetic runner
104 arithmetic_time = options.get("duration", None)
107 duration = duration_time
108 elif arithmetic_time:
109 duration = arithmetic_time
113 self._iptables_setup()
115 cmd = "sudo bash pktgen.sh %s %s %s %s" \
116 % (ipaddr, self.number_of_ports, packetsize, duration)
117 LOG.debug("Executing command: %s", cmd)
118 status, stdout, stderr = self.client.execute(cmd)
121 raise RuntimeError(stderr)
123 data = json.loads(stdout)
125 data['packets_received'] = self._iptables_get_result()
128 sent = data['packets_sent']
129 received = data['packets_received']
130 ppm = 1000000 * (sent - received) / sent
131 sla_max_ppm = int(args["sla"]["max_ppm"])
132 assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
139 '''internal test function'''
140 key_filename = pkg_resources.resource_filename('yardstick.resources',
141 'files/yardstick_key')
142 ctx = {'host': '172.16.0.137',
143 'target': '172.16.0.138',
145 'key_filename': key_filename
148 logger = logging.getLogger('yardstick')
149 logger.setLevel(logging.DEBUG)
153 options = {'packetsize': 120}
155 args = {'options': options,
156 'ipaddr': '192.168.111.31'}
160 if __name__ == '__main__':