3

So I use phpseclib which downloads a 50MB file over sftp in roughly 45 seconds. Which is fast compared to ssh2_scp_recv() which takes 90+ seconds, but slow compared to my sftp client (filezilla), which takes 10 seconds max.

My question is, what can I do to speed up file downloads through sftp, other than enabling the mcrypt, gmp and bcmath extensions which I've done already?

I'm running PHP 5.5 on Windows 7, and got the same results when using either cli or browser/apache, and using sftp->get to download a file as a whole, or download a file in chunks of various sizes.

Source:

set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib');
require 'phpseclib/Net/SFTP.php';

$sftp = new Net_SFTP($host, $port, $timeout);
$sftp->login($user, $password);

$sftp->get($remoteFile, $localFile);
Kevin Op den Kamp
  • 565
  • 1
  • 8
  • 22
  • 1
    When comparing phpseclib to filezilla, do you take into account the time it takes for filezilla to connect and perform directory listings? The only way to speed this up that I know of is to have a constant connection which is not a great idea for obvious reasons. – Scopey Jan 07 '15 at 22:31
  • I don't expect phpseclib to be just as fast or faster than a dedicated client, but keep in mind that I only compared the amount of time it takes to actually download a file, not settings up a connection and logging in (which takes less than a second really). phpseclib works like a charm and I'm just looking for ways to improve its download speeds :) – Kevin Op den Kamp Jan 08 '15 at 07:33
  • Do you have mcrypt installed? Even tho phpseclib's pure-PHP implementations of RC4 / AES / etc are among the fastest pure-PHP implementations around it doesn't hold a torch to mcrypt, which is compiled C. – neubert Jan 13 '15 at 04:38

1 Answers1

4

With an SFTP protocol, a client (client library) uses a "READ" request repeatedly to get chunks of file contents.

A dumb implementation, that phpseclib uses, sends one "READ" request (for up to 32 kB), waits for a "DATA" response, sends another "READ" request, waits, and so on, until it gets a whole file.

If a roundtrip to/from a server is long (big latency), the client (library) may be uselessly waiting most of the time.

Smart clients (libraries) overcome this by sending multiple "READ" requests, without waiting for the response, or by using a large "READ" request, or both.

FileZilla for instance, sends a sequence of 32 kB "READ" requests for up to total 1 MB worth of data.

The phpseclib does not support this optimization (note, that it does for uploads, though).

All you can do is to increase a size of the "READ" request, using Net_SFTP::max_sftp_packet.

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992