SSH.NET icon indicating copy to clipboard operation
SSH.NET copied to clipboard

SFTP performance

Open lochnar187 opened this issue 10 years ago • 56 comments

I am seeing some performance issues and would like to ask others to take my test code and see if they have the same problem. Mostly, this is because I am running this against an SFTP server that I don't control or have any knowledge about, it's "black box" to me. So I want to make sure it's not that.

After noticing I wasn't getting the speeds I expected (sometimes as little as half) I wrote some code that gives a head to head comparison of speeds between SSH.NET and WinSCP. I used NuGet to get the latest versions of both SSH.NET and WinSCP. Testing file is an 834KB file.

Using VS 2015 Code build is targeted on .NET Framework 4.5.2 OS is Windows 10 (dev machine) and Windows Server 2012 R2 (test machine)

SSH.NET version, 2016.0.0 WinSCP version, 5.9.2

SFTP server reports it is: SSH-2.0-WS_FTP-SSH_7.6.3

Test Code:

using Renci.SshNet;
using System;
using System.Diagnostics;
using System.IO;
using WinSCP;

namespace SFTP_Compare {
    class Program {
        static string strServer     = "xxxx";
        static string strUser       = "xxxx";
        static string strPass       = "xxxx";
        static string strHostKey    = "ssh-rsa 1024 xxxxxx";
        static string strRmPath     = "/home/";
        static string strLcPath     = "C:\\Data\\";
        static string strFile       = "test_file.xyz";
        static int    intPort       = 22;
        static void TestWinSCP(int pintLoops) {
            long    lngByteKnt  = 0;
            double  dblCreated, dblConnected, dblOpenStream, dblDownloaded, dblEndTime;
            double  dblKBKntTotal = 0.0, dblCreatedTotal = 0.0, dblConnectedTotal = 0.0, dblOpenStreamTotal = 0.0, dblDownloadedTotal = 0.0, dblEndTimeTotal = 0.0;
            Stopwatch swTimer = new Stopwatch();

            Console.WriteLine();
            Console.WriteLine("WinSCP download test, {0} iteration(s).", pintLoops);
            Console.WriteLine("___________________________________________________________________");
            Console.WriteLine("|  Run  |  Client Cr |  Connection  |    Download    |   Delete   |");

            for (int i = 0; i < pintLoops; i++) {
                try {
                    swTimer.Start();
                    SessionOptions soptSettings = new SessionOptions();
                    soptSettings.Protocol = Protocol.Sftp;
                    soptSettings.HostName = strServer;
                    soptSettings.UserName = strUser;
                    soptSettings.Password = strPass;
                    soptSettings.SshHostKeyFingerprint = strHostKey;
                    using (WinSCP.Session sesConnection = new WinSCP.Session()) {
                        swTimer.Stop();
                        dblCreated = swTimer.Elapsed.TotalMilliseconds;
                        swTimer.Restart();
                        sesConnection.Open(soptSettings);
                        swTimer.Stop();
                        dblConnected = swTimer.Elapsed.TotalMilliseconds;
                        swTimer.Restart();
                        dblOpenStream = 0;
                        TransferOptions toptStreamSettings = new TransferOptions();
                        toptStreamSettings.TransferMode = TransferMode.Binary;
                        sesConnection.GetFiles(strRmPath + strFile, strLcPath + strFile, false, toptStreamSettings);
                        swTimer.Stop();
                        dblDownloaded = swTimer.Elapsed.TotalMilliseconds;
                        FileInfo fiFile = new FileInfo(strLcPath + strFile);
                        lngByteKnt = fiFile.Length;
                        swTimer.Restart();
                    }
                    File.Delete(strLcPath + strFile); // Delete local copy for next run...WRG
                    swTimer.Stop();
                    dblEndTime = swTimer.Elapsed.TotalMilliseconds;

                    // Output test results...WRG
                    Console.WriteLine("|{0,4}   |{1,8:N1} ms |{2,10:N1} ms |{3,12:N1} ms |{4,8:N1} ms |", i + 1, dblCreated, dblConnected, dblDownloaded, dblEndTime);

                    // Save totals for later...WRG
                    dblKBKntTotal += (lngByteKnt / 1024);
                    dblCreatedTotal += dblCreated;
                    dblConnectedTotal += dblConnected;
                    dblOpenStreamTotal += dblOpenStream;
                    dblDownloadedTotal += (dblDownloaded);
                    dblEndTimeTotal += dblEndTime;

                } catch (Exception ex) {
                    Console.WriteLine(" *** Error on run {0}, Message: {1}", i + 1, ex.Message);
                }
            }
            Console.WriteLine("|=======|============|==============|================|============|");
            Console.WriteLine("|  avg  |{0,8:N1} ms |{1,10:N1} ms |{2,12:N1} ms |{3,8:N1} ms |", (dblCreatedTotal / pintLoops), (dblConnectedTotal / pintLoops), (dblDownloadedTotal / pintLoops), (dblEndTimeTotal / pintLoops));
            Console.WriteLine("|_______|____________|______________|________________|____________|");
            Console.WriteLine(" *** Average download speed for {0:N0} bytes: {1,5:N2} KB/s", (dblKBKntTotal * 1024), (dblKBKntTotal / (dblDownloadedTotal / 1000)));
            Console.WriteLine();
        }
        static void TestSSHNET(int pintLoops) {
            long    lngByteKnt  = 0;
            double  dblCreated, dblConnected, dblOpenStream, dblDownloaded, dblEndTime;
            double  dblKBKntTotal = 0.0, dblCreatedTotal = 0.0, dblConnectedTotal = 0.0, dblOpenStreamTotal = 0.0, dblDownloadedTotal = 0.0, dblEndTimeTotal = 0.0;
            Stopwatch swTimer = new Stopwatch();

            Console.WriteLine();
            Console.WriteLine("SSH.NET download test, {0} iteration(s).", pintLoops);
            Console.WriteLine("___________________________________________________________________");
            Console.WriteLine("|  Run  |  Client Cr |  Connection  |    Download    |   Delete   |");

            for (int i = 0; i < pintLoops; i++) {
                try {
                    swTimer.Start();
                    using (SftpClient sftpClient = new SftpClient(strServer, intPort, strUser, strPass)) {
                        swTimer.Stop();
                        dblCreated = swTimer.Elapsed.TotalMilliseconds;
                        swTimer.Restart();
                        sftpClient.Connect();
                        swTimer.Stop();
                        dblConnected = swTimer.Elapsed.TotalMilliseconds;
                        swTimer.Restart();
                        using (Stream sFile = File.Open(strLcPath + strFile, FileMode.Create, FileAccess.Write)) {
                            dblOpenStream = 0;
                            //swTimer.Stop();
                            //dblOpenStream = swTimer.Elapsed.TotalMilliseconds;
                            //swTimer.Restart();
                            sftpClient.DownloadFile(strRmPath + strFile, sFile);
                            swTimer.Stop();
                            dblDownloaded = swTimer.Elapsed.TotalMilliseconds;
                            lngByteKnt = sFile.Length;
                            swTimer.Restart();
                        }
                    }
                    File.Delete(strLcPath + strFile); // Delete local copy for next run...WRG
                    swTimer.Stop();
                    dblEndTime = swTimer.Elapsed.TotalMilliseconds;

                    // Output test results...WRG
                    Console.WriteLine("|{0,4}   |{1,8:N1} ms |{2,10:N1} ms |{3,12:N1} ms |{4,8:N1} ms |", i+1, dblCreated, dblConnected, dblDownloaded, dblEndTime);

                    // Save totals for later...WRG
                    dblKBKntTotal += (lngByteKnt/1024);
                    dblCreatedTotal += dblCreated;
                    dblConnectedTotal += dblConnected;
                    dblOpenStreamTotal += dblOpenStream;
                    dblDownloadedTotal += (dblDownloaded);
                    dblEndTimeTotal += dblEndTime;

                } catch (Exception ex) {
                    Console.WriteLine(" *** Error on run {0}, Message: {1}", i+1, ex.Message);
                }
            }
            Console.WriteLine("|=======|============|==============|================|============|");
            Console.WriteLine("|  avg  |{0,8:N1} ms |{1,10:N1} ms |{2,12:N1} ms |{3,8:N1} ms |", (dblCreatedTotal / pintLoops), (dblConnectedTotal / pintLoops), (dblDownloadedTotal / pintLoops), (dblEndTimeTotal / pintLoops));
            Console.WriteLine("|_______|____________|______________|________________|____________|");
            Console.WriteLine(" *** Average download speed for {0:N0} bytes: {1,5:N2} KB/s", (dblKBKntTotal * 1024), (dblKBKntTotal / (dblDownloadedTotal / 1000)));
            Console.WriteLine();
        }

