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__)
19 class Pktgen(base.Scenario):
20 """Execute pktgen between two hosts
23 packetsize - packet size in bytes without the CRC
27 number_of_ports - number of UDP ports to test
31 duration - duration of the test
36 __scenario_type__ = "Pktgen"
38 TARGET_SCRIPT = 'pktgen_benchmark.bash'
40 def __init__(self, context):
41 self.context = context
42 self.setup_done = False
46 self.target_script = pkg_resources.resource_filename(
47 'yardstick.benchmark.scenarios.networking',
49 user = self.context.get('user', 'ubuntu')
50 host = self.context.get('host', None)
51 target = self.context.get('target', None)
52 key_filename = self.context.get('key_filename', '~/.ssh/id_rsa')
54 LOG.info("user:%s, target:%s", user, target)
55 self.server = ssh.SSH(user, target, key_filename=key_filename)
56 self.server.wait(timeout=600)
58 LOG.info("user:%s, host:%s", user, host)
59 self.client = ssh.SSH(user, host, key_filename=key_filename)
60 self.client.wait(timeout=600)
63 self.client.run("cat > ~/pktgen.sh",
64 stdin=open(self.target_script, "rb"))
66 self.setup_done = True
68 def _iptables_setup(self):
69 """Setup iptables on server to monitor for received packets"""
70 cmd = "sudo iptables -F; " \
71 "sudo iptables -A INPUT -p udp --dport 1000:%s -j DROP" \
72 % (1000 + self.number_of_ports)
73 LOG.debug("Executing command: %s", cmd)
74 status, _, stderr = self.server.execute(cmd)
76 raise RuntimeError(stderr)
78 def _iptables_get_result(self):
79 """Get packet statistics from server"""
80 cmd = "sudo iptables -L INPUT -vnx |" \
81 "awk '/dpts:1000:%s/ {{printf \"%%s\", $1}}'" \
82 % (1000 + self.number_of_ports)
83 LOG.debug("Executing command: %s", cmd)
84 status, stdout, stderr = self.server.execute(cmd)
86 raise RuntimeError(stderr)
90 """execute the benchmark"""
92 if not self.setup_done:
95 ipaddr = args.get("ipaddr", '127.0.0.1')
97 options = args['options']
98 packetsize = options.get("packetsize", 60)
99 self.number_of_ports = options.get("number_of_ports", 10)
100 # if run by a duration runner
101 duration_time = self.context.get("duration", None)
102 # if run by an arithmetic runner
103 arithmetic_time = options.get("duration", None)
106 duration = duration_time
107 elif arithmetic_time:
108 duration = arithmetic_time
112 self._iptables_setup()
114 cmd = "sudo bash pktgen.sh %s %s %s %s" \
115 % (ipaddr, self.number_of_ports, packetsize, duration)
116 LOG.debug("Executing command: %s", cmd)
117 status, stdout, stderr = self.client.execute(cmd)
120 raise RuntimeError(stderr)
122 data = json.loads(stdout)
124 data['packets_received'] = self._iptables_get_result()
127 sent = data['packets_sent']
128 received = data['packets_received']
129 ppm = 1000000 * (sent - received) / sent
130 sla_max_ppm = int(args["sla"]["max_ppm"])
131 assert ppm <= sla_max_ppm, "ppm %d > sla_max_ppm %d" \
138 '''internal test function'''
139 key_filename = pkg_resources.resource_filename('yardstick.resources',
140 'files/yardstick_key')
141 ctx = {'host': '172.16.0.137',
142 'target': '172.16.0.138',
144 'key_filename': key_filename
147 logger = logging.getLogger('yardstick')
148 logger.setLevel(logging.DEBUG)
152 options = {'packetsize': 120}
154 args = {'options': options,
155 'ipaddr': '192.168.111.31'}
159 if __name__ == '__main__':