Skip to content

Commit 2eec748

Browse files
zybexXLPedro Fonseca
and
Pedro Fonseca
authored
Improve SFTP performance on medium/high latency connections (#866)
* Improve SFTP performance on medium-high latency connections - Increases maxPendingReads from 10 to 100 - Increases socket send/receive buffer to 10 SSH packets (320K) * Fix merge * Adjust SFTP FileReader testcases to accept new MaxPendingReads values * Fix CreateSftpFileReader testcase, make it dependant on MaxPendingReads constant --------- Co-authored-by: Pedro Fonseca <[email protected]>
1 parent 823bc1b commit 2eec748

4 files changed

+9
-7
lines changed

src/Renci.SshNet/Connection/ConnectorBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ protected Socket SocketConnect(string host, int port, TimeSpan timeout)
4949
{
5050
SocketAbstraction.Connect(socket, ep, timeout);
5151

52-
const int socketBufferSize = 2 * Session.MaximumSshPacketSize;
52+
const int socketBufferSize = 10 * Session.MaximumSshPacketSize;
5353
socket.SendBufferSize = socketBufferSize;
5454
socket.ReceiveBufferSize = socketBufferSize;
5555
return socket;

src/Renci.SshNet/ServiceFactory.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public INetConfSession CreateNetConfSession(ISession session, int operationTimeo
143143
/// </returns>
144144
public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize)
145145
{
146-
const int defaultMaxPendingReads = 3;
146+
const int defaultMaxPendingReads = 10;
147147

148148
// Issue #292: Avoid overlapping SSH_FXP_OPEN and SSH_FXP_LSTAT requests for the same file as this
149149
// causes a performance degradation on Sun SSH
@@ -163,7 +163,7 @@ public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSe
163163
{
164164
var fileAttributes = sftpSession.EndLStat(statAsyncResult);
165165
fileSize = fileAttributes.Size;
166-
maxPendingReads = Math.Min(10, (int) Math.Ceiling((double) fileAttributes.Size / chunkSize) + 1);
166+
maxPendingReads = Math.Min(100, (int)Math.Ceiling((double)fileAttributes.Size / chunkSize) + 1);
167167
}
168168
catch (SshException ex)
169169
{

test/Renci.SshNet.Tests/Classes/ServiceFactoryTest_CreateSftpFileReader_EndLStatThrowsSshException.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ private void SetupMocks()
5959
.Setup(p => p.EndLStat(_statAsyncResult))
6060
.Throws(new SshException());
6161
_sftpSessionMock.InSequence(seq)
62-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 3, null))
62+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 10, null))
6363
.Returns(_sftpFileReaderMock.Object);
6464
}
6565

Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
namespace Renci.SshNet.Tests.Classes
99
{
1010
[TestClass]
11-
public class ServiceFactoryTest_CreateSftpFileReader_FileSizeIsMoreThanTenTimesGreaterThanChunkSize
11+
public class ServiceFactoryTest_CreateSftpFileReader_FileSizeIsMoreThanMaxPendingReadsTimesChunkSize
1212
{
1313
private ServiceFactory _serviceFactory;
1414
private Mock<ISftpSession> _sftpSessionMock;
@@ -22,18 +22,20 @@ public class ServiceFactoryTest_CreateSftpFileReader_FileSizeIsMoreThanTenTimesG
2222
private SftpFileAttributes _fileAttributes;
2323
private long _fileSize;
2424
private ISftpFileReader _actual;
25+
private int _maxPendingReads;
2526

2627
private void SetupData()
2728
{
2829
var random = new Random();
2930

31+
_maxPendingReads = 100;
3032
_bufferSize = (uint)random.Next(1, int.MaxValue);
3133
_openAsyncResult = new SftpOpenAsyncResult(null, null);
3234
_handle = CryptoAbstraction.GenerateRandom(random.Next(1, 10));
3335
_statAsyncResult = new SFtpStatAsyncResult(null, null);
3436
_fileName = random.Next().ToString();
3537
_chunkSize = (uint) random.Next(1000, 5000);
36-
_fileSize = _chunkSize * random.Next(11, 50);
38+
_fileSize = _chunkSize * random.Next(_maxPendingReads + 1, _maxPendingReads * 2);
3739
_fileAttributes = new SftpFileAttributesBuilder().WithSize(_fileSize).Build();
3840
}
3941

@@ -63,7 +65,7 @@ private void SetupMocks()
6365
.Setup(p => p.EndLStat(_statAsyncResult))
6466
.Returns(_fileAttributes);
6567
_sftpSessionMock.InSequence(seq)
66-
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, 10, _fileSize))
68+
.Setup(p => p.CreateFileReader(_handle, _sftpSessionMock.Object, _chunkSize, _maxPendingReads, _fileSize))
6769
.Returns(_sftpFileReaderMock.Object);
6870
}
6971

0 commit comments

Comments
 (0)