        static void Main(string[] args) {
            TestWinSCP(50);
            TestSSHNET(50);
        }
    }
}

Output from command prompt session on test machine:

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Windows\system32>cd C:\Data

C:\Data>dir
 Volume in drive C has no label.
 Volume Serial Number is E6AE-D23F

 Directory of C:\Data

10/11/2016  01:03 PM    <DIR>          .
10/11/2016  01:03 PM    <DIR>          ..
10/06/2016  07:21 PM           408,064 Renci.SshNet.dll
10/06/2016  07:21 PM           961,463 Renci.SshNet.xml
10/11/2016  01:02 PM             9,216 SFTP_Compare.exe
10/06/2016  07:18 PM               189 SFTP_Compare.exe.config
10/11/2016  01:02 PM            15,872 SFTP_Compare.pdb
10/10/2016  03:03 PM            22,696 SFTP_Compare.vshost.exe
10/06/2016  07:18 PM               189 SFTP_Compare.vshost.exe.config
07/10/2015  07:01 AM               490 SFTP_Compare.vshost.exe.manifest
10/06/2016  07:22 PM        18,886,048 WinSCP.exe
10/08/2016  02:48 PM                75 winscp.ini
10/06/2016  07:22 PM           149,584 WinSCPnet.dll
              11 File(s)     20,453,886 bytes
               2 Dir(s)  16,536,723,456 bytes free

C:\Data>sftp_compare

