blib/lib/Catmandu/Fix/Bind/benchmark.pm

filter: Statementstmt 97.3% 37 / 38 Branchbran 75.0% 3 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 1 totaltotal 94.0% 47 / 50 CC 4 SCAR 5.9
CC
4
Cov
95%
DESTROY
3.0
bind
2.0
BEGIN
1.0
CRAP 1.8
« blib/lib/Catmandu/Fix/substring.pm ↑ Summary blib/lib/Catmandu/MultiIterator.pm »
1 package Catmandu::Fix::Bind::benchmark;
2  
3 1 ▶use Catmandu::Sane;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '1.0201_02';
6  
7 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 with 'Catmandu::Fix::Bind';
12  
13 1 has output => (is => 'ro', required => 1);
14 1 has stats => (is => 'lazy');
15  
16 sub _build_stats {
17 10 ▶ +{};
Subroutine _build_stats: called CC=1 SCAR=0.0
18 }
19  
20 sub bind {
21 14 ▶ my ($self, $data, $code, $name) = @_;
Subroutine bind: called CC=2 SCAR=6.9
Pod undocumented
22 14 ▶ $name = '<undef>' unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 6 8 14
23 14 my $t0 = [gettimeofday];
24 14 $data = $code->($data);
25 13 my $elapsed = tv_interval($t0);
26  
27 13 $self->stats->{$name}->{count} += 1;
28 13 $self->stats->{$name}->{elapsed} += $elapsed;
29  
30 13 $data;
31 }
32  
33 sub DESTROY {
34 10 ▶ my ($self) = @_;
Subroutine DESTROY: called CC=3 SCAR=11.1
35 10 local (*OUT);
36 10 ▶ open(OUT, '>', $self->output) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
open OUT, ">", $self->output or return undef 0 10 10
37  
38 10 printf OUT "%-8.8s\t%-40.40s\t%-8.8s\t%-8.8s\n", 'elapsed', 'command',
39 'calls', 'sec/command';
40 10 printf OUT "-" x 100 . "\n";
41  
42 10 for my $key (
43 0 sort {$self->stats->{$b}->{elapsed} cmp $self->stats->{$a}->{elapsed}}
44 10 keys %{$self->stats}
45 )
46 {
47 my $speed
48 8 = $self->stats->{$key}->{elapsed} / $self->stats->{$key}->{count};
49 printf OUT "%f\t%-40.40s\t%d times\t%f secs/command\n",
50 $self->stats->{$key}->{elapsed}, $key,
51 8 $self->stats->{$key}->{count}, $speed;
52 }
53  
54 10 printf OUT "\n\n";
55 10 close(OUT);
56 }
57  
58 1 1;
59  
60 __END__
61  
62 =pod
63  
64 =head1 NAME
65  
66 Catmandu::Fix::Bind::benchmark - a binder that calculates the execution time of Fix functions
67  
68 =head1 SYNOPSIS
69  
70 do benchmark(output => /dev/stderr)
71 foo()
72 end
73  
74 # will create as side effect computation statistics on the stderr
75  
76 elapsed command calls sec/comm
77 -------------------------------------------------------------
78 0.000006 Catmandu::Fix::foo 1 times 0.000006 secs/command
79  
80 =head1 DESCRIPTION
81  
82 The benchmark binder computes all the Fix function plus as side effect
83 calculates the execution time of all wrapped functions over all input records.
84  
85 =head1 CONFIGURATION
86  
87 =head2 output
88  
89 Required. The path of a file to which the benchmark statistics will be written.
90  
91 =head1 SEE ALSO
92  
93 L<Catmandu::Fix::Bind>
94  
95 =cut
« blib/lib/Catmandu/Fix/substring.pm ↑ Summary blib/lib/Catmandu/MultiIterator.pm »