File Coverage

blib/lib/Benchmark/Forking.pm
Criterion Covered Total %
statement 29 31 93.5
branch 5 8 62.5
condition n/a
subroutine 9 11 81.8
pod 3 4 75.0
total 46 54 85.1


line stmt bran cond sub pod time code
1             package Benchmark::Forking;
2              
3             $VERSION = 1.01;
4              
5 9     9   75033 use Benchmark;
  9         80397  
  9         99  
6             require Exporter;
7              
8 9     9   1368 use strict;
  9         27  
  9         351  
9 9     9   54 use vars qw( $Enabled $RunLoop );
  9         72  
  9         3177  
10              
11 9     9   90 sub import { enable(); Exporter::export_to_level('Benchmark', 1, @_) }
  9         21402  
12 0     0   0 sub unimport { disable() }
13              
14 14     14 1 61647 sub enable { $Enabled = 1 }
15 5     5 1 3180 sub disable { $Enabled = 0 }
16 0 0   0 1 0 sub enabled { ( $#_ > 0 ) ? $Enabled = $_[1] : $Enabled }
17              
18             # The runloop sub uses a special open() call that causes our process to fork,
19             # with a filehandle acting as an IO channel from the child back to the parent.
20             # The child runs the timing loop and prints the values from the Benchmark
21             # result object to its STDOUT, then it exits, terminating the child process.
22             # The output from the child appears in the main process' FORK handle, which
23             # is read, re-blessed to form a proper Benchmark result object, and returned.
24              
25             sub runloop {
26 64 100   64 0 195044 $Enabled or return &$RunLoop;
27            
28 44 100       57855 if ( not open( FORK, '-|' ) ) {
29 8         715 print join "\n", @{ &$RunLoop };
  8         783  
30 8         142487 exit;
31             } else {
32 36         10570686 my @td = ;
33 36 50       2789 close( FORK ) or die $!;
34 36         1389 return bless \@td, 'Benchmark';
35             }
36             }
37              
38             # The BEGIN block captures a reference to the normal Benchmark runloop sub to
39             # be called by the wrapper, then installs our sub in the original's place.
40              
41             BEGIN {
42 9     9   18 $Enabled = 1;
43 9         27 $RunLoop = \&Benchmark::runloop;
44 9     9   54 no strict 'refs';
  9         18  
  9         459  
45 9         45 local $^W; # avoid sub redefined warning
46 9         18 *{'Benchmark::runloop'} = \&runloop;
  9         288  
47             }
48              
49             1;
50              
51             __END__