WinSCP download test, 50 iteration(s).
___________________________________________________________________
|  Run  |  Client Cr |  Connection  |    Download    |   Delete   |
|   1   |     4.2 ms |   1,797.8 ms |       781.9 ms |   110.5 ms |
|   2   |   110.6 ms |   1,778.7 ms |       843.9 ms |   110.3 ms |
|   3   |   110.4 ms |   1,779.8 ms |       843.7 ms |   110.4 ms |
|   4   |   110.4 ms |   1,779.9 ms |       843.8 ms |   110.4 ms |
|   5   |   110.4 ms |   1,779.9 ms |       843.8 ms |   110.5 ms |
|   6   |   110.5 ms |   1,779.8 ms |       843.7 ms |   110.3 ms |
|   7   |   110.4 ms |   1,780.0 ms |       843.8 ms |   110.3 ms |
|   8   |   110.3 ms |   2,592.7 ms |       843.8 ms |   110.6 ms |
|   9   |   110.7 ms |   2,582.1 ms |       843.6 ms |   110.2 ms |
|  10   |   110.3 ms |   1,782.1 ms |     1,656.3 ms |   110.6 ms |
|  11   |   110.7 ms |   1,779.4 ms |       843.9 ms |   110.4 ms |
|  12   |   110.4 ms |   2,592.2 ms |       843.8 ms |   110.5 ms |
|  13   |   110.6 ms |   1,779.7 ms |       843.9 ms |   110.6 ms |
|  14   |   110.7 ms |   1,779.4 ms |     1,656.4 ms |   110.2 ms |
|  15   |   110.3 ms |   2,592.3 ms |       843.7 ms |   110.6 ms |
|  16   |   110.7 ms |   2,592.1 ms |       843.9 ms |   110.2 ms |
|  17   |   110.3 ms |   1,779.9 ms |       843.8 ms |   110.6 ms |
|  18   |   110.7 ms |   1,779.7 ms |       843.8 ms |   110.6 ms |
|  19   |   110.7 ms |   2,591.9 ms |       843.9 ms |   110.4 ms |
|  20   |   110.5 ms |   1,779.5 ms |       846.6 ms |   110.5 ms |
|  21   |   110.5 ms |   2,593.7 ms |       844.8 ms |   110.2 ms |
|  22   |   110.3 ms |   1,780.0 ms |       843.7 ms |   113.8 ms |
|  23   |   113.9 ms |   2,590.6 ms |       843.8 ms |   110.5 ms |
|  24   |   110.6 ms |   2,582.5 ms |       843.7 ms |   110.6 ms |
|  25   |   110.7 ms |   1,780.0 ms |       843.8 ms |   110.6 ms |
|  26   |   110.6 ms |   1,779.7 ms |       843.8 ms |   110.4 ms |
|  27   |   110.5 ms |   2,592.1 ms |       843.9 ms |   110.2 ms |
|  28   |   110.2 ms |   1,780.8 ms |     1,656.3 ms |   110.3 ms |
|  29   |   110.4 ms |   1,779.8 ms |       843.9 ms |   110.5 ms |
|  30   |   110.5 ms |   1,779.8 ms |       843.7 ms |   110.6 ms |
|  31   |   110.7 ms |   1,779.6 ms |       843.9 ms |   110.7 ms |
|  32   |   110.8 ms |   1,779.1 ms |       843.9 ms |   110.5 ms |
|  33   |   110.6 ms |   2,592.0 ms |       843.7 ms |   111.1 ms |
|  34   |   111.2 ms |   2,592.5 ms |       843.8 ms |   110.0 ms |
|  35   |   110.1 ms |   1,781.4 ms |       843.6 ms |   110.4 ms |
|  36   |   110.5 ms |   2,592.9 ms |       843.8 ms |   110.2 ms |
|  37   |   110.3 ms |   1,780.0 ms |       843.7 ms |   110.2 ms |
|  38   |   110.3 ms |   1,776.1 ms |       843.7 ms |   110.6 ms |
|  39   |   110.7 ms |   2,592.2 ms |       843.7 ms |   110.3 ms |
|  40   |   110.4 ms |   1,780.0 ms |       843.7 ms |   110.4 ms |
|  41   |   110.4 ms |   2,592.9 ms |       843.8 ms |   110.3 ms |
|  42   |   110.3 ms |   1,780.4 ms |       843.9 ms |   110.4 ms |
|  43   |   110.5 ms |   2,592.2 ms |       843.8 ms |   110.5 ms |
|  44   |   110.6 ms |   2,592.3 ms |       843.7 ms |   102.5 ms |
|  45   |   102.6 ms |   1,779.5 ms |       843.7 ms |   110.3 ms |
|  46   |   110.4 ms |   2,593.1 ms |     1,657.7 ms |   110.3 ms |
|  47   |   110.4 ms |   1,779.6 ms |       843.8 ms |   110.5 ms |
|  48   |   110.6 ms |   2,582.8 ms |       843.8 ms |   110.1 ms |
|  49   |   110.2 ms |   1,779.6 ms |       843.7 ms |   110.3 ms |
|  50   |   110.4 ms |   1,781.4 ms |       843.8 ms |   110.5 ms |
|=======|============|==============|================|============|
|  avg  |   108.3 ms |   2,088.4 ms |       907.7 ms |   110.3 ms |
|_______|____________|______________|________________|____________|
 *** Average download speed for 42,700,800 bytes: 918.86 KB/s


