File Coverage

lib/Spoon/Trace.pm
Criterion Covered Total %
statement 12 48 25.0
branch 0 18 0.0
condition 0 3 0.0
subroutine 4 11 36.3
pod 0 6 0.0
total 16 86 18.6


line stmt bran cond sub pod time code
1             package Spoon::Trace;
2 1     1   2092 use Spiffy -Base;
  1         3  
  1         10  
3 1     1   1080 use Time::HiRes qw(gettimeofday);
  1     1   3  
  1     1   35  
  1         7  
  1         3  
  1         47  
  1         1376  
  1         2628  
  1         7  
4              
5             my $global_self;
6              
7             field data => [];
8             field show_num => 1;
9             field show_time => 1;
10             field auto_print => 0;
11             field auto_warn => 0;
12             field add_label => 0;
13              
14 0     0 0   sub mode1 {
15 0           $self->show_num(0);
16 0           $self->show_time(0);
17 0           $self->add_label(1);
18 0           return $self;
19             }
20              
21             sub trace() {
22 0 0   0 0   $global_self = defined $global_self
23             ? $global_self
24             : Spoon::Trace->new;
25             }
26              
27 0     0 0   sub mark {
28 0 0 0       my $label = @_
29             ? join(' ', @_) . ($self->add_label && "\t(" . $self->get_label . ')')
30             : $self->get_label;
31 0           my $data = $self->data;
32 0           my ($seconds, $microseconds) = gettimeofday;
33 0           push @$data, +{
34             label => $label,
35             time => $seconds + $microseconds / 1000000,
36             };
37 0           return $self;
38             }
39              
40 0     0 0   sub get_label {
41 0 0         my $i = (caller(2))[3] eq 'Spoon::Base::t' ? 1 : 0;
42 0           my $line = (caller(1 + $i))[2];
43 0           my $sub = (caller(2 + $i))[3];
44 0           return "$sub,$line";
45             }
46              
47 0     0 0   sub clear {
48 0           $global_self = undef;
49 0           $self->data([]);
50 0           return $self;
51             }
52              
53 0     0 0   sub report {
54 0           my $data = $self->data;
55 0           my $output = '';
56 0 0         return $output unless @$data;
57 0           my $base_time = $data->[0]{time};
58 0           for (my $i = 0; $i < @$data; $i++) {
59 0 0         if ($self->show_num) {
60 0           $output .= sprintf "%03d) ", $i + 1;
61             }
62 0 0         if ($self->show_time) {
63 0 0         $output .= sprintf "%2.4f %2.2f ",
64             $i ? $data->[$i]{time} - $data->[$i - 1]{time} : 0,
65             $data->[$i]{time} - $base_time;
66             }
67 0           $output .= $data->[$i]{label} . "\n";
68             }
69 0           return $output;
70             }
71              
72 0     0     sub DESTROY {
73 0 0         print $self->report
74             if $self->auto_print;
75 0 0         warn $self->report
76             if $self->auto_warn;
77             }