File Coverage

blib/lib/Devel/MojoProf/Reporter.pm
Criterion Covered Total %
statement 24 26 92.3
branch 7 12 58.3
condition 3 3 100.0
subroutine 4 4 100.0
pod 1 1 100.0
total 39 46 84.7


line stmt bran cond sub pod time code
1             package Devel::MojoProf::Reporter;
2 6     6   35 use Mojo::Base -base;
  6         8  
  6         33  
3              
4 6     6   2836 use Mojo::File 'path';
  6         143982  
  6         2257  
5              
6             has handler => undef;
7             has out_csv => $ENV{DEVEL_MOJOPROF_OUT_CSV};
8              
9             # Note that $prof is just here to be back compat
10             sub report {
11 14     14 1 65 my ($self, $report, $prof) = @_;
12              
13 14 100 100     94 if ($self->{out_fh} ||= $self->_build_out_fh) {
14 11         31 my $message = $report->{message};
15 11         28 $message =~ s!"!""!g;
16 11         612 return printf {$self->{out_fh}} qq(%s,%.5f,%s,%s,%s,%s,"%s"\n), $report->{t0}[0],
17 11         18 @$report{qw(elapsed class method file line)}, $message;
18             }
19              
20 3 50       40 return $self->{handler}->($prof, $report) if $self->{handler};
21 0 0       0 return printf STDERR "%.5fms [%s::%s] %s\n", @$report{qw(elapsed class method message)} unless $report->{line};
22 0         0 return printf STDERR "%.5fms [%s::%s] %s at %s line %s\n", @$report{qw(elapsed class method message file line)};
23             }
24              
25             sub _build_out_fh {
26 4     4   9 my $self = shift;
27 4 100       21 my $path = $self->out_csv or return;
28              
29 1 50       15 $path = "devel-mojoprof-reporter-$^T.csv" if $path eq '1';
30 1 50       30 die "[Devel::MojoProf] Cannot overwrite existing $path report.\n" if -e $path;
31              
32 1         6 $path = path $path;
33 1         18 my $fh = $path->open('>');
34 1         487 $fh->autoflush(1);
35 1         47 printf {$fh} "%s\n", join ',', qw(t0 elapsed class method file line message);
  1         58  
36 1         9 $self->out_csv($path->to_abs);
37 1         66 return $fh;
38             }
39              
40             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 is an object that is capable of reporting how long
51             certain operations take.
52              
53             See L 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 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 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 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.
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 and C keys can be disabled by setting the
109             C 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.
115              
116             =cut