FluentFTP icon indicating copy to clipboard operation
FluentFTP copied to clipboard

Slow UploadFileAsync when specifying SocketLocalIp.

Open maruyama-f4b1 opened this issue 3 years ago • 7 comments

FTP Server: Vsftpd

FluentFTP Version: 39.4.0

Framework: .NET 6

I'm developing in C# and got FluentFTP on NuGet.

When I did a one-time connect with SocketLocalIp and repeated UploadFileAsync, the results were much slower than expected. It was faster if I didn't specify the property. I have multiple IPs, and I absolutely have to specify the IP of the connection source. Why the slow transfer time?

maruyama-f4b1 avatar Sep 05 '22 06:09 maruyama-f4b1

Could it be that the interface associated with this specific IP is somehow detrimented: Slower, different routing, lower priority, more heavily used by other processes?

FanDjango avatar Sep 07 '22 18:09 FanDjango

It looks like you have a lot of forwarding sockets. I wrote a sample like below. This seems to depend on the interface state.

using FluentFTP;
using System.Net;
using System.Net.Sockets;

var client = new FtpClient();

client.Host = args[0];
client.Port = int.Parse(args[1]);

client.Credentials = new NetworkCredential(args[2], args[3]);

client.SocketKeepAlive = false;

client.ConnectTimeout = 5000;

var socketLocalIp = args[4];
client.InternetProtocolVersions =
    IPAddress.Parse(socketLocalIp).AddressFamily == AddressFamily.InterNetworkV6 ? FtpIpVersion.IPv6 : FtpIpVersion.IPv4;

client.SocketLocalIp = IPAddress.Parse(socketLocalIp);

var sw = new System.Diagnostics.Stopwatch();

sw.Start();

try
{
    client.Connect();
    for (int i = 0; i < 10000; i++)
    {
        await client.UploadFileAsync(args[5], $"/{i + 1:D03}{args[5]}", FtpRemoteExists.Overwrite, true, FtpVerify.None, null);
    }

}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    client.Disconnect();
}

sw.Stop();

var ts = sw.Elapsed;
Console.WriteLine($"{ts}");

// ---------------------------------------------------------------------

client = new FtpClient();

client.Host = args[0];
client.Port = int.Parse(args[1]);

client.Credentials = new NetworkCredential(args[2], args[3]);

client.SocketKeepAlive = false;

client.ConnectTimeout = 5000;

sw = new System.Diagnostics.Stopwatch();
sw.Start();

try
{
    client.Connect();
    for (int i = 0; i < 10000; i++)
    {
        await client.UploadFileAsync(args[5], $"/{i + 1:D03}{args[5]}", FtpRemoteExists.Overwrite, true, FtpVerify.None, null);
    }

}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
}
finally
{
    client.Disconnect();
}

sw.Stop();

ts = sw.Elapsed;
Console.WriteLine($"{ts}");

00:00:01.5912324
00:00:01.4241461

The measurement results were marginal. But, It looks like it also seems to perform poorly when using SocketLocalIp.

maruyama-f4b1 avatar Sep 09 '22 01:09 maruyama-f4b1

First, thanks for the code. I have used it to do my own tests.

I get 8.09 vs. 8.02.

My test file has a size of 1752 bytes, and I transfer 100 times. What is your file size?

Your results do not look like a terrible difference, can you explain better?

Why the slow transfer time?

1.59 vs. 1.42? Ok, for 10.000 transfers. 12%

It looks like you have a lot of forwarding sockets

I don't understand what you mean

FanDjango avatar Sep 09 '22 18:09 FanDjango

1:13:10 vs 1:06:04 on 1000 transfers. So I agree there is a difference.

FanDjango avatar Sep 09 '22 18:09 FanDjango

It looks like you have a lot of forwarding sockets

I don't understand what you mean

Bad translation sorry... ( ̄▽ ̄;)

My test file has a size of 1752 bytes, and I transfer 100 times. What is your file size?

I did it in 46626 bytes.

I wanted to mention that there is a difference between the two cases as the number of transfers increases. This happens even if only have one IP. It seems that specifying the IP in the SocketLocalIp itself is the key.

maruyama-f4b1 avatar Sep 09 '22 19:09 maruyama-f4b1

Ok, but let me ask you: Is this a problem for you and how do you think we should find out more? The difference seems not to be very big.

FanDjango avatar Sep 19 '22 17:09 FanDjango

I'm not sure if this is the problem. The real cause may be CPU, storage or network. The sample had little error. I'll try to see if I can show a larger case.

maruyama-f4b1 avatar Sep 21 '22 03:09 maruyama-f4b1

There may be a problem with the number of times send. Reversing the order in which the samples were run reversed the results. Will repeating FTP transfers be a problem?

maruyama-f4b1 avatar Sep 28 '22 06:09 maruyama-f4b1

Closed on account of no feedback

FanDjango avatar Oct 05 '22 21:10 FanDjango