SSH.NET download test, 50 iteration(s).
___________________________________________________________________
|  Run  |  Client Cr |  Connection  |    Download    |   Delete   |
|   1   |    11.3 ms |     974.3 ms |     1,337.5 ms |   190.6 ms |
|   2   |   190.6 ms |     815.4 ms |     1,323.7 ms |   195.5 ms |
|   3   |   195.5 ms |   1,365.0 ms |     1,452.2 ms |   190.0 ms |
|   4   |   190.0 ms |   1,577.7 ms |     1,516.7 ms |   202.2 ms |
|   5   |   202.3 ms |   1,197.2 ms |     1,562.5 ms |   192.6 ms |
|   6   |   192.7 ms |   1,194.1 ms |     1,584.5 ms |   189.3 ms |
|   7   |   189.3 ms |   1,311.7 ms |     1,609.4 ms |   198.8 ms |
|   8   |   198.8 ms |   1,477.7 ms |     1,427.2 ms |   188.1 ms |
|   9   |   188.2 ms |   1,417.4 ms |     1,561.7 ms |   201.3 ms |
|  10   |   201.3 ms |   1,401.8 ms |     1,926.7 ms |   194.3 ms |
|  11   |   194.3 ms |   1,401.4 ms |     1,599.1 ms |   190.2 ms |
|  12   |   190.3 ms |   1,528.9 ms |     1,607.8 ms |   197.8 ms |
|  13   |   197.9 ms |   1,338.8 ms |     1,770.6 ms |   196.8 ms |
|  14   |   196.9 ms |   1,833.7 ms |     1,683.3 ms |   201.9 ms |
|  15   |   201.9 ms |   1,414.5 ms |     1,405.0 ms |   195.4 ms |
|  16   |   195.4 ms |   1,197.0 ms |     1,465.8 ms |   216.8 ms |
|  17   |   216.9 ms |     854.8 ms |     1,282.3 ms |   192.2 ms |
|  18   |   192.2 ms |   1,121.3 ms |     1,356.2 ms |   200.7 ms |
|  19   |   200.7 ms |   1,166.9 ms |     1,321.3 ms |   193.4 ms |
|  20   |   193.4 ms |   1,147.5 ms |     1,341.7 ms |   202.1 ms |
|  21   |   202.1 ms |   1,165.9 ms |     1,549.1 ms |   202.5 ms |
|  22   |   202.5 ms |   1,157.9 ms |     1,469.9 ms |   202.1 ms |
|  23   |   202.2 ms |   1,420.7 ms |     1,295.2 ms |   189.1 ms |
|  24   |   189.2 ms |   1,191.0 ms |     1,428.3 ms |   193.2 ms |
|  25   |   193.2 ms |   1,251.3 ms |     2,123.6 ms |   190.8 ms |
|  26   |   190.8 ms |   2,066.9 ms |     1,852.7 ms |   190.1 ms |
|  27   |   190.1 ms |   1,295.0 ms |     1,322.2 ms |   198.5 ms |
|  28   |   198.5 ms |   1,256.5 ms |     1,364.1 ms |   190.4 ms |
|  29   |   190.4 ms |   1,338.1 ms |     1,509.6 ms |   188.2 ms |
|  30   |   188.3 ms |     863.8 ms |     1,268.3 ms |   200.8 ms |
|  31   |   200.8 ms |     830.0 ms |     1,263.6 ms |   199.9 ms |
|  32   |   199.9 ms |   1,245.4 ms |     1,709.0 ms |   199.5 ms |
|  33   |   199.5 ms |   1,316.6 ms |     1,409.2 ms |   200.2 ms |
|  34   |   200.3 ms |   1,367.3 ms |     1,335.6 ms |   201.4 ms |
|  35   |   201.4 ms |   1,309.9 ms |     1,434.1 ms |   195.3 ms |
|  36   |   195.3 ms |     855.0 ms |     1,299.7 ms |   188.2 ms |
|  37   |   188.2 ms |   1,400.1 ms |     1,395.0 ms |   188.3 ms |
|  38   |   188.3 ms |   1,339.3 ms |     1,466.6 ms |   199.9 ms |
|  39   |   199.9 ms |   1,604.9 ms |     1,511.3 ms |   256.2 ms |
|  40   |   256.3 ms |   1,414.4 ms |     1,353.6 ms |   186.9 ms |
|  41   |   186.9 ms |   1,393.3 ms |     1,414.8 ms |   196.7 ms |
|  42   |   196.7 ms |     889.2 ms |     1,269.1 ms |   196.9 ms |
|  43   |   197.0 ms |     809.1 ms |     1,397.1 ms |   199.6 ms |
|  44   |   199.6 ms |   1,305.2 ms |     1,567.5 ms |   188.3 ms |
|  45   |   188.3 ms |     882.4 ms |     1,376.4 ms |   201.8 ms |
|  46   |   201.9 ms |   1,276.5 ms |     1,633.0 ms |   218.6 ms |
|  47   |   218.7 ms |   1,211.7 ms |     1,462.9 ms |   197.9 ms |
|  48   |   197.9 ms |     783.3 ms |     1,288.4 ms |   193.7 ms |
|  49   |   193.8 ms |   1,333.6 ms |     1,439.8 ms |   201.3 ms |
|  50   |   201.3 ms |   1,171.2 ms |     1,374.2 ms |   191.5 ms |
|=======|============|==============|================|============|
|  avg  |   194.0 ms |   1,249.7 ms |     1,474.4 ms |   197.6 ms |
|_______|____________|______________|________________|____________|
 *** Average download speed for 42,700,800 bytes: 565.66 KB/s


