9d9ed9b197f861ea1f8c1cbd855288d97d25f1ff
[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 module 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 += f"ip netns exec {args.namespace} " if args.namespace else u""
48                 cmd += f"iperf3 "
49                 cmd += f"--client {args.host} "
50                 cmd += f"--bind {args.bind} "
51                 if args.rate and args.frame_size:
52                     cmd += f"--bandwidth {bandwidth} "
53                     cmd += f"--length {iperf_frame_size} "
54                 cmd += f"--port {5201 + i} "
55                 cmd += f"--parallel {args.parallel} "
56                 cmd += f"--time {args.warmup_time} "
57                 if args.affinity:
58                     cmd += f"--affinity {args.affinity} "
59                 if args.udp:
60                     cmd += f"--udp "
61                 cmd += f"--zerocopy "
62                 cmd += f"--json"
63                 if1_process.append(
64                     subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
65                 )
66         finally:
67             for i in range(0, args.instances):
68                 if1, _ = if1_process[i].communicate(
69                     timeout=args.warmup_time + 60)
70
71     if1_process = []
72     if1_results = []
73     cmd = None
74
75     try:
76         if args.async_start:
77             args.duration += 999
78         for i in range(0, args.instances):
79             cmd = u"exec sudo "
80             cmd += f"ip netns exec {args.namespace} " if args.namespace else u""
81             cmd += f"iperf3 "
82             cmd += f"--client {args.host} "
83             cmd += f"--bind {args.bind} "
84             if args.rate and args.frame_size:
85                 cmd += f"--bandwidth {bandwidth} "
86                 cmd += f"--length {iperf_frame_size} "
87             cmd += f"--port {5201 + i} "
88             cmd += f"--parallel {args.parallel} "
89             cmd += f"--time {args.duration} "
90             if args.affinity:
91                 cmd += f"--affinity {args.affinity} "
92             if args.udp:
93                 cmd += f"--udp "
94             cmd += f"--zerocopy "
95             cmd += f"--json"
96             if1_process.append(
97                 subprocess.Popen([cmd], shell=True, stdout=subprocess.PIPE)
98             )
99     finally:
100         if args.async_start:
101             for i in range(0, args.instances):
102                 print(if1_process[i].pid)
103         else:
104             for i in range(0, args.instances):
105                 if1, _ = if1_process[i].communicate(timeout=args.duration + 60)
106                 if1_results.append(json.loads(if1))
107                 if1_results[i][u"end"][u"command"] = cmd
108                 print(f"{json.dumps(if1_results[i]['end'], indent = 4)}")
109
110
111 def main():
112     """Main function for the traffic generator using iPerf3.
113
114     It verifies the given command line arguments and runs "simple_burst"
115     function.
116     """
117     parser = argparse.ArgumentParser()
118     parser.add_argument(
119         u"--namespace", required=False, type=str,
120         help=u"Port netns name to run iPerf client on."
121     )
122     parser.add_argument(
123         u"--host", required=True, type=str,
124         help=u"Run in client mode, connecting to an iPerf server host."
125     )
126     parser.add_argument(
127         u"--bind", required=True, type=str,
128         help=u"Client bind IP address."
129     )
130     parser.add_argument(
131         u"--udp", action=u"store_true", default=False,
132         help=u"UDP test."
133     )
134     parser.add_argument(
135         u"--affinity", required=False, type=str,
136         help=u"Set the CPU affinity, if possible."
137     )
138     parser.add_argument(
139         u"--duration", required=True, type=float,
140         help=u"Duration of traffic run in seconds (-1=infinite)."
141     )
142     parser.add_argument(
143         u"--frame_size", required=False,
144         help=u"Size of a Frame without padding and IPG."
145     )
146     parser.add_argument(
147         u"--rate", required=False,
148         help=u"Traffic rate with included units (pps)."
149     )
150     parser.add_argument(
151         u"--traffic_directions", default=1, type=int,
152         help=u"Send bi- (2) or uni- (1) directional traffic."
153     )
154     parser.add_argument(
155         u"--warmup_time", type=float, default=5.0,
156         help=u"Traffic warm-up time in seconds, (0=disable)."
157     )
158     parser.add_argument(
159         u"--async_start", action=u"store_true", default=False,
160         help=u"Non-blocking call of the script."
161     )
162     parser.add_argument(
163         u"--instances", default=1, type=int,
164         help=u"The number of simultaneous client instances."
165     )
166     parser.add_argument(
167         u"--parallel", default=8, type=int,
168         help=u"The number of simultaneous client streams."
169     )
170
171     args = parser.parse_args()
172
173     # Currently limiting to uni- directional traffic.
174     if args.traffic_directions != 1:
175         print(f"Currently only uni- directional traffic is supported!")
176         sys.exit(1)
177
178     simple_burst(args)
179
180
181 if __name__ == u"__main__":
182     main()