You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
slow sftp transfer speed compared with system rsync/sftp/scp #1080
I have noticed that I can't get the same transfer speed when performing a get or put with paramiko's sftp.
Across our gigabit network from our mac mini server (running Mac os 10.12.6) a file transfer via rsync/sftp/scp/finder sustains around 95-100MB/sec. If I use paramiko's sftp.get I see it reaches a maximum of 25MB/sec.
I was using paramiko 1.17 and updated to 2.3.1 but am seeing pretty much the same speed.
Any ideas what could be causing the limitation?
Thanks!
Adam
Reacted by Zach Liu, truelegend, eddebc, Jamie Scott and Jinzhe Zeng
Ok so made some very good progress. Turns out that there is no contention issue.
The problem in my case comes from broken prefetching:
SFTPFile.prefetch() establishes a list of blocks to prefetch of size SFTPFile.MAX_REQUEST_SIZE (unless the file is smaller than that).
The prefetch thread (SFTP._prefetch_thread()) does its job and starts async requests for each block.
The blocks are saved in _prefetch_data attribute when the response arrives But it turns out my server is only sending back 4k blocks, even when the request was for SFTPFile.MAX_REQUEST_SIZE == 32k. This means that _prefetch_data is like that:
This makes the prefetched data useless, as we get a tiny bit of content every 32k. This makes SFTPFile._read_prefetch() ignore the prefetched data after the first read, and fall back to a sequential read.
When adjusting SFTPFile.MAX_REQUEST_SIZE = 4096, the file is transferred in 2 seconds instead of 36s.
diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py
index 0104d857..4716abac 100644
--- a/paramiko/sftp_file.py
+++ b/paramiko/sftp_file.py
@@ -465,11 +465,23 @@ class SFTPFile(BufferedFile):
if file_size is None:
file_size = self.stat().st_size
+ # Some servers will respond with a lower amount of data than expected,
+ # so adjust dynamically the chunk size to that. Otherwise, the
+ # prefetched content is unusable.
+ n = self._realpos
+ test_size = min(self.MAX_REQUEST_SIZE, file_size - n)
+ test_data = self._read(test_size)
+ test_data_len = len(test_data)
+ chunk_size = min(test_data_len, test_size)
+
+ with self._prefetch_lock:
+ self._prefetch_data[n] = test_data
+ n += test_data_len
+
# queue up async reads for the rest of the file
chunks = []
- n = self._realpos
while n < file_size:
- chunk = min(self.MAX_REQUEST_SIZE, file_size - n)
+ chunk = min(chunk_size, file_size - n)
chunks.append((n, chunk))
n += chunk
if len(chunks) > 0:
EDIT: the issue that this patch deals with seems to only appear when reading sysfs files (aka in /sys/). This is probably the result of OpenSSH not knowing what to do, as the reported file size in sysfs is always 0. In other cases, it does not seem to make any difference.
Activity
zachliu commentedon Nov 30, 2017
in addition to
rsync/sftp/scp/finder,lftpis also much faster than paramikochatainsim commentedon Feb 19, 2020
Same issue here.
douglas-raillard-arm commentedon Aug 11, 2021
+1, one transfer that takes less than a second with the
sftpcommand line client takes ~36 seconds withSFTPClient.get().SCPClient.get()from thescpPython package seems to be much faster:https://pypi.org/project/scp/
douglas-raillard-arm commentedon Aug 11, 2021
Profiling
SFTPClient.getfo()and sorting by cumtime gave:I don't know if it's an artifact but there seems to be a very high contention on a lock:
douglas-raillard-arm commentedon Aug 11, 2021
Ok so made some very good progress. Turns out that there is no contention issue.
The problem in my case comes from broken prefetching:
SFTPFile.prefetch()establishes a list of blocks to prefetch of sizeSFTPFile.MAX_REQUEST_SIZE(unless the file is smaller than that).SFTP._prefetch_thread()) does its job and starts async requests for each block._prefetch_dataattribute when the response arrivesBut it turns out my server is only sending back 4k blocks, even when the request was for
SFTPFile.MAX_REQUEST_SIZE == 32k. This means that_prefetch_datais like that:This makes the prefetched data useless, as we get a tiny bit of content every 32k. This makes
SFTPFile._read_prefetch()ignore the prefetched data after the first read, and fall back to a sequential read.When adjusting
SFTPFile.MAX_REQUEST_SIZE = 4096, the file is transferred in 2 seconds instead of 36s.It looks like that based on https://github.com/paramiko/paramiko/pull/1328/files, it should be easy to modify the max_request_size on the fly if the server returns less data than requested, so that we can establish a correct chunk size.
douglas-raillard-arm commentedon Aug 11, 2021
@bitprophet this patch fixes the issue:
EDIT: the issue that this patch deals with seems to only appear when reading sysfs files (aka in /sys/). This is probably the result of OpenSSH not knowing what to do, as the reported file size in sysfs is always 0. In other cases, it does not seem to make any difference.
ssh: Fix SFTP pull speed issue
ssh: Fix SFTP pull speed issue
ssh: Fix SFTP pull speed issue