IO-FD
view release on metacpan or search on metacpan
t/02-sendfile.t view on Meta::CPAN
use strict;
use warnings;
use feature ":all";
use Test::More;
use IO::FD;
use Fcntl qw<O_NONBLOCK F_SETFL O_RDWR O_CREAT>;
use POSIX qw<errno_h>;
use Socket ":all";
# Netbsd does not support sendfile
use Config;
my @vers=split /\./, $Config{osvers};
plan skip_all=>"No sendfile to test on NetBSD or OpenBSD" if $Config{osname}=~/netbsd|openbsd/i;
#create a socket pair
#====================
die "Could not create pair" unless defined IO::FD::socketpair my $s1, my $s2, AF_UNIX, SOCK_STREAM, 0;
#set both to non blocking mode
die unless defined IO::FD::fcntl $s1, F_SETFL, O_NONBLOCK;
die unless defined IO::FD::fcntl $s2, F_SETFL, O_NONBLOCK;
#Create a file with random data
#=================
my $path="tmp_large_file_.txt";
unlink $path;
die unless defined IO::FD::sysopen my $file, $path, O_CREAT|O_RDWR;
my $count=0;
my $file_size=0;
for(1..10){
my $data= join "", map { chr ord("A")+ rand(26)} 1..1024;
$file_size+=$count=IO::FD::syswrite $file, $data;
die "Could not write to file $!" unless defined $count;
}
#Read contents for test comparison
#=================================
my $data=do { local $/=undef; open my $fh, "<", $path; <$fh>};
ok $data, "Loading comparision/sendfile data";
#Do nonblocking sendfile and read via the socket pair
#=================
#NOTE: file position is not reset as sendfile uses an explicit offset
my $send_offset=0;
my $send_count=0;
my $run=2;
( run in 1.983 second using v1.01-cache-2.11-cpan-8f98c5d2c55 )