def _run_iperf3(self, results_buffer, iperf): ''' Runs the given iperf3 and writes the results into the given list buffer by appending the result to it Note: this driver function is specifically tailored to the purposes of traffic testing. Key differences from calling IPerf3.run(): - only uses the IPerf3 objects as containers - uses subprocess to run iperf3 with the given arguments - as a result, only picks some arguments that are expected to exist and ignores others This is done to enable multiple instances to run concurrently in threads without worrying about race conditions on stdout, as the iperf3 module's run() will reroute stdout to a private pipe. To avoid this, we use subprocess to give each iperf run its own private pipe. The only other option is to fork each child iperf into its own subprocess, which is not too different from this implementation. Args: results_buffer (list): the results buffer to which the iperf3.TestResult object to should be appended iperf (iperf3.IPerf3): the iperf3 object to run ''' # Constructing the subprocess call params = ('-B', iperf.bind_address, '-p', str(iperf.port), '-J') if ipaddress.ip_address(iperf.bind_address).version == 6: params += ('-6', ) if 'c' == iperf.role: params = ('-c', iperf.server_hostname) + params params += ('-b', str(iperf.bandwidth), '-t', str(iperf.duration)) # For ipv6 there is delay in configuring ipv6 address on eth3 # interface of test vm, so sleep for 5 secs if ipaddress.ip_address(iperf.bind_address).version == 6: time.sleep(5) if 'udp' == iperf.protocol: params += ('-u', ) else: params = ('-s', ) + params params += ('-1', ) params = ('iperf3', ) + params # Make the iperf3 call and spin off the subprocess self._server.log.debug('Running iperf3 command: %s', ' '.join(params)) with subprocess.Popen(params, stdout=subprocess.PIPE) as proc: result_str = proc.stdout.read().decode('utf-8') results_buffer += [iperf3.TestResult(result_str)] self._barrier.wait()
def test_result(self): dirname = os.path.dirname(os.path.abspath(__file__)) with open(os.path.join(dirname, 'results.json')) as f: json = f.read() result = iperf3.TestResult(json) assert result.sent_bps == 935992000 assert result.sent_kbps == 935992 assert result.sent_Mbps == 935.992 assert isclose(result.sent_kB_s, 114256.836, rel_tol=0.01) assert isclose(result.sent_MB_s, 111.579, rel_tol=0.01) assert result.received_bps == 934268000 assert result.received_kbps == 934268 assert result.received_Mbps == 934.268 assert isclose(result.received_kB_s, 114046.387, rel_tol=0.01) assert isclose(result.received_MB_s, 111.373, rel_tol=0.01)