Line length: Fix recent merges
[csit.git] / resources / tools / iperf / iperf_client.py
1 #!/usr/bin/python3
2
3 # Copyright (c) 2021 Cisco and/or its affiliates.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 """This script gets a bandwith limit together with other parameters, reads
17 the iPerf3 configuration and sends the traffic. At the end, it measures
18 the packet loss and latency.
19 """
20
21 import argparse
22 import json
23 import sys
24 import time
25 import subprocess
26
27 def simple_burst(args):
28     """Send traffic and measure throughput.
29
30     :param args: Named arguments from command line.
31     :type args: dict
32     """
33     if1_process = []
34     if1_results = []
35     cmd = None
36
37     if args.rate and args.frame_size:
38         iperf_frame_size = args.frame_size - 18
39         iperf_rate = float(args.rate)
40         bandwidth = \
41             int(args.frame_size) * float(iperf_rate) / args.instances
42
43     if args.warmup_time > 0:
44         try:
45             for i in range(0, args.instances):
46                 cmd = u"exec sudo "
47                 cmd += (
48                     f"ip netns exec {args.namespace} "
49                     if args.namespace else u""
50                 )
51                 cmd += f"iperf3 "
52                 cmd += f"--client {args.host} "
53                 cmd += f"--bind {args.bind} "
54                 if args.rate and args.frame_size:
55                     cmd += f"--bandwidth {bandwidth} "
56                     cmd += f"--length {iperf_frame_size} "
57                 cmd += f"--port {5201 + i} "
58                 cmd += f"--parallel {args.parallel} "
59                 cmd += f"--time {args.warmup_time} "
60                 if args.affinity:
61                     cmd += f"--affinity {args.affinity} "
62                 if args.udp:
63                     cmd += f"--udp "
64                 cmd += f"--zerocopy "
65                 cmd += f"--json"
66                 if1_process.append(
67                     subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
68                 )
69         finally:
70             for i in range(0, args.instances):
71                 if1, _ = if1_process[i].communicate(
72                     timeout=args.warmup_time + 60)
73
74     if1_process = []
75     if1_results = []
76     cmd = None
77
78     try:
79         if args.async_start:
80             args.duration += 999
81         for i in range(0, args.instances):
82             cmd = u"exec sudo "
83             cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
84             cmd += f"iperf3 "
85             cmd += f"--client {args.host} "
86             cmd += f"--bind {args.bind} "
87             if args.rate and args.frame_size:
88                 cmd += f"--bandwidth {bandwidth} "
89                 cmd += f"--length {iperf_frame_size} "
90             cmd += f"--port {5201 + i} "
91             cmd += f"--parallel {args.parallel} "
92             cmd += f"--time {args.duration} "
93             if args.affinity:
94                 cmd += f"--affinity {args.affinity} "
95             if args.udp:
96                 cmd += f"--udp "
97             cmd += f"--zerocopy "
98             cmd += f"--json"
99             if1_process.append(
100                 subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
101             )
102     finally:
103         if args.async_start:
104             for i in range(0, args.instances):
105                 print(if1_process[i].pid)
106         else:
107             for i in range(0, args.instances):
108                 if1, _ = if1_process[i].communicate(timeout=args.duration + 60)
109                 if1_results.append(json.loads(if1))
110                 if1_results[i][u"end"][u"command"] = cmd
111                 print(f"{json.dumps(if1_results[i]['end'], indent = 4)}")
112
113
114 def main():
115     """Main function for the traffic generator using iPerf3.
116
117     It verifies the given command line arguments and runs "simple_burst"
118     function.
119     """
120     parser = argparse.ArgumentParser()
121     parser.add_argument(
122         u"--namespace", required=False, type=str,
123         help=u"Port netns name to run iPerf client on."
124     )
125     parser.add_argument(
126         u"--host", required=True, type=str,
127         help=u"Run in client mode, connecting to an iPerf server host."
128     )
129     parser.add_argument(
130         u"--bind", required=True, type=str,
131         help=u"Client bind IP address."
132     )
133     parser.add_argument(
134         u"--udp", action=u"store_true", default=False,
135         help=u"UDP test."
136     )
137     parser.add_argument(
138         u"--affinity", required=False, type=str,
139         help=u"Set the CPU affinity, if possible."
140     )
141     parser.add_argument(
142         u"--duration", required=True, type=float,
143         help=u"Duration of traffic run in seconds (-1=infinite)."
144     )
145     parser.add_argument(
146         u"--frame_size", required=False,
147         help=u"Size of a Frame without padding and IPG."
148     )
149     parser.add_argument(
150         u"--rate", required=False,
151         help=u"Traffic rate with included units (pps)."
152     )
153     parser.add_argument(
154         u"--traffic_directions", default=1, type=int,
155         help=u"Send bi- (2) or uni- (1) directional traffic."
156     )
157     parser.add_argument(
158         u"--warmup_time", type=float, default=5.0,
159         help=u"Traffic warm-up time in seconds, (0=disable)."
160     )
161     parser.add_argument(
162         u"--async_start", action=u"store_true", default=False,
163         help=u"Non-blocking call of the script."
164     )
165     parser.add_argument(
166         u"--instances", default=1, type=int,
167         help=u"The number of simultaneous client instances."
168     )
169     parser.add_argument(
170         u"--parallel", default=8, type=int,
171         help=u"The number of simultaneous client streams."
172     )
173
174     args = parser.parse_args()
175
176     # Currently limiting to uni- directional traffic.
177     if args.traffic_directions != 1:
178         print(f"Currently only uni- directional traffic is supported!")
179         sys.exit(1)
180
181     simple_burst(args)
182
183
184 if __name__ == u"__main__":
185     main()