ssh - Perl SFTP really really slow -
i'm using perl net:ssh2 library sftp. works enough super slow. starts off being 7x slower link speed (something handle) pretty has transferred 2mb of data drops being 1000x slower. here's cut down version of code. used linux command create 10m file of random data dd if=/dev/urandom of=10m.dat bs=1m count=10
. run code please enter details server, user, password etc.
#!/usr/bin/perl use strict; use warnings; use fcntl ('o_wronly', 'o_creat'); use net::ssh2; use time::hires qw(gettimeofday); $server = 'myserver'; $username = 'myusername'; $password = 'mypassword'; $remotefile = 'delme.dat'; $localfile = '10m.dat'; $ssh = net::ssh2->new(); $ssh->connect($server) or die('failed connect remote server sftp'); $ssh->auth_password($username, $password); $ssh->auth_ok() or die('invalid username or password'); $sftp = $ssh->sftp() or die('ahhh!!'); open(my $localhandle, '<', $localfile) or die($!); $remotehandle = $sftp->open($remotefile, o_wronly | o_creat) or die($sftp->error()); $tran = 0; $t = gettimeofday(); while(my $line = <$localhandle>) { print $remotehandle $line; $tran += length($line); $newtime = gettimeofday(); if($newtime - $t > 1) { print 'speed ' . ($tran / ($newtime - $t)) . "b/sec\n"; $t = $newtime; $tran = 0; } } close($remotehandle); close($localhandle);
support sftp in net::ssh2/libssh2 suboptimal.
use net::sftp::foreign instead (with net::ssh2 backend net::sftp::foreign::backend::net_ssh2 if on windows).
Comments
Post a Comment