X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=extras%2Fslurp_bench.pl;h=74725f4acd676b9f2478c881b050b9409d78b9cc;hb=b03ff8fdf43bde95c1ae665ab136fc35ecbdf52f;hp=a8cdc16bcadd63fbabdbd962db7a2a77c4fefcfb;hpb=fa575d4f392df9d5c4e72591be100c12bbe0cd83;p=urisagit%2FFile-Slurp.git diff --git a/extras/slurp_bench.pl b/extras/slurp_bench.pl index a8cdc16..74725f4 100755 --- a/extras/slurp_bench.pl +++ b/extras/slurp_bench.pl @@ -11,7 +11,8 @@ use Fcntl qw( :DEFAULT :seek ); use File::Slurp () ; -my $file = 'slurp_data' ; +my $file_name = 'slurp_data' ; +my( @lines, $text ) ; my %opts ; @@ -19,12 +20,10 @@ parse_options() ; run_benchmarks() ; -unlink $file ; +unlink $file_name ; exit ; -my( @lines, $text, $size ) ; - sub run_benchmarks { foreach my $size ( @{$opts{size_list}} ) { @@ -36,117 +35,190 @@ sub run_benchmarks { substr( $text, -$overage, $overage, '' ) ; substr( $lines[-1], -$overage, $overage, '' ) ; - File::Slurp::write_file( $file, $text ) ; + if ( $opts{slurp} ) { + + File::Slurp::write_file( $file_name, $text ) ; + + bench_list_slurp( $size ) if $opts{list} ; + bench_scalar_slurp( $size ) if $opts{scalar} ; + } -# bench_list_slurp( $size ) if $opts{list} && $opts{slurp} ; -# bench_scalar_slurp( $size ) if $opts{scalar} && $opts{slurp} ; - bench_spew_list() -# bench_scalar_spew( $size ) if $opts{scalar} && $opts{spew} ; + if ( $opts{spew} ) { + + bench_spew_list( $size ) if $opts{list} ; + bench_scalar_spew( $size ) if $opts{scalar} ; + } } } +########################################## + sub bench_spew_list { - return unless $opts{list} && $opts{spew} ; + my( $size ) = @_ ; print "\n\nWriting (Spew) a list of lines: Size = $size bytes\n\n" ; - my $result = timethese( $opts{duration}, { + my $result = timethese( $opts{iterations}, { + 'FS::write_file' => sub { unlink $file_name if $opts{unlink} ; + File::Slurp::write_file( $file_name, @lines ) }, + 'FS::write_file Aref' => sub { unlink $file_name if $opts{unlink} ; + File::Slurp::write_file( $file_name, \@lines ) }, + 'print' => sub { unlink $file_name if $opts{unlink} ; + print_file( $file_name, @lines ) }, + 'print/join' => sub { unlink $file_name if $opts{unlink} ; + print_join_file( $file_name, @lines ) }, + 'syswrite/join' => sub { unlink $file_name if $opts{unlink} ; + syswrite_join_file( $file_name, @lines ) }, + 'original write_file' => sub { unlink $file_name if $opts{unlink} ; + orig_write_file( $file_name, @lines ) }, + } ) ; + + cmpthese( $result ) ; +} + +sub print_file { - 'FS::write_file' => - sub { File::Slurp::write_file( $file, @lines ) }, + my( $file_name ) = shift ; - 'print' => - sub { print_file( $file, @lines ) }, + local( *FH ) ; + open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; - 'print/join' => - sub { print_join_file( $file, @lines ) }, + print FH @_ ; +} - 'syswrite/join' => - sub { syswrite_join_file( $file, @lines ) }, +sub print_join_file { - 'original write_file' => - sub { orig_write_file( $file, @lines ) }, + my( $file_name ) = shift ; - } ) ; + local( *FH ) ; + open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; - cmpthese( $result ) ; + print FH join( '', @_ ) ; } -# sub bench_scalar_spew { +sub syswrite_join_file { -# my ( $size ) = @_ ; + my( $file_name ) = shift ; -# print "\n\nScalar Spew of $size file\n\n" ; + local( *FH ) ; + open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; -# my $result = timethese( $dur, { + syswrite( FH, join( '', @_ ) ) ; +} -# new => -# sub { File::Slurp::write_file( $file, $text ) }, +sub sysopen_syswrite_join_file { -# new_ref => -# sub { File::Slurp::write_file( $file, \$text ) }, + my( $file_name ) = shift ; -# print_file => -# sub { print_file( $file, $text ) }, + local( *FH ) ; + sysopen( FH, $file_name, O_WRONLY | O_CREAT ) || + carp "can't create $file_name $!" ; -# print_join_file => -# sub { print_join_file( $file, $text ) }, + syswrite( FH, join( '', @_ ) ) ; +} -# syswrite_file => -# sub { syswrite_file( $file, $text ) }, +sub orig_write_file +{ + my ($f, @data) = @_; -# syswrite_file2 => -# sub { syswrite_file2( $file, $text ) }, + local(*F); -# orig_write_file => -# sub { orig_write_file( $file, $text ) }, + open(F, ">$f") || croak "open >$f: $!"; + (print F @data) || croak "write $f: $!"; + close(F) || croak "close $f: $!"; + return 1; +} -# } ) ; +########################################## + +sub bench_scalar_spew { + + my ( $size ) = @_ ; + + print "\n\nWriting (Spew) a scalar: Size = $size bytes\n\n" ; + + my $result = timethese( $opts{iterations}, { + 'FS::write_file' => sub { unlink $file_name if $opts{unlink} ; + File::Slurp::write_file( $file_name, $text ) }, + 'FS::write_file Sref' => sub { unlink $file_name if $opts{unlink} ; + File::Slurp::write_file( $file_name, \$text ) }, + 'print' => sub { unlink $file_name if $opts{unlink} ; + print_file( $file_name, $text ) }, + 'syswrite_file' => sub { unlink $file_name if $opts{unlink} ; + syswrite_file( $file_name, $text ) }, + 'syswrite_file_ref' => sub { unlink $file_name if $opts{unlink} ; + syswrite_file_ref( $file_name, \$text ) }, + 'orig_write_file' => sub { unlink $file_name if $opts{unlink} ; + orig_write_file( $file_name, $text ) }, + } ) ; + + cmpthese( $result ) ; +} + +sub syswrite_file { + + my( $file_name, $text ) = @_ ; + + local( *FH ) ; + open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; + + syswrite( FH, $text ) ; +} + +sub syswrite_file_ref { + + my( $file_name, $text_ref ) = @_ ; + + local( *FH ) ; + open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; + + syswrite( FH, ${$text_ref} ) ; +} + +############################################# -# cmpthese( $result ) ; -# } # sub bench_scalar_slurp { # my ( $size ) = @_ ; -# print "\n\nScalar Slurp of $size file\n\n" ; +# print "\n\nScalar Slurp of $size bytes\n\n" ; # my $buffer ; # my $result = timethese( $dur, { # new => -# sub { my $text = File::Slurp::read_file( $file ) }, +# sub { my $text = File::Slurp::read_file( $file_name ) }, # new_buf_ref => # sub { my $text ; -# File::Slurp::read_file( $file, buf_ref => \$text ) }, +# File::Slurp::read_file( $file_name, buf_ref => \$text ) }, # new_buf_ref2 => # sub { -# File::Slurp::read_file( $file, buf_ref => \$buffer ) }, +# File::Slurp::read_file( $file_name, buf_ref => \$buffer ) }, # new_scalar_ref => # sub { my $text = -# File::Slurp::read_file( $file, scalar_ref => 1 ) }, +# File::Slurp::read_file( $file_name, scalar_ref => 1 ) }, # read_file => -# sub { my $text = read_file( $file ) }, +# sub { my $text = read_file( $file_name ) }, # sysread_file => -# sub { my $text = sysread_file( $file ) }, +# sub { my $text = sysread_file( $file_name ) }, # orig_read_file => -# sub { my $text = orig_read_file( $file ) }, +# sub { my $text = orig_read_file( $file_name ) }, # 'Slurp.pm scalar' => -# sub { my $text = slurp_scalar( $file ) }, +# sub { my $text = slurp_scalar( $file_name ) }, # file_contents => -# sub { my $text = file_contents( $file ) }, +# sub { my $text = file_contents( $file_name ) }, # file_contents_no_OO => -# sub { my $text = file_contents_no_OO( $file ) }, +# sub { my $text = file_contents_no_OO( $file_name ) }, # } ) ; # cmpthese( $result ) ; @@ -161,30 +233,30 @@ sub bench_spew_list { # my $result = timethese( $dur, { # new => -# sub { my @lines = File::Slurp::read_file( $file ) }, +# sub { my @lines = File::Slurp::read_file( $file_name ) }, # new_array_ref => # sub { my $lines_ref = -# File::Slurp::read_file( $file, array_ref => 1 ) }, +# File::Slurp::read_file( $file_name, array_ref => 1 ) }, # new_in_anon_array => # sub { my $lines_ref = -# [ File::Slurp::read_file( $file ) ] }, +# [ File::Slurp::read_file( $file_name ) ] }, # read_file => -# sub { my @lines = read_file( $file ) }, +# sub { my @lines = read_file( $file_name ) }, # sysread_file => -# sub { my @lines = sysread_file( $file ) }, +# sub { my @lines = sysread_file( $file_name ) }, # orig_read_file => -# sub { my @lines = orig_read_file( $file ) }, +# sub { my @lines = orig_read_file( $file_name ) }, # 'Slurp.pm to array' => -# sub { my @lines = slurp_array( $file ) }, +# sub { my @lines = slurp_array( $file_name ) }, # orig_slurp_to_array_ref => -# sub { my $lines_ref = orig_slurp_to_array( $file ) }, +# sub { my $lines_ref = orig_slurp_to_array( $file_name ) }, # } ) ; # cmpthese( $result ) ; @@ -196,79 +268,6 @@ sub bench_spew_list { ########################### -sub print_file { - - my( $file_name ) = shift ; - - local( *FH ) ; - - open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; - - print FH @_ ; -} - -sub print_file2 { - - my( $file_name ) = shift ; - - local( *FH ) ; - - my $mode = ( -e $file_name ) ? '<' : '>' ; - - open( FH, "+$mode$file_name" ) || carp "can't create $file_name $!" ; - - print FH @_ ; -} - -sub print_join_file { - - my( $file_name ) = shift ; - - local( *FH ) ; - - my $mode = ( -e $file_name ) ? '<' : '>' ; - - open( FH, "+$mode$file_name" ) || carp "can't create $file_name $!" ; - - print FH join( '', @_ ) ; -} - - -sub syswrite_join_file { - - my( $file_name ) = shift ; - - local( *FH ) ; - - open( FH, ">$file_name" ) || carp "can't create $file_name $!" ; - - syswrite( FH, join( '', @_ ) ) ; -} - -sub sysopen_syswrite_join_file { - - my( $file_name ) = shift ; - - local( *FH ) ; - - sysopen( FH, $file_name, O_WRONLY | O_CREAT ) || - carp "can't create $file_name $!" ; - - syswrite( FH, join( '', @_ ) ) ; -} - -sub orig_write_file -{ - my ($f, @data) = @_; - - local(*F); - - open(F, ">$f") || croak "open >$f: $!"; - (print F @data) || croak "write $f: $!"; - close(F) || croak "close $f: $!"; - return 1; -} - ####################### # top level subs for script @@ -282,6 +281,7 @@ sub parse_options { direction|d=s context|c=s sizes|s=s + unlink|u legend|key|l|k help|usage ) ) ; @@ -377,22 +377,76 @@ sub parse_options { sub legend { die <<'LEGEND' ; -k -Key to Slurp/Spew Benchmarks +Legend for the Slurp Benchmark Entries +In all cases below 'FS' or 'F::S' means the current File::Slurp module +is being used in the benchmark. The full name and description will say +which options are being used. -Write a list of lines to a file +These benchmarks write a list of lines to a file. Use the direction option +of 'out' or 'both' and the context option is 'list' or 'both'. + + Key Description/Source + --- ------------------ + FS::write_file Current F::S write_file + FS::write_file Aref Current F::S write_file on array ref of data + print Open a file and call print() on the list data + print/join Open a file and call print() on the joined + list data + syswrite/join Open a file, call syswrite on joined list data + sysopen/syswrite Sysopen a file, call syswrite on joined + list data + original write_file write_file code from original File::Slurp + (pre-version 9999.*) + +These benchmarks write a scalar to a file. Use the direction option +of 'out' or 'both' and the context option is 'scalar' or 'both'. Key Description/Source --- ------------------ + FS::write_file Current F::S write_file + FS::write_file Sref Current F::S write_file of scalar ref of data + print Open a file and call print() on the scalar data + syswrite_file Open a file, call syswrite on scalar data + syswrite_file_ref Open a file, call syswrite on scalar ref of data + orig_write_file write_file code from original File::Slurp + (pre-version 9999.*) + +These benchmarks slurp a file into an array. Use the direction option +of 'in' or 'both' and the context option is 'list' or 'both'. + +FIX THIS - FS:write_file Current File::Slurp::write_file - FS:write_file_ref Current File::Slurp::write_file (scalar ref) - print Open a file and call print() - syswrite/join Open a file, call syswrite on joined lines - sysopen/syswrite Sysopen a file, call syswrite on joined lines - original write_file Original (pre 9999.*) File::Slurp::write_file + Key Description/Source + --- ------------------ + FS::write_file Current F::S write_file + FS::write_file Aref Current F::S write_file on array ref of data + print Open a file and call print() on the list data + print/join Open a file and call print() on the joined + list data + syswrite/join Open a file, call syswrite on joined list data + sysopen/syswrite Sysopen a file, call syswrite on joined + list data + original write_file write_file code from original File::Slurp + (pre-version 9999.*) + +These benchmarks slurp a file into a scalar. Use the direction option +of 'in' or 'both' and the context option is 'scalar' or 'both'. + +FIX THIS + Key Description/Source + --- ------------------ + FS::write_file Current F::S write_file + FS::write_file Aref Current F::S write_file on array ref of data + print Open a file and call print() on the list data + print/join Open a file and call print() on the joined + list data + syswrite/join Open a file, call syswrite on joined list data + sysopen/syswrite Sysopen a file, call syswrite on joined + list data + original write_file write_file code from original File::Slurp + (pre-version 9999.*) LEGEND } @@ -424,6 +478,9 @@ Usage: $0 [--iterations=] [--direction=] [--context=] integers. You can use 'k' or 'm' as suffixes for 1024 and 1024**2. Default is '500,1k,1m'. + --unlink Unlink the written file before each time + -u a file is written + --legend Print out a legend of all the benchmark entries. --key -l @@ -438,39 +495,6 @@ DIE __END__ -sub bench_scalar_spew { - - my ( $size ) = @_ ; - - print "\n\nScalar Spew of $size file\n\n" ; - - my $result = timethese( $dur, { - - new => - sub { File::Slurp::write_file( $file, $text ) }, - - new_ref => - sub { File::Slurp::write_file( $file, \$text ) }, - - print_file => - sub { print_file( $file, $text ) }, - - print_join_file => - sub { print_join_file( $file, $text ) }, - - syswrite_file => - sub { syswrite_file( $file, $text ) }, - - syswrite_file2 => - sub { syswrite_file2( $file, $text ) }, - - orig_write_file => - sub { orig_write_file( $file, $text ) }, - - } ) ; - - cmpthese( $result ) ; -} sub bench_scalar_slurp { @@ -483,35 +507,35 @@ sub bench_scalar_slurp { my $result = timethese( $dur, { new => - sub { my $text = File::Slurp::read_file( $file ) }, + sub { my $text = File::Slurp::read_file( $file_name ) }, new_buf_ref => sub { my $text ; - File::Slurp::read_file( $file, buf_ref => \$text ) }, + File::Slurp::read_file( $file_name, buf_ref => \$text ) }, new_buf_ref2 => sub { - File::Slurp::read_file( $file, buf_ref => \$buffer ) }, + File::Slurp::read_file( $file_name, buf_ref => \$buffer ) }, new_scalar_ref => sub { my $text = - File::Slurp::read_file( $file, scalar_ref => 1 ) }, + File::Slurp::read_file( $file_name, scalar_ref => 1 ) }, read_file => - sub { my $text = read_file( $file ) }, + sub { my $text = read_file( $file_name ) }, sysread_file => - sub { my $text = sysread_file( $file ) }, + sub { my $text = sysread_file( $file_name ) }, orig_read_file => - sub { my $text = orig_read_file( $file ) }, + sub { my $text = orig_read_file( $file_name ) }, orig_slurp => - sub { my $text = orig_slurp_to_scalar( $file ) }, + sub { my $text = orig_slurp_to_scalar( $file_name ) }, file_contents => - sub { my $text = file_contents( $file ) }, + sub { my $text = file_contents( $file_name ) }, file_contents_no_OO => - sub { my $text = file_contents_no_OO( $file ) }, + sub { my $text = file_contents_no_OO( $file_name ) }, } ) ; cmpthese( $result ) ; @@ -526,30 +550,30 @@ sub bench_list_slurp { my $result = timethese( $dur, { new => - sub { my @lines = File::Slurp::read_file( $file ) }, + sub { my @lines = File::Slurp::read_file( $file_name ) }, new_array_ref => sub { my $lines_ref = - File::Slurp::read_file( $file, array_ref => 1 ) }, + File::Slurp::read_file( $file_name, array_ref => 1 ) }, new_in_anon_array => sub { my $lines_ref = - [ File::Slurp::read_file( $file ) ] }, + [ File::Slurp::read_file( $file_name ) ] }, read_file => - sub { my @lines = read_file( $file ) }, + sub { my @lines = read_file( $file_name ) }, sysread_file => - sub { my @lines = sysread_file( $file ) }, + sub { my @lines = sysread_file( $file_name ) }, orig_read_file => - sub { my @lines = orig_read_file( $file ) }, + sub { my @lines = orig_read_file( $file_name ) }, orig_slurp_to_array => - sub { my @lines = orig_slurp_to_array( $file ) }, + sub { my @lines = orig_slurp_to_array( $file_name ) }, orig_slurp_to_array_ref => - sub { my $lines_ref = orig_slurp_to_array( $file ) }, + sub { my $lines_ref = orig_slurp_to_array( $file_name ) }, } ) ; cmpthese( $result ) ;