Add Pktgen test step duration
[yardstick.git] / yardstick / benchmark / scenarios / networking / pktgen.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 import pkg_resources
10 import logging
11 import json
12
13 import yardstick.ssh as ssh
14 from yardstick.benchmark.scenarios import base
15
16 LOG = logging.getLogger(__name__)
17 LOG.setLevel(logging.DEBUG)
18
19
20 class Pktgen(base.Scenario):
21     """Execute pktgen between two hosts
22
23   Parameters
24     packetsize - packet size in bytes without the CRC
25         type:    int
26         unit:    bytes
27         default: 60
28     number_of_ports - number of UDP ports to test
29         type:    int
30         unit:    na
31         default: 10
32     duration - duration of the test
33         type:    int
34         unit:    seconds
35         default: 20
36     """
37     __scenario_type__ = "Pktgen"
38
39     TARGET_SCRIPT = 'pktgen_benchmark.bash'
40
41     def __init__(self, context):
42         self.context = context
43         self.setup_done = False
44
45     def setup(self):
46         '''scenario setup'''
47         self.target_script = pkg_resources.resource_filename(
48             'yardstick.benchmark.scenarios.networking',
49             Pktgen.TARGET_SCRIPT)
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')
54
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)
58
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)
62
63         # copy script to host
64         self.client.run("cat > ~/pktgen.sh",
65                         stdin=open(self.target_script, "rb"))
66
67         self.setup_done = True
68
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)
76         if status:
77             raise RuntimeError(stderr)
78
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)
86         if status:
87             raise RuntimeError(stderr)
88         return int(stdout)
89
90     def run(self, args):
91         """execute the benchmark"""
92
93         if not self.setup_done:
94             self.setup()
95
96         ipaddr = args.get("ipaddr", '127.0.0.1')
97
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)
105
106         if duration_time:
107             duration = duration_time
108         elif arithmetic_time:
109             duration = arithmetic_time
110         else:
111             duration = 20
112
113         self._iptables_setup()
114
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)
119
120         if status:
121             raise RuntimeError(stderr)
122
123         data = json.loads(stdout)
124
125         data['packets_received'] = self._iptables_get_result()
126
127         if "sla" in args:
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" \
133                 % (ppm, sla_max_ppm)
134
135         return data
136
137
138 def _test():
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',
144            'user': 'ubuntu',
145            'key_filename': key_filename
146            }
147
148     logger = logging.getLogger('yardstick')
149     logger.setLevel(logging.DEBUG)
150
151     p = Pktgen(ctx)
152
153     options = {'packetsize': 120}
154
155     args = {'options': options,
156             'ipaddr': '192.168.111.31'}
157     result = p.run(args)
158     print result
159
160 if __name__ == '__main__':
161     _test()