C:\Data>

I'd welcome any suggestions to improve performance.

Thanks

lochnar187 avatar Oct 11 '16 19:10 lochnar187

15 seems to be a sweet spot as far as read-aheads go for my environment, and with a specific server. There’s not a truly significant difference though over 10. Going above 15, however, tends to actually slow down the transfers.

I think I can be content with the value of 10 that is present currently.

On Feb 7, 2017, at 7:08 PM, Gert Driesen [email protected] wrote:

There's almost no reason to play around with the buffer size. As I mentioned earlier, 32 KB is a good default.

On my system, with a local (and responsive) SSH server, there's almost no difference between 2 and 10 read-aheads. I guess 10 was the last value that I tested with; there was no million dollar usage study to get to that value :-)

Please do some tests with values higher than 10, and let me know if that changes much for you.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sshnet/SSH.NET/issues/100#issuecomment-278186443, or mute the thread https://github.com/notifications/unsubscribe-auth/ADQXsYPG3iS48E-OkXCvcPISt0BXMwIxks5raQdsgaJpZM4KUBlH.

imapangolin avatar Feb 10 '17 12:02 imapangolin

I did some further tests and attempts to improve transfer rate, but I failed to make a notable difference. Putty is still quite a lot faster.

I'm gonna try to see if using the crypto classes from the .NET base class library makes a difference. But to be honest, I'm not a crypto wizard and have no ambition to become one :)

drieseng avatar Feb 11 '17 10:02 drieseng

Before making the transfer more robust you were faster. I wonder if there's something there that can be done.

On Feb 11, 2017, at 5:03 AM, Gert Driesen [email protected] wrote:

I did some further tests and attempt to improve transfer rate, but I failed to make a difference. Putty is still quite a lot faster.

I'm gonna try to see if using the crypto classes from the .NET base class library makes a difference. But to be honest, I'm not a crypto wizard and have no ambition to become one :)

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

imapangolin avatar Feb 11 '17 19:02 imapangolin

Please try using version 2016.1.0-beta2, and let me know if performance is good now.

drieseng avatar Aug 16 '17 20:08 drieseng

Did a quick run with 2016-1.0-beta2, I'm seeing a major boost! The speed almost doubled. Here's the results, same test program as above.

C:\Data>dir
 Volume in drive C has no label.
 Volume Serial Number is E6AE-D23F

 Directory of C:\Data

08/16/2017  06:18 PM    <DIR>          .
08/16/2017  06:18 PM    <DIR>          ..
08/16/2017  06:07 PM           422,912 Renci.SshNet.dll
01/27/2017  06:26 PM         1,502,720 Renci.SshNet.pdb
08/16/2017  06:07 PM         1,008,622 Renci.SshNet.xml
01/27/2017  06:59 PM             9,216 SFTP_Compare.exe
10/06/2016  07:18 PM               189 SFTP_Compare.exe.config
01/27/2017  06:59 PM            15,872 SFTP_Compare.pdb
08/16/2017  06:09 PM            22,696 SFTP_Compare.vshost.exe
10/06/2016  07:18 PM               189 SFTP_Compare.vshost.exe.config
07/10/2015  07:01 AM               490 SFTP_Compare.vshost.exe.manifest
10/06/2016  07:22 PM        18,886,048 WinSCP.exe
10/08/2016  02:48 PM                75 winscp.ini
10/06/2016  07:22 PM           149,584 WinSCPnet.dll
              18 File(s)     26,195,039 bytes
               2 Dir(s)   9,880,752,128 bytes free

C:\Data>sftp_compare

