MojoProf/Reporter.pm

filter: Statementstmt 93.1% 27 / 29 Branchbran 58.3% 7 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 86.2% 44 / 51 CC 8 SCAR 14.3
CC
8
Cov
84%
report
5.5
_build_out_fh
4.0
BEGIN
1.0
CRAP 4.2
↑ Summary MojoProf.pm »
1 package Devel::MojoProf::Reporter;
2 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use Mojo::File 'path';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 has handler => undef;
7 6 has out_csv => $ENV{DEVEL_MOJOPROF_OUT_CSV};
8  
9 # Note that $prof is just here to be back compat
10 14 ▶sub report {
Subroutine report: called CC=5 SCAR=17.1
Pod documented
11 14 my ($self, $report, $prof) = @_;
12  
13 14 ▶ if ($self->{out_fh} ||= $self->_build_out_fh) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"out_fh"} ||= $self->_build_out_fh) 11 3 14
Condition100%3/3
$self->{"out_fh"} ||= $self->_build_out_fh
l!l&&r!l&&!r
1013
MC/DC100%2/2
$self->{"out_fh"} ||= $self->_build_out_fh
$self->{"out_fh"} $self->_build_out_fh
Truth table100%3/3
A ||= B
A= $self->{"out_fh"}B= $self->_build_out_fh
ABresult
1X1
011
000
14 11 my $message = $report->{message};
15 11 $message =~ s!"!""!g;
16 11 return printf {$self->{out_fh}} qq(%s,%.5f,%s,%s,%s,%s,"%s"\n), $report->{t0}[0],
17 11 @$report{qw(elapsed class method file line)}, $message;
18 }
19  
20 3 ▶ return $self->{handler}->($prof, $report) if $self->{handler};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"handler"} 3 0 3
21 0 ▶ return printf STDERR "%.5fms [%s::%s] %s\n", @$report{qw(elapsed class method message)} unless $report->{line};
Branch: 1 branch
BranchTrueFalseTotal
unless $report->{"line"} 0 0 0
22 0 return printf STDERR "%.5fms [%s::%s] %s at %s line %s\n", @$report{qw(elapsed class method message file line)};
23 }
24  
25 4 ▶sub _build_out_fh {
Subroutine _build_out_fh: called CC=4 SCAR=13.9
26 4 my $self = shift;
27 4 ▶ my $path = $self->out_csv or return;
Branch: 1 branch
BranchTrueFalseTotal
my $path = $self->out_csv or return 1 3 4
28  
29 1 ▶ $path = "devel-mojoprof-reporter-$^T.csv" if $path eq '1';
Branch: 1 branch
BranchTrueFalseTotal
if $path eq 1 1 0 1
30 1 ▶ die "[Devel::MojoProf] Cannot overwrite existing $path report.\n" if -e $path;
Branch: 1 branch
BranchTrueFalseTotal
if -e $path 0 1 1
31  
32 1 $path = path $path;
33 1 my $fh = $path->open('>');
34 1 $fh->autoflush(1);
35 1 printf {$fh} "%s\n", join ',', qw(t0 elapsed class method file line message);
36 1 $self->out_csv($path->to_abs);
37 1 return $fh;
38 }
39  
40 6 1;
41  
42 =encoding utf8
43  
44 =head1 NAME
45  
46 Devel::MojoProf::Reporter - Default mojo profile reporter
47  
48 =head1 DESCRIPTION
49  
50 L<Devel::MojoProf::Reporter> is an object that is capable of reporting how long
51 certain operations take.
52  
53 See L<Devel::MojoProf> for how to use this.
54  
55 =head1 ATTRIBUTES
56  
57 =head2 handler
58  
59 my $cb = $reporter->handler;
60 my $reporter = $reporter->handler(sub { ... });
61  
62 Only useful to be back compat with L<Devel::MojoProf> 0.01:
63  
64 $prof->reporter(sub { ... });
65  
66 Will be removed in the future.
67  
68 =head2 out_csv
69  
70 $str = $reporter->out_csv;
71 $reporter = $reporter->out_csv("/path/to/file.csv");
72  
73 Setting this attribute will cause L</report> to print the results to a CSV
74 file, instead of printing to STDERR. This will allow you to post-process
75 the information in a structured way in your favorite spreadsheet editor.
76  
77 You can also set the environment variable C<DEVEL_MOJOPROF_OUT_CSV> to a given
78 file or give it a special value "1", which will generate a file in the current
79 directory for you, with the filename "devel-mojoprof-reporter-1548746277.csv",
80 where "1548746277" will be the unix timestamp of when you started the run.
81  
82 =head1 METHODS
83  
84 =head2 report
85  
86 $reporter->report(\%report);
87  
88 Will be called every time a meassurement has been done by L<Devel::MojoProf>.
89  
90 The C<%report> variable contains the following example information:
91  
92 {
93 file => "path/to/app.pl",
94 line => 23,
95 class => "Mojo::Pg::Database",
96 method => "query_p",
97 t0 => [Time::HiRes::gettimeofday],
98 elapsed => Time::HiRes::tv_interval($report->{t0}),
99 message => "SELECT 1 as whatever",
100 }
101  
102 The C<%report> above will print the following line to STDERR:
103  
104 0.00038ms [Mojo::Pg::Database::query_p] SELECT 1 as whatever at path/to/app.pl line 23
105  
106 The log format is currently EXPERIMENTAL and could be changed.
107  
108 Note that the C<file> and C<line> keys can be disabled by setting the
109 C<DEVEL_MOJOPROF_CALLER> environment variable to "0". This can be useful to
110 speed up the run of the program.
111  
112 =head1 SEE ALSO
113  
114 L<Devel::MojoProf>.
115  
116 =cut
↑ Summary MojoProf.pm »