File Coverage

blib/lib/DBI/Shell/Timing.pm
Criterion Covered Total %
statement 29 29 100.0
branch 12 12 100.0
condition n/a
subroutine 7 7 100.0
pod 0 3 0.0
total 48 51 94.1


line stmt bran cond sub pod time code
1             #
2             # Package meta, adds meta database commands to dbish
3             #
4             package DBI::Shell::Timing;
5              
6             our $VERSION = '11.97'; # VERSION
7              
8 4     4   29 use strict;
  4         9  
  4         156  
9 4     4   22 use vars qw(@ISA);
  4         8  
  4         207  
10 4     4   2346 use Benchmark qw(timeit timestr);
  4         27821  
  4         26  
11              
12             sub init {
13 4     4 0 12 my ($self, $sh, @arg) = @_;
14              
15              
16 4         53 $sh->install_options(
17             [
18             [ 'timing_style' => qq{auto} ],
19             [ 'timing_timing' => 1 ], # Set the default to on
20             [ 'timing_format' => '5.2f' ],
21             [ 'timing_prefix' => 'Elapsed: ' ],
22             ]);
23 4         15 my $com_ref = $sh->{commands};
24             $com_ref->{timing} = {
25 4         20 hint =>
26             "timing: on/off (1/0) display execute time upon completion of command",
27             };
28            
29 4         14 return $self;
30             }
31              
32             sub do_timing {
33 19     19 0 45 my $self = shift;
34 19 100       49 if (@_) {
35 8         14 my $t = shift;
36             # $self->log( qq{timing called with $t} );
37 8 100       57 $t = 0 if ($t =~ m/off|stop|end/i);
38 8 100       35 $t = 1 if ($t =~ m/on|start|begin/i);
39 8 100       24 $self->{timing_timing} = ($t?1:0);
40             }
41 19 100       112 $self->print_buffer(qq{timing: } . ($self->{timing_timing}? 'on': 'off'));
42 19         151 return $self->{timing_timing};
43             }
44              
45              
46             #
47             # Subclass the do_go command to include the timing options. I'm not
48             # sure which is better, to subclass this command or completely
49             # override it.
50             #
51             sub do_go {
52 40     40 0 93 my $self = shift;
53 40     40   325 my $rv = timeit( 1, sub { $self->DBI::Shell::Base::do_go( @_ ) } );
  40         610182  
54 40 100       4084 if ($self->{timing_timing}) {
55             my $str = $self->{timing_prefix} .
56 39         179 timestr( $rv, $self->{timing_style}, $self->{timing_format} );
57 39         3373 $self->log( $str );
58             }
59 40         419 return;
60             }
61              
62             my $_unimp = qq{timing: not implemented yet};
63              
64             1;