WinSCP download test, 50 iteration(s).
___________________________________________________________________
|  Run  |  Client Cr |  Connection  |    Download    |   Delete   |
|   1   |     4.5 ms |   1,806.8 ms |       784.8 ms |   113.1 ms |
|   2   |   113.2 ms |   1,775.5 ms |       843.7 ms |   110.6 ms |
|   3   |   110.7 ms |   1,779.3 ms |       843.7 ms |   111.7 ms |
|   4   |   111.8 ms |   1,782.3 ms |       843.8 ms |   111.5 ms |
|   5   |   111.6 ms |   1,777.8 ms |       843.7 ms |   110.5 ms |
|   6   |   110.5 ms |   1,779.4 ms |       843.8 ms |   110.3 ms |
|   7   |   110.4 ms |   1,779.5 ms |       843.8 ms |   110.5 ms |
|   8   |   110.6 ms |   1,779.4 ms |       843.8 ms |   110.5 ms |
|   9   |   110.6 ms |   1,778.8 ms |       843.7 ms |   110.6 ms |
|  10   |   110.7 ms |   1,779.0 ms |       843.8 ms |   111.4 ms |
|  11   |   111.5 ms |   1,778.7 ms |       843.8 ms |   110.5 ms |
|  12   |   110.6 ms |   1,779.0 ms |       843.9 ms |   110.5 ms |
|  13   |   110.7 ms |   1,778.8 ms |       849.4 ms |   110.3 ms |
|  14   |   110.4 ms |   1,779.2 ms |       843.9 ms |   111.0 ms |
|  15   |   111.1 ms |   1,778.5 ms |       845.3 ms |   110.5 ms |
|  16   |   110.6 ms |   1,779.8 ms |       843.8 ms |   110.8 ms |
|  17   |   111.0 ms |   1,779.1 ms |       843.7 ms |   110.3 ms |
|  18   |   110.4 ms |   1,779.5 ms |       843.9 ms |   110.5 ms |
|  19   |   110.5 ms |   1,779.2 ms |       843.8 ms |   110.6 ms |
|  20   |   110.6 ms |   1,779.2 ms |       843.7 ms |   110.3 ms |
|  21   |   110.4 ms |     967.0 ms |       843.8 ms |   110.4 ms |
|  22   |   110.5 ms |     966.9 ms |       843.9 ms |   111.1 ms |
|  23   |   111.3 ms |   1,778.3 ms |       844.5 ms |   107.3 ms |
|  24   |   107.4 ms |   1,779.0 ms |       843.8 ms |   110.1 ms |
|  25   |   110.2 ms |   1,779.0 ms |       843.8 ms |   110.3 ms |
|  26   |   110.4 ms |   1,779.5 ms |       843.7 ms |   110.6 ms |
|  27   |   110.6 ms |   1,779.4 ms |       844.8 ms |   110.2 ms |
|  28   |   110.3 ms |   1,779.6 ms |       843.8 ms |   111.3 ms |
|  29   |   111.4 ms |   1,778.5 ms |     1,656.3 ms |   110.2 ms |
|  30   |   110.2 ms |   3,408.8 ms |     1,656.2 ms |   110.3 ms |
|  31   |   110.4 ms |   1,779.2 ms |       844.7 ms |   110.4 ms |
|  32   |   110.5 ms |   1,779.3 ms |       843.8 ms |   111.6 ms |
|  33   |   111.8 ms |   1,780.6 ms |     2,469.2 ms |   111.5 ms |
|  34   |   111.6 ms |   1,777.9 ms |       843.8 ms |   110.4 ms |
|  35   |   110.5 ms |   1,779.3 ms |       845.8 ms |   110.6 ms |
|  36   |   110.7 ms |   1,778.8 ms |       843.9 ms |   110.2 ms |
|  37   |   110.3 ms |   1,779.4 ms |       843.8 ms |   110.4 ms |
|  38   |   110.5 ms |   1,779.0 ms |       843.9 ms |   111.4 ms |
|  39   |   111.6 ms |   1,778.1 ms |       843.7 ms |   110.5 ms |
|  40   |   110.5 ms |   1,779.0 ms |       843.9 ms |   111.3 ms |
|  41   |   111.5 ms |   1,778.2 ms |       843.8 ms |   110.4 ms |
|  42   |   110.4 ms |   1,786.1 ms |       846.0 ms |   110.3 ms |
|  43   |   110.4 ms |   1,779.5 ms |       843.8 ms |   110.4 ms |
|  44   |   110.5 ms |   1,779.4 ms |     1,328.1 ms |   110.4 ms |
|  45   |   110.5 ms |   1,513.7 ms |       968.8 ms |   110.4 ms |
|  46   |   110.5 ms |   2,221.5 ms |       843.7 ms |   112.5 ms |
|  47   |   112.6 ms |   1,904.7 ms |       843.8 ms |   110.3 ms |
|  48   |   110.4 ms |   1,279.5 ms |       843.8 ms |   110.3 ms |
|  49   |   110.3 ms |   1,982.7 ms |       843.8 ms |   111.3 ms |
|  50   |   111.5 ms |   2,215.9 ms |       843.7 ms |   635.1 ms |
|=======|============|==============|================|============|
|  avg  |   108.6 ms |   1,788.7 ms |       920.1 ms |   121.2 ms |
|_______|____________|______________|________________|____________|
 *** Average download speed for 40,448,000 bytes: 858.61 KB/s


