#!env perl # # pm11142815.pl # # How fast is random I/O with big (default size) vs small buffers? To find out, # we'll first read N blocks (randomly) from the first half of the big file using # the default block size. Then we'll set the block size to the record size and # read N blocks from the second half of the big file. # # 20220408 use strict; use warnings; use Time::HiRes qw( gettimeofday tv_interval ); my $FName = shift or die "Missing FName and record size!"; my $recSize = shift or die "Missing record size!"; $recSize = $recSize + 0; open my $FH, '<', $FName or die "Can't open $FName: $!\n"; binmode $FH; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($FName); my $half_file_size = int($size/2); print "File size: $size, record size=$recSize\n"; my $N = 10_000; read_N_blocks($N, $FH, 0, $half_file_size); print "Setting record size to $recSize bytes\n"; $/=\$recSize; read_N_blocks($N, $FH, $half_file_size, $size); sub read_N_blocks { my ($N, $FH, $start, $end) = @_; print "Read $N records from file between $start and $end\n"; my $dS = $end - $start - $recSize; my $t = [gettimeofday]; for my $i (1 .. $N) { my $offset = int($dS * rand) + $start; seek($FH, $offset, 0); my $record = <$FH>; } my $dT = tv_interval($t, [gettimeofday]); print "\ttook ${dT}s\n\n"; }