7 use Benchmark qw( timethese cmpthese ) ;
10 use Fcntl qw( :DEFAULT :seek );
14 my $file = 'slurp_data' ;
16 GetOptions (\my %opts,
17 qw( slurp spew scalar list sizes=s key duration=i help ) ) ;
27 my( @lines, $text, $size ) ;
31 foreach my $size ( @{$opts{sizes}} ) {
33 @lines = ( 'a' x 80 . "\n") x $size ;
34 $text = join( '', @lines ) ;
35 $size = length $text ;
37 File::Slurp::write_file( $file, $text ) ;
39 # bench_list_slurp( $size ) if $opts{list} && $opts{slurp} ;
40 # bench_scalar_slurp( $size ) if $opts{scalar} && $opts{slurp} ;
42 # bench_scalar_spew( $size ) if $opts{scalar} && $opts{spew} ;
48 return unless $opts{list} && $opts{spew} ;
50 print "\n\nWriting (Spew) a list of lines: Size = $size bytes\n\n" ;
52 my $result = timethese( $opts{duration}, {
55 sub { File::Slurp::write_file( $file, @lines ) },
58 sub { print_file( $file, @lines ) },
61 sub { print_join_file( $file, @lines ) },
64 sub { syswrite_join_file( $file, @lines ) },
66 'original write_file' =>
67 sub { orig_write_file( $file, @lines ) },
74 # sub bench_scalar_spew {
78 # print "\n\nScalar Spew of $size file\n\n" ;
80 # my $result = timethese( $dur, {
83 # sub { File::Slurp::write_file( $file, $text ) },
86 # sub { File::Slurp::write_file( $file, \$text ) },
89 # sub { print_file( $file, $text ) },
92 # sub { print_join_file( $file, $text ) },
95 # sub { syswrite_file( $file, $text ) },
98 # sub { syswrite_file2( $file, $text ) },
101 # sub { orig_write_file( $file, $text ) },
105 # cmpthese( $result ) ;
108 # sub bench_scalar_slurp {
110 # my ( $size ) = @_ ;
112 # print "\n\nScalar Slurp of $size file\n\n" ;
116 # my $result = timethese( $dur, {
119 # sub { my $text = File::Slurp::read_file( $file ) },
123 # File::Slurp::read_file( $file, buf_ref => \$text ) },
126 # File::Slurp::read_file( $file, buf_ref => \$buffer ) },
129 # File::Slurp::read_file( $file, scalar_ref => 1 ) },
132 # sub { my $text = read_file( $file ) },
135 # sub { my $text = sysread_file( $file ) },
138 # sub { my $text = orig_read_file( $file ) },
140 # 'Slurp.pm scalar' =>
141 # sub { my $text = slurp_scalar( $file ) },
144 # sub { my $text = file_contents( $file ) },
146 # file_contents_no_OO =>
147 # sub { my $text = file_contents_no_OO( $file ) },
150 # cmpthese( $result ) ;
153 # sub bench_list_slurp {
155 # my ( $size ) = @_ ;
157 # print "\n\nList Slurp of $size file\n\n" ;
159 # my $result = timethese( $dur, {
162 # sub { my @lines = File::Slurp::read_file( $file ) },
165 # sub { my $lines_ref =
166 # File::Slurp::read_file( $file, array_ref => 1 ) },
168 # new_in_anon_array =>
169 # sub { my $lines_ref =
170 # [ File::Slurp::read_file( $file ) ] },
173 # sub { my @lines = read_file( $file ) },
176 # sub { my @lines = sysread_file( $file ) },
179 # sub { my @lines = orig_read_file( $file ) },
181 # 'Slurp.pm to array' =>
182 # sub { my @lines = slurp_array( $file ) },
184 # orig_slurp_to_array_ref =>
185 # sub { my $lines_ref = orig_slurp_to_array( $file ) },
188 # cmpthese( $result ) ;
192 ###########################
193 # write file benchmark subs
194 ###########################
199 my( $file_name ) = shift ;
203 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;
210 my( $file_name ) = shift ;
214 my $mode = ( -e $file_name ) ? '<' : '>' ;
216 open( FH, "+$mode$file_name" ) || carp "can't create $file_name $!" ;
221 sub print_join_file {
223 my( $file_name ) = shift ;
227 my $mode = ( -e $file_name ) ? '<' : '>' ;
229 open( FH, "+$mode$file_name" ) || carp "can't create $file_name $!" ;
231 print FH join( '', @_ ) ;
235 sub syswrite_join_file {
237 my( $file_name ) = shift ;
241 open( FH, ">$file_name" ) || carp "can't create $file_name $!" ;
243 syswrite( FH, join( '', @_ ) ) ;
246 sub sysopen_syswrite_join_file {
248 my( $file_name ) = shift ;
252 sysopen( FH, $file_name, O_WRONLY | O_CREAT ) ||
253 carp "can't create $file_name $!" ;
255 syswrite( FH, join( '', @_ ) ) ;
264 open(F, ">$f") || croak "open >$f: $!";
265 (print F @data) || croak "write $f: $!";
266 close(F) || croak "close $f: $!";
271 #######################
272 # top level subs for script
274 #######################
278 help() if $opts{help} ;
280 key() if $opts{key} ;
282 unless( $opts{spew} || $opts{slurp} ) {
288 unless( $opts{list} || $opts{scalar} ) {
294 $opts{sizes} = [split ',', ( $opts{sizes} || '10,100,1000' ) ];
296 $opts{duration} ||= -2 ;
303 Key to Slurp/Spew Benchmarks
306 Write a list of lines to a file
308 Key Description/Source
309 --- ------------------
311 FS:write_file Current File::Slurp::write_file
312 FS:write_file_ref Current File::Slurp::write_file (scalar ref)
313 print Open a file and call print()
314 syswrite/join Open a file, call syswrite on joined lines
315 sysopen/syswrite Sysopen a file, call syswrite on joined lines
316 original write_file Original (pre 9999.*) File::Slurp::write_file
328 Usage: $0 [--list] [--scalar] [--slurp] [--spew]
329 [--sizes=10,100] [--help]
331 --list Run the list context benchmarks
332 --scalar Run the scalar context benchmarks
333 Those default to on unless one is set
335 --slurp Run the slurp benchmarks
336 --spew Run the spew benchmarks
337 Those default to on unless one is set
339 --sizes Comma separated list of file sizes to benchmark
340 Defaults to 10,100,1000
342 --key Print the benchmark names and code orig_ins
344 --help Print this help text
354 sub bench_scalar_spew {
358 print "\n\nScalar Spew of $size file\n\n" ;
360 my $result = timethese( $dur, {
363 sub { File::Slurp::write_file( $file, $text ) },
366 sub { File::Slurp::write_file( $file, \$text ) },
369 sub { print_file( $file, $text ) },
372 sub { print_join_file( $file, $text ) },
375 sub { syswrite_file( $file, $text ) },
378 sub { syswrite_file2( $file, $text ) },
381 sub { orig_write_file( $file, $text ) },
385 cmpthese( $result ) ;
388 sub bench_scalar_slurp {
392 print "\n\nScalar Slurp of $size file\n\n" ;
396 my $result = timethese( $dur, {
399 sub { my $text = File::Slurp::read_file( $file ) },
403 File::Slurp::read_file( $file, buf_ref => \$text ) },
406 File::Slurp::read_file( $file, buf_ref => \$buffer ) },
409 File::Slurp::read_file( $file, scalar_ref => 1 ) },
412 sub { my $text = read_file( $file ) },
415 sub { my $text = sysread_file( $file ) },
418 sub { my $text = orig_read_file( $file ) },
421 sub { my $text = orig_slurp_to_scalar( $file ) },
424 sub { my $text = file_contents( $file ) },
426 file_contents_no_OO =>
427 sub { my $text = file_contents_no_OO( $file ) },
430 cmpthese( $result ) ;
433 sub bench_list_slurp {
437 print "\n\nList Slurp of $size file\n\n" ;
439 my $result = timethese( $dur, {
442 sub { my @lines = File::Slurp::read_file( $file ) },
445 sub { my $lines_ref =
446 File::Slurp::read_file( $file, array_ref => 1 ) },
449 sub { my $lines_ref =
450 [ File::Slurp::read_file( $file ) ] },
453 sub { my @lines = read_file( $file ) },
456 sub { my @lines = sysread_file( $file ) },
459 sub { my @lines = orig_read_file( $file ) },
461 orig_slurp_to_array =>
462 sub { my @lines = orig_slurp_to_array( $file ) },
464 orig_slurp_to_array_ref =>
465 sub { my $lines_ref = orig_slurp_to_array( $file ) },
468 cmpthese( $result ) ;
471 ######################################
472 # uri's old fast slurp
476 my( $file_name ) = shift ;
479 open( FH, $file_name ) || carp "can't open $file_name $!" ;
481 return <FH> if wantarray ;
485 read( FH, $buf, -s FH ) ;
491 my( $file_name ) = shift ;
494 open( FH, $file_name ) || carp "can't open $file_name $!" ;
496 return <FH> if wantarray ;
500 sysread( FH, $buf, -s FH ) ;
504 ######################################
505 # from File::Slurp.pm on cpan
511 local($/) = wantarray ? $/ : undef;
516 open(F, "<$file") || croak "open $file: $!";
518 close(F) || croak "close $file: $!";
520 return $r[0] unless wantarray;
525 ######################################
526 # from Slurp.pm on cpan
529 local( $/, @ARGV ) = ( wantarray ? $/ : undef, @_ );
534 my @array = slurp( @_ );
535 return wantarray ? @array : \@array;
539 my $scalar = slurp( @_ );
543 ######################################
544 # very slow slurp code used by a client
548 my $fh = new FileHandle $file or
549 warn("Util::file_contents:Can't open file $file"), return '';
550 return join '', <$fh>;
553 # same code but doesn't use FileHandle.pm
555 sub file_contents_no_OO {
559 open( FH, $file ) || carp "can't open $file $!" ;
561 return join '', <FH>;
564 ##########################