SSH.NET download test, 50 iteration(s).
___________________________________________________________________
|  Run  |  Client Cr |  Connection  |    Download    |   Delete   |
|   1   |    10.7 ms |     927.5 ms |       628.9 ms |    37.0 ms |
|   2   |    37.0 ms |     922.5 ms |       793.7 ms |    30.2 ms |
|   3   |    30.2 ms |   1,028.0 ms |       676.0 ms |    30.1 ms |
|   4   |    30.1 ms |     878.7 ms |     1,258.9 ms |    30.0 ms |
|   5   |    30.0 ms |     928.2 ms |       766.1 ms |    30.8 ms |
|   6   |    30.8 ms |     878.3 ms |       682.4 ms |    30.3 ms |
|   7   |    30.4 ms |     997.2 ms |       707.5 ms |    45.3 ms |
|   8   |    45.3 ms |   1,011.9 ms |       667.9 ms |    30.0 ms |
|   9   |    30.1 ms |     996.0 ms |       649.9 ms |    33.7 ms |
|  10   |    33.7 ms |     833.0 ms |       869.0 ms |    57.9 ms |
|  11   |    58.0 ms |     890.6 ms |     1,246.2 ms |    30.6 ms |
|  12   |    30.7 ms |     915.5 ms |       670.6 ms |    34.2 ms |
|  13   |    34.2 ms |     940.2 ms |       804.1 ms |    30.5 ms |
|  14   |    30.5 ms |   1,051.0 ms |     1,122.1 ms |    30.3 ms |
|  15   |    30.4 ms |     864.3 ms |       617.7 ms |    29.7 ms |
|  16   |    29.7 ms |     857.0 ms |       638.0 ms |    30.0 ms |
|  17   |    30.0 ms |     991.7 ms |       766.8 ms |    30.1 ms |
|  18   |    30.1 ms |     903.6 ms |     1,156.0 ms |    30.8 ms |
|  19   |    30.8 ms |     959.0 ms |       842.0 ms |    30.2 ms |
|  20   |    30.2 ms |     945.7 ms |     1,065.2 ms |    29.7 ms |
|  21   |    29.7 ms |     948.2 ms |     1,136.4 ms |    31.1 ms |
|  22   |    31.1 ms |     902.3 ms |       613.0 ms |    30.4 ms |
|  23   |    30.4 ms |     842.2 ms |       821.2 ms |    29.9 ms |
|  24   |    30.0 ms |     887.7 ms |       647.8 ms |    29.5 ms |
|  25   |    29.5 ms |     852.7 ms |       632.1 ms |    30.0 ms |
|  26   |    30.0 ms |     906.8 ms |       644.7 ms |    30.1 ms |
|  27   |    30.1 ms |     906.1 ms |       596.0 ms |    30.4 ms |
|  28   |    30.4 ms |   1,014.1 ms |       674.0 ms |    29.6 ms |
|  29   |    29.7 ms |     895.5 ms |     1,076.0 ms |    30.6 ms |
|  30   |    30.6 ms |     953.3 ms |       716.5 ms |    31.1 ms |
|  31   |    31.1 ms |     929.5 ms |       643.4 ms |    30.4 ms |
|  32   |    30.5 ms |     970.7 ms |       731.7 ms |    30.7 ms |
|  33   |    30.8 ms |     926.8 ms |       621.8 ms |    30.3 ms |
|  34   |    30.4 ms |     847.4 ms |       670.3 ms |    30.3 ms |
|  35   |    30.4 ms |     895.4 ms |       707.8 ms |    31.4 ms |
|  36   |    31.4 ms |   1,060.1 ms |       653.8 ms |    30.3 ms |
|  37   |    30.4 ms |     898.9 ms |       632.5 ms |    30.2 ms |
|  38   |    30.2 ms |     891.6 ms |       658.6 ms |    30.5 ms |
|  39   |    30.5 ms |     847.9 ms |       602.4 ms |    29.8 ms |
|  40   |    29.9 ms |     905.8 ms |       624.0 ms |    29.5 ms |
|  41   |    29.5 ms |     975.0 ms |       605.6 ms |    30.0 ms |
|  42   |    30.1 ms |     877.6 ms |       674.4 ms |    29.6 ms |
|  43   |    29.6 ms |   1,000.7 ms |       760.5 ms |    31.7 ms |
|  44   |    31.7 ms |     895.9 ms |       693.0 ms |    30.1 ms |
|  45   |    30.1 ms |     886.6 ms |       649.7 ms |    30.2 ms |
|  46   |    30.2 ms |     875.3 ms |       701.4 ms |    29.9 ms |
|  47   |    29.9 ms |   1,052.4 ms |       621.3 ms |    30.3 ms |
|  48   |    30.4 ms |     856.2 ms |       653.4 ms |    30.5 ms |
|  49   |    30.5 ms |     922.2 ms |       687.3 ms |    30.3 ms |
|  50   |    30.3 ms |     918.8 ms |       700.7 ms |    30.9 ms |
|=======|============|==============|================|============|
|  avg  |    31.0 ms |     925.3 ms |       749.6 ms |    31.4 ms |
|_______|____________|______________|________________|____________|
 *** Average download speed for 40,448,000 bytes: 1,053.89 KB/s

Keep up the great work and thank you.

lochnar187 avatar Aug 16 '17 22:08 lochnar187

Thanks for the feedback!

drieseng avatar Aug 17 '17 06:08 drieseng

@lochnar187 I've discovered a performance regression when the read-ahead mechanism is used with Sun SSH (issue #292). The fix for that issue should also improve performance a little for other SFTP servers. I might call upon you to perform another test when beta3 is available.

drieseng avatar Aug 29 '17 18:08 drieseng

I was out of town but I will be happy to test with beta3 when available or with any developer build.

On Aug 29, 2017, at 11:28 AM, Gert Driesen [email protected] wrote:

@lochnar187 https://github.com/lochnar187 I've discover a performance regression when used with Sun SSH (issue #292 https://github.com/sshnet/SSH.NET/issues/292). The fix for that issue should also improve performance a little for other SFTP servers. I might call upon you to perform another test when beta3 is available.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/sshnet/SSH.NET/issues/100#issuecomment-325753208, or mute the thread https://github.com/notifications/unsubscribe-auth/ADQXsQLRU1l7AJ2Ff_xfBRNjPki7Q69Oks5sdFhLgaJpZM4KUBlH.

