Skip to content

slow sftp transfer speed compared with system rsync/sftp/scp #1080

Open

Description

@adamteale

Hey guys,

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

Activity

  1. zachliu commented on Nov 30, 2017

    @zachliu

    in addition to rsync/sftp/scp/finder, lftp is also much faster than paramiko

  2. chatainsim commented on Feb 19, 2020

    @chatainsim

    Same issue here.

  3. douglas-raillard-arm commented on Aug 11, 2021

    @douglas-raillard-arm

    +1, one transfer that takes less than a second with the sftp command line client takes ~36 seconds with SFTPClient.get(). SCPClient.get() from the scp Python package seems to be much faster:
    https://pypi.org/project/scp/

  4. douglas-raillard-arm commented on Aug 11, 2021

    @douglas-raillard-arm

    Profiling SFTPClient.getfo() and sorting by cumtime gave:

       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
            1    0.001    0.001   48.978   48.978 sftp_client.py:675(_transfer_with_callback)
          111    0.020    0.000   48.954    0.441 file.py:172(read)
          879    0.013    0.000   48.933    0.056 sftp_file.py:179(_read)
          878    0.012    0.000   48.497    0.055 sftp_client.py:815(_request)
          988    0.026    0.000   48.435    0.049 sftp_client.py:844(_read_response)
          988    0.017    0.000   48.382    0.049 sftp.py:200(_read_packet)
         1976    0.022    0.000   48.358    0.024 sftp.py:170(_read_all)
         1986    0.016    0.000   48.332    0.024 channel.py:685(recv)
         1986    0.034    0.000   48.293    0.024 buffered_pipe.py:126(read)
          907    0.020    0.000   48.234    0.053 threading.py:270(wait)
        11240   48.210    0.004   48.210    0.004 {method 'acquire' of '_thread.lock' objects}
          878    0.026    0.000    0.455    0.001 sftp_client.py:819(_async_request)
            2    0.001    0.001    0.407    0.204 sftp_file.py:149(_read_prefetch)
          878    0.010    0.000    0.376    0.000 sftp.py:193(_send_packet)
          878    0.007    0.000    0.330    0.000 sftp.py:160(_write_all)
          878    0.008    0.000    0.322    0.000 channel.py:783(send)
          878    0.011    0.000    0.308    0.000 channel.py:1191(_send)
          896    0.013    0.000    0.288    0.000 transport.py:1841(_send_user_message)
          896    0.004    0.000    0.260    0.000 transport.py:1838(_send_message)
          896    0.033    0.000    0.256    0.000 packet.py:388(send_message)
    

    I don't know if it's an artifact but there seems to be a very high contention on a lock:

       ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        11240   48.210    0.004   48.210    0.004 {method 'acquire' of '_thread.lock' objects}
    
  5. douglas-raillard-arm commented on Aug 11, 2021

    @douglas-raillard-arm

    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:
    {
        0: <4k bytes block>,
        32k: <4k bytes block>,
        ...
    }
    

    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.

  6. douglas-raillard-arm commented on Aug 11, 2021

    @douglas-raillard-arm

    @bitprophet this patch fixes the issue:

    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.

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