Repository navigation
Open
Description
I am noticing a big difference in transfer performance between using the SFTPClient.put() method vs using SFTPClient.open() even when mimicking the way SFTPClient.put() operates.
dd if=/dev/zero of=/tmp/100m.img bs=1024 count=0 seek=$[1024*100]import logging
from time import perf_counter
logger = logging.getLogger("ssh_test")
logging.basicConfig(level="INFO")
def test_paramiko_put():
import paramiko
start = perf_counter()
source = "/tmp/100m.img"
target = "/tmp/100m-paraminko-put-copy.img"
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
bytes_copied = 0
throughput = []
def progress_callback(size, file_size):
nonlocal bytes_copied, throughput, start
bytes_copied = size
if bytes_copied % (1024 * 1024 * 10) == 0:
sample = bytes_copied / (perf_counter() - start) / 1024 / 1024
logger.info("paramiko_put: %.6f MB/s", sample)
throughput.append(sample)
ssh.connect("localhost", port=2222, username="vscode", password="vscode")
transport = ssh.get_transport()
sftp = transport.open_sftp_client()
sftp.put(source, target, callback=progress_callback)
return (perf_counter() - start, sum(throughput)/len(throughput))
def test_paramiko_mimick_put():
import paramiko
import os
start = perf_counter()
source = "/tmp/100m.img"
target = "/tmp/100m-paraminko-copy.img"
ssh = paramiko.SSHClient()
ssh.load_system_host_keys()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect("localhost", port=2222, username="vscode", password="vscode")
transport = ssh.get_transport()
sftp = transport.open_sftp_client()
bytes_copied = 0
throughput = []
def progress_callback(size, file_size):
nonlocal bytes_copied, throughput, start
bytes_copied = size
if bytes_copied % (1024 * 1024 * 10) == 0:
sample = bytes_copied / (perf_counter() - start) / 1024 / 1024
logger.info("paramiko_put: %.6f MB/s", sample)
throughput.append(sample)
file_size = os.stat(source).st_size
reader = sftp.file(source, mode="rb")
writer = sftp.file(target, mode="wb")
writer.set_pipelined(True)
size = 0
while True:
data = reader.read(32768)
writer.write(data)
size += len(data)
if len(data) == 0:
break
progress_callback(size, file_size)
reader.close()
writer.close()
return (perf_counter() - start, sum(throughput)/len(throughput))
tests = {
'paramiko_put': test_paramiko_put(),
'paramiko_mimick_put': test_paramiko_mimick_put(),
}
for k, v in tests.items():
print(k, v)INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_8.4p1)
INFO:paramiko.transport:Authentication (publickey) failed.
INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport.sftp:[chan 0] Opened sftp connection (server version 3)
INFO:ssh_test:paramiko_put: 27.600279 MB/s
INFO:ssh_test:paramiko_put: 37.961904 MB/s
INFO:ssh_test:paramiko_put: 45.995843 MB/s
INFO:ssh_test:paramiko_put: 49.008660 MB/s
INFO:ssh_test:paramiko_put: 48.492214 MB/s
INFO:ssh_test:paramiko_put: 49.089114 MB/s
INFO:ssh_test:paramiko_put: 48.663036 MB/s
INFO:ssh_test:paramiko_put: 50.963728 MB/s
INFO:ssh_test:paramiko_put: 50.162616 MB/s
INFO:ssh_test:paramiko_put: 50.997146 MB/s
INFO:paramiko.transport:Connected (version 2.0, client OpenSSH_8.4p1)
INFO:paramiko.transport:Authentication (publickey) failed.
INFO:paramiko.transport:Authentication (password) successful!
INFO:paramiko.transport.sftp:[chan 0] Opened sftp connection (server version 3)
INFO:ssh_test:paramiko_mimick_put: 1.958528 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.972298 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.979350 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.975763 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.980049 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.979022 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.976248 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.978444 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.976587 MB/s
INFO:ssh_test:paramiko_mimick_put: 1.975596 MB/s
paramiko_put (1.9805563550035004, 45.89345396184179)
paramiko_mimick_put (50.62033093300124, 1.975188623889063)
Any idea why I am seeing such a drastic difference in performance?
Reacted by Jason Walker and Maximilian Knespel
Activity
jun66j5 commentedon Apr 28, 2023
Your code is wrong. It is caused by non-prefetch reader. See
prefetchinSFTPClient.get().rgibbard commentedon Apr 28, 2023
@jun66j5 At the risk of missing something obvious here, I dont see
prefetchbeing called in the code path forSFTPClient.put(). If I add a prefetch call like this:An exception is thrown:
I believe this is caused by also calling
writer.set_pipelined(True). Removing that line eliminates the exception, but I still only achieve 1-2Mb/s throughput.My goal here is to achieve similar throughput to paramiko get/put functions with the
SFTPClient.openfunction as implemented in the smart_open packagejun66j5 commentedon Apr 28, 2023
I'd suggest checking log of the remote ssh server, rather than believing something.
rgibbard commentedon Apr 28, 2023
@jun66j5 My test environment is completely local inside a VSCode devcontainer. Im reading a file via ssh on the localhost and writing to a different file via ssh on the same host. Commenting out either
writer.set_pipelined(True)orreader.prefetch(file_size)results in a successful (but slow) test every time, while having both in place results in an exception:All of this is besides the point. My true goal here is to understand how to achieve the same throughput of get/put using only the
SFTPClient.openfunction. Do you have any sample code you can share that achieves this?VideoFX commentedon Dec 23, 2023
I am also looking for a throughput solution to slow sftp SFTPClient put.
What is everyone else doing as an alternative to slow SFTPClient performance?
mxmlnkn commentedon Sep 22, 2024
Maybe they are looking at alternatives such as asyncssh. At least that's what I am considering after the time I sunk into debugging my performance problems without much to show for it. Alternatively, I am considering reimplementing
SFTPFilemyself in order to implement better prefetching, but I'll have to check whether the existing implementation uses any important private methods or whether there is a sufficiently lower-level API to do this without having to do a full pull-request-merge cycle.I can reproduce the issue with the initial code. On my system, the performance difference is even worse, yielding a faster bandwidth with
putand half the bandwidth withmimick_putcompared to the initial post, yielding a 60x performance difference between the compared methods:One thing to notice is that your comparison is unfair because
test_paramiko_putreads from a local file and uploads it viaputwhiletest_paramiko_mimick_putreads a remote file while also writing it into another remote file! I.e., you are not benchmarking a pure put but a get+put combination and therefore running into the same issue I have with pure read performance.When I add the
prefetchcall as proposed, then I also get:I get this error with and without the
writer.set_pipelined(True)line. And withwriter.set_pipelined(False), I get another error:OSError: Socket is closed.But to make the comparison fair, let's read the file locally and upload that:
Now, I get much better performance, meaning the read is the actual problem, not the put:
If one really wants to read from remote and put to remote, then one could buffer it:
Output:
Of course, this adds a possibly huge memory overhead for very large files. But, because of the aforementioned
paramiko.ssh_exception.SSHException: Server connection dropped:error, I don't know how one would stream a file with prefetching without fully buffering it locally. It seems like a bug to me that this does not work. Almost as if the read and writes over the connection interfere with each other, but then why does it only happen with prefetching? As the prefetching runs in another thread, maybe the locking mechanism to avoid concurrent read/writes to the connection contains errors.