我正在尝试自动将FTP上传/从本地系统下载到FTP服务器并计算带宽或一些统计信息。
要求:
代码:
use strict;
use warnings;
use Net::FTP;
use Time::Piece;
use Time::Seconds qw/ ONE_DAY /;
my ($ftp, $host, $user, $pass, $dir, $fpath);
$host = "hostname";
$user = "username";
$pass = "password";
$dir = "/home/file/user";
$fpath = "E:/Automation/tesfiles/10meg.test";
$ftp = Net::FTP->new($host, Debug => 0);
$ftp->login($user, $pass) or die $ftp->message;
$ftp->cwd($dir);
#FTP Upload
my $upload_start = localtime;
$ftp->put($fpath) or die $ftp->message;
print $ftp->message;
my $upload_end = localtime;
my $upload_time = ($upload_end - $upload_start)->pretty;
my $fname = '10meg.test';
my $size = $ftp->size( $fname );
defined( $size ) or die( " Failed to get size: " . $ftp->message );
#FTP Download
my $download_start = localtime;
print "Size of $fname is $size\n";
$ftp->get($fname) or die $ftp->message;
print $ftp->message;
my $download_end = localtime;
my $download_time = ($download_end - $download_start)->pretty;
print "Time taken for FTP download is $upload_time for $fname file\n";
print "Time taken for FTP upload is $download_time for $fname file\n";
print "Hence Throughput for Download is: $size/$download_time\n";
print "Hence Throughput for Upload is: $size/$upload_time\n";
控制台结果:
转移完成。
10meg.test的大小是10038600
转移完成。
对于10meg.test文件,FTP下载所花费的时间为43秒
对于10meg.test文件,FTP上传所需的时间为5秒
因此,下载的吞吐量为:10038600/43秒
因此,上传吞吐量为:10038600/5秒
的问题:
一个。请参阅吞吐量不是数字 湾我需要将输出重定向到 excel或记事本 C。我需要在循环中重复这个过程。
P.S:
我在Perl中是全新的,但不知怎的,我管理了一个将文件上传到FTP服务器的脚本(源:SO)。
答案 0 :(得分:0)
use strict;
use warnings;
use Net::FTP;
my ($ftp, $host, $user, $pass, $dir, $fpath);
$host = "hostname";
$user = "username";
$pass = "password";
$dir = "/home/file/user";
$fpath = "E:/Automation/testfiles/10meg.test";
$ftp = Net::FTP->new($host, Debug => 0);
$ftp->login($user, $pass) or die $ftp->message;
$ftp->cwd($dir);
$ftp->put($fpath) or die $ftp->message;
print $ftp->message;
my $fname = '10meg.test';
my $size = $ftp->size( $fname );
defined( $size ) or die( " Failed to get size: " . $ftp->message );
my $transfer_start = time;
print " Size of $fname is $size\n";
$ftp->get($fname) or die $ftp->message;
print $ftp->message;
my $transfer_end = time;
my $time_taken = $transfer_end - $transfer_start;
print "Time take to download of $fname is $time_taken\n";
print "Transfer speed $size/$time_taken\n";
有一个名为Hash的 - > New()函数的选项,每次传输1024个字节时都会通知您。阅读它!