Skip to content

SftpClient: slow download #1331

Open
Open

Description

@goremykin

Hi! Thank you for this project, I appreciate your work.

I have a job in js that downloads some files over sftp every night using ssh2-sftp-client.
I decided to rewrite this job in C# using SSH.NET, but it turned out that the same files are now downloaded up to 15 times slower.
For example, a 25 MB file can be downloaded in 5 seconds using ssh2-sftp-client and in over a minute using SSH.NET.

I tested on ubuntu and arch linux in a simple .net 8 project. SSH.NET version - 2023.0.1.

using var sftpClient = new SftpClient(host, port, userName, password);
await using (var localFileStream = new FileStream("/someFile", FileMode.Create, FileAccess.Write))
{
    sftpClient.DownloadFile(file.FullName, localFileStream);
}

In case of reading from a stream it even slower:

await using var remoteStream = await sftpClient.OpenAsync("/someFile", FileMode.Open, FileAccess.Read, CancellationToken.None);
await remoteStream.CopyToAsync(localFileStream);

Do you have any ideas on what I should check or what settings I should play with?

Activity

  1. scott-xu commented on Feb 21, 2024

    @scott-xu
    Collaborator

    Not sure if it relates to compression. Do you know if your SSH service supports and only supports compression?
    Well, I don't think it relates to compression.

  2. Waynezhi commented on May 3, 2024

    @Waynezhi

    Same situation, using 2024.0.0, download speed is around 1MB/s only on a sftp server (protocol v3), and same environment with sftp command I got 15MB/s. @WojciechNagorski

  3. goremykin commented on May 7, 2024

    @goremykin
    Author

    Can confirm the same issue with 2024.0.0

  4. Waynezhi commented on May 7, 2024

    @Waynezhi

    I tried with 2024.0.0 and 2023.0.1, same result @goremykin, with .net 6
    #733
    #1122

    seems this fix does not work for me: #866

  5. Rob-Hague commented on May 7, 2024

    @Rob-Hague
    Collaborator

    There has been no recent investigation into SFTP performance that I know of. If you make an investigation, please do share your findings. Thanks

  6. TheGreenAirplane commented on Jul 30, 2024

    @TheGreenAirplane

    Hello, has there been any progress on this? I'm experiencing slow download with the SCP client, this could be related.

  7. WojciechNagorski commented on Jul 31, 2024

    @WojciechNagorski
    Collaborator

    @TheGreenAirplane I would be happy to review your PR that will solve this problem.

  8. megapro17 commented on Sep 9, 2024

    @megapro17

    I would be happy to review your PR that will solve this problem.

    You can't solve a problem without finding it's cause first

  9. scott-xu commented on Sep 9, 2024

    @scott-xu
    Collaborator

    I would be happy to review your PR that will solve this problem.

    You can't solve a problem without finding it's cause first

    Since this is an open source project, anyone can investigate the issue, find the root cause and propose a fix, including you 😄

  10. megapro17 commented on Sep 9, 2024

    @megapro17

    Library reads file more than ten times slower than sftp cli. Tried in release mode, no difference
    From profiling it spends most time on WaitOnHandle from RequestRead function

    var result = WaitHandle.WaitAny(waitHandles, millisecondsTimeout);

    I've modified a code to use RequestReadAsync and ReadAllBytesAsync but it didn't changed anything

    var client = new SftpClient("localhost", 8022, "megapro17", "1");
    client.Connect();
    //client.BufferSize = 9999999; // No difference
    Stopwatch sw = Stopwatch.StartNew();
    var file = await client.ReadAllBytesAsync(@"/sdcard/DCIM/Big file.mp4");
    sw.Stop();
    Console.WriteLine($"{sw.Elapsed.ToString()} size:{file.Length} speed:{file.Length/1024/1024/sw.Elapsed.TotalSeconds} MiB/s");
    
    //00:00:09.8068972 size:114186331 speed:11,012657499866522 MiB/s
  11. supplemarty commented on Sep 23, 2024

    @supplemarty

    Seeing terrible performance using 2024.1.0 reading 2.5GB file as stream; reading in 32k chunks using all defaults. On average about .5 MBPS vs 5.5 MBPS using WinSCP.Net for same file/environment streaming. Would love to move to SSH.Net for Azure Functions to be able to run on Linux but cannot with this perf. Is anyone looking into this or any advice on how to get better perf?

  12. jaredballingham commented on Oct 1, 2024

    @jaredballingham

    I am running into the same issue when dealing with a 25MB file. ReadAllBytes() is taking ~60s. Unsure if it helps you or actually addresses the underlying problem, but I found that you can call DownloadFile() into a MemoryStream. This still doesn't get me the performance that I would like to see (only 3-7x faster) and comes with some overhead, but it was still consistently faster. I did not do exhaustive testing by any means, but this approach was able to meet the urgent need I had.

    `
    var localStream = new MemoryStream();
    sftp.DownloadFile(filePath, localStream);
    _fileBytes = localStream.ToArray();

    vs

    _fileBytes = sftp.ReadAllBytes(filePath);
    `
    I normally don't post, but thought this work-around could help and maybe assist in diagnosing the root cause of the slowness.

  13. goremykin commented on Nov 14, 2024

    @goremykin
    Author

    Upgraded from Ubuntu 24.04 to 24.10 yesterday and now it is 2-3 times faster. Still slow, but much better now. And I have no idea why.

  14. bugproof commented on Nov 28, 2024

    @bugproof
  15. tmds commented on Dec 1, 2024

    @tmds

    I wrote a small program that downloads a 10MB file from a remote system using different clients. You can find the source here: https://gist.github.com/tmds/119a83fa9772c29934f848b49fe30f9a#file-measure-cs.

    It will output something like: https://gist.github.com/tmds/119a83fa9772c29934f848b49fe30f9a#file-example_output-txt.

    In my measurement I don't see the factor of magnitude difference that is reported here. Bandwidth vs latency (round-trip-time) can have a large effect on SFTP performance. In your setup you may see a large difference between different clients.

    Feel free to open up a ticket in the Tmds.Ssh repo if you can demonstrate a large difference against another client.

  16. goremykin commented on Dec 1, 2024

    @goremykin
    Author

    @tmds, What operating system did you use?

  17. tmds commented on Dec 2, 2024

    @tmds

    Linux on both ends.

    You can run the program in your own environment. At the top there are some variables that control the remote system to connect to.

  18. goremykin commented on Dec 5, 2024

    @goremykin
    Author

    @tmds, In my case the numbers are quite different. But Windows Server is installed on the server.
    Will try to switch to Tmds.Ssh, thank you!

    Client: Arch Linux, dotnet 8 Native AOT

    Tmds.Ssh                 3277.16 KB/s
    Renci.SshNet             782.05 KB/s
    --
    Tmds.Ssh                 2786.88 KB/s
    Renci.SshNet             793.22 KB/s
    --
    Tmds.Ssh                 3012.58 KB/s
    Renci.SshNet             770.53 KB/s
    
    
    ssh2-sftp-client (fastGet): 3451 KB/s.
    
  19. yzkzero commented on Mar 4, 2025

    @yzkzero

    Same issue on my side.

    Client: Window 10 + SSH.NET 2024.2.0 (build with .net framework 4.6.2)
    Server: Arm based embedded board running Linux 4.4

    Test for file upload:
    SSH.net is around 30% speed of other tools e.g. WinSCP or pscp.exe (~15Mbps vs ~45Mbps)

  20. Rayzbam commented on May 13, 2025

    @Rayzbam

    Any update on this issue ?
    Can someone explain the performance gap between a synchronous download and an asynchronous download ? (in favor of the synchronous one, by far as i witnessed)

  21. yzkzero commented on May 22, 2025

    @yzkzero

    I solved the issue by replacing SftpClient with ScpClient. The upload performance is tripled (from 15Mbps -> 45Mbps). Download was also boosted to 50Mbps

    So it seems the issue on my side is related to the protocol itself (sftp vs scp). Not SSH.NET implementation.

    I also tried pscp.exe, it supports explictly selecting sftp or scp protocol by passing argument. It behaves the same. Scp protocol is much faster than sftp protocol for file transferring.

  22. tmds commented on May 22, 2025

    @tmds

    With SCP the file gets sent as a whole, while with SFTP it requires making several requests (because requests are limited in data per request). It's a challenge to use SFTP so it performs well because the implementation needs mitigate the impact of round-trip delays through pipelining the requests.

    OpenSSH switched their scp tool to use SFTP protocol instead of SCP protocol by default about 3 years ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions