Perl script: speed.pl

A small script I wrote to see how fast a file grows (for instance when it’s being copied from one partition to another, disk to disk, network to network, …)

#!/usr/bin/perl
use strict;
use warnings;

my $file = shift @ARGV;
$| = 1;

my @LABELS = qw/kbytes mbytes gbytes/;
my $prev_size = -s $file;
my ($_TOTAL, $count) = (0, 1);

while(1) {
 my $size = -s $file;

 my $size_diff      = $size - $prev_size;
 $_TOTAL            = ( $_TOTAL + $size_diff ) / $count++;
 my($total, $label) = human_readable($_TOTAL);

 printf "r%80s", ' ';
 printf "rSize: %0.2f %s | Speed: %0.2f %s/s", human_readable($size)
 , $total
 , $label;

 sleep 1;
 $prev_size = $size;
}

sub human_readable {
 my ($bytes) = @_;
 my $label = 'bytes';

 my @labels = @LABELS;

 while ( $bytes > 1024 ) {
 $bytes /= 1024;
 $label = shift @labels;
 }

 return ($bytes, $label);
}

Comments are closed.