imapangolin avatar Aug 29 '17 19:08 imapangolin

@drieseng I'd be happy to do that! Let me know when.

lochnar187 avatar Aug 29 '17 19:08 lochnar187

@lochnar187 @imapangolin I've committed the fix for issue #292.

I had to revert sending SSH_FXP_OPEN and SSH_FXP_LSTAT requests in parallel since Sun SSH does not handle this well. This may result in a minor performance regression, but this should only be measurable for small files.

On the other hand, we now no longer send unnecessary SSH_FXP_READ requests once we've reached the reported file size. This should have a positive effect on all downloads.

Can you perform some tests with the develop branch? Also feel free to play around with the maximum number of pending reads (which is determined in ServiceFactory.CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize).

drieseng avatar Sep 16 '17 11:09 drieseng

@drieseng I gave it another run with the dev branch code and I'm seeing another 15-20% increase in speed over the last run (Aug 16). Mostly, this was due to less variation in today's runs with each cycle coming in with 600 to 750 ms while the last run saw wider variance. Very nice :)

I'm not done yet, I want to do a new bit of code to play with pending read count and see what that does. I'll post more in a while. Thank you.

lochnar187 avatar Sep 20 '17 18:09 lochnar187

Hi @drieseng great work on this project! I'm debugging the latest code because SSH.NET isn't performing as well as WinSCP (700 kb/s vs 1.6 mb/s < x) and I discovered the read ahead mechanism seems underperformant for me... I already confirmed that my max pending reads is 10.

All I did was add this simple debug statement

if (_queue.Count > 1)
{
    Debug.WriteLine("Queue count is : " + _queue.Count);
}

right here

And my results showed that over the course of my "large" download (290 mb file) over a high latency and high bandwidth network, I only ever reached a _queue count of 2 a couple of times.

Do I understand correctly that my queue is not being filled and as such I am downloading the entire file almost without any readaheads? Should I open a new issue for this? Thank you very much in advance.

UPDATE: After playing around more with the code I'm thinking that the queue not ever being filled is a symptom of lock contention. Every time a successful SSH_FXP_READ is sent and the queue written to, we immediately Pulse and activate the Read thread. The read thread locks down the queue and then loops around to Monitor.Wait which then reactivates the Read-Ahead thread. All this back and forth seems to essentially be creating synchronicity between the threads.

NoahLerner avatar Jan 26 '21 21:01 NoahLerner

We're also still seeing very poor performance on large file downloads in 2020.0.1 - 250kB transfers quickly (a few seconds), but 61MB transfers very slowly (7-8 minutes, 100-200kB/sec throughput during the transfer). Setting a large (256kB) buffer for large files doesn't help. The same file downloads very quickly in WinSCP (7-8 MB/sec).

jhardin-accumula avatar Feb 17 '21 16:02 jhardin-accumula

@jhardin-accumula I’ve got the same issue, but I also can’t get WinSCP download large files faster than ssh.net. Can you please share your code? Perhaps I need to tune session parameters.

I write extension for vscode and visual studio. Npm package for sftp downloads 70 mb file in a minute while it takes 10 minute or more to download the same file using SSH.NET.

helviett avatar Mar 16 '21 14:03 helviett

@jhardin-accumula I’ve got the same issue, but I also can’t get WinSCP download large files faster than ssh.net. Can you please share your code? Perhaps I need to tune session parameters.

I write extension for vscode and visual studio. Npm package for sftp downloads 70 mb file in a minute while it takes 10 minute or more to download the same file using SSH.NET.

Actually, my quick comparison for the above may have been misleading - I'm doing C# dev in a MacOS-hosted Parallels VM and I was actually comparing SSH.NET in the VM to sftp from the Mac environment... Running WinSCP in the VM gives similar mediocre network performance, and the network throughput reported by Task Manager in the VM is much higher than the network throughput for the VM environment reported by the MacOS host. I'm not sure SSH.NET is the problem for me. The production install at AWS is showing > 400kB/s throughput from the same SFTP server.

Modified SSH.NET parameters: timeout = 10m, keepalive = 30s (to allow for connection idle delay from processing the file after download), and if the file being retrieved is >1MB the buffer size is set to 256kB (even though Noah's comment suggests that's pointless... I'm anticipating a fix :) ).

jhardin-accumula avatar Mar 16 '21 15:03 jhardin-accumula

I've added two PRs today that improve SFTP performance (by a LOT, in my case), and simultaneously reduce CPU usage: #865 and #866. I'm not sure if the SCP file transfers also benefit, I didn't test/check that.

According to my benchmarks, both FileUpload and FileDownload have massive speed gains and are now comparable to Filezilla.

These changes are for SftpClient.UploadFile/DownloadFile and variants. The Stream/CopyTo versions will also benefit a little on the CPU-usage side, but since they are synchronous and don't queue requests, I don't think they'll benefit that much performance-wise. However, if you are using (like I was) the Stream versions because they support Resume, you might want to check out #864.

I'd be very interested to know if these changes also help you guys :)

zybexXL avatar Aug 30 '21 21:08 zybexXL