MojoProf.pm

filter: Statementstmt 95.6% 88 / 92 Branchbran 69.2% 36 / 52 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 89.2% 25 / 28 Podpod 100% 4 / 4 totaltotal 81.4% 162 / 199 CC 32 SCAR 13.1
CC
32
Cov
83%
__ANON__
8.5
add_profiling_for
7.4
_ensure_loaded
4.1
CRAP 3.7
« MojoProf/Reporter.pm ↑ Summary
1 package Devel::MojoProf;
2 6 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 6 ▶use Class::Method::Modifiers 'install_modifier';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Devel::MojoProf::Reporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Mojo::Loader 'load_class';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Time::HiRes qw(gettimeofday tv_interval);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶use constant CALLER => $ENV{DEVEL_MOJOPROF_CALLER} // 1;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"DEVEL_MOJOPROF_CALLER"} // 1
l!l
06
MC/DC0%0/1
$ENV{"DEVEL_MOJOPROF_CALLER"} // 1
$ENV{"DEVEL_MOJOPROF_CALLER"}
Truth table50%1/2
A
A= $ENV{"DEVEL_MOJOPROF_CALLER"}
Aresult
11
00
11  
12 6 our $VERSION = '0.04';
13  
14 # Required for "perl -d:MojoProf ..."
15 6 ▶DB->can('DB') or *DB::DB = sub { };
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
"DB"->can("DB") or *DB::DB = sub { } 0 6 6
16  
17 8 ▶sub add_profiling_for {
Subroutine add_profiling_for: called CC=7 SCAR=20.0
Pod documented
18 8 ▶ my $params = ref $_[-1] eq 'HASH' ? pop : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'HASH' ? : 0 8 8
19 8 my $self = _instance(shift);
20 8 ▶ return $self->tap($self->can("_add_profiling_for_$_[0]")) if @_ == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 6 2 8
21  
22 2 ▶ return unless my $target = $self->_ensure_loaded(shift);
Branch: 1 branch
BranchTrueFalseTotal
unless my $target = $self->_ensure_loaded(shift()) 0 2 2
23 2 ▶ while (my $method = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = shift()) 2 2 4
24 2 ▶ next if $self->{installed}{$target}{$method}++;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"installed"}{$target}{$method}++ 0 2 2
25 2 ▶ $self->_add_profiling_for_method($target, $method, ref $_[0] ? shift : undef, $params);
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
26 }
27  
28 2 return $self;
29 }
30  
31 5 ▶sub import {
Subroutine import: called CC=3 SCAR=11.1
32 5 my $class = shift;
33 5 ▶ my @flags = @_ ? @_ : qw(-mysql -pg -redis -sqlite -ua);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 5 0 5
34  
35 5 $class->add_profiling_for($_) for map { s!^-!!; $_ } @flags;
36 }
37  
38 6 ▶sub new {
Subroutine new: called CC=2 SCAR=7.6
Pod documented
39 6 my $self = shift->SUPER::new(@_);
40 6 ▶ $self->reporter($self->{reporter} || Devel::MojoProf::Reporter->new);
Condition33%1/3
$self->{'reporter'} || 'Devel::MojoProf::Reporter'->new
l!l&&r!l&&!r
060
MC/DC0%0/2
$self->{'reporter'} || 'Devel::MojoProf::Reporter'->new
$self->{'reporter'} 'Devel::MojoProf::Reporter'->new
Truth table33%1/3
A || B
A= $self->{'reporter'}B= 'Devel::MojoProf::Reporter'->new
ABresult
1X1
011
000
41 6 $self;
42 }
43  
44 11 ▶sub reporter {
Subroutine reporter: called CC=3 SCAR=11.0
Pod documented
45 11 my $self = shift;
46 11 ▶ return $self->{reporter} unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 4 7 11
47 7 ▶ $self->{reporter} = blessed $_[0] ? $_[0] : Devel::MojoProf::Reporter->new->handler($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($_[0]) ? : 6 1 7
48 7 return $self;
49 }
50  
51 7 ▶sub singleton { state $self = __PACKAGE__->new }
Subroutine singleton: called CC=1 SCAR=0.0
Pod documented
52  
53 2 ▶sub _add_profiling_for_method {
Subroutine _add_profiling_for_method: called CC=2 SCAR=7.3
54 2 my ($self, $target, $method, $make_message, $params) = @_;
55  
56 2 my %params = (ignore_caller => qr{^$target}, %$params);
57 0 ▶ $make_message ||= sub { shift; join ' ', @_ };
Subroutine __ANON__: not called CC=1 SCAR=2.6
Condition33%1/3
$make_message ||= sub { shift(); join " ", @_; }
l!l&&r!l&&!r
200
MC/DC0%0/2
$make_message ||= sub { shift(); join " ", @_; }
$make_message sub { shift(); join " ", @_; }
Truth table33%1/3
A ||= B
A= $make_messageB= sub { shift(); join " ", @_; }
ABresult
1X1
011
000
58  
59 13 ▶ install_modifier $target => around => $method => sub {
Subroutine __ANON__: called CC=8 SCAR=21.4
60 13 my ($orig, @args) = @_;
61 13 my $wantarray = wantarray;
62 13 my %report = (class => $target, method => $method);
63  
64 13 _add_caller_to_report(\%params, \%report) if CALLER;
65  
66 13 ▶ my $cb = ref $args[-1] eq 'CODE' ? pop @args : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $args[-1] eq 'CODE' ? : 2 11 13
67 2 ▶ push @args, sub { $self->_report_for(\%report, $make_message->(@args)); $cb->(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
68 13 ▶ if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 2 11 13
69  
70 13 $report{t0} = [gettimeofday];
71 13 ▶ my @res = $wantarray ? $orig->(@args) : (scalar $orig->(@args));
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 13 13
72  
73 13 ▶ if ($cb) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cb) { } 2 11 13
elsif (builtin::blessed($res[0]) and $res[0]->isa('Mojo::Promise')) { } 0 11 11
74 2 1; # do nothing
75 }
76 ▶ elsif (blessed $res[0] and $res[0]->isa('Mojo::Promise')) {
Condition33%1/3
builtin::blessed($res[0]) and $res[0]->isa("Mojo::Promise")
!ll&&!rl&&r
0110
MC/DC0%0/2
builtin::blessed($res[0]) and $res[0]->isa("Mojo::Promise")
builtin::blessed($res[0]) $res[0]->isa("Mojo::Promise")
Truth table33%1/3
A and B
A= builtin::blessed($res[0])B= $res[0]->isa("Mojo::Promise")
ABresult
0X0
100
111
77 0 $res[0]->finally($self->_report_for(\%report, $make_message->(@args)));
78 }
79 else {
80 11 $self->_report_for(\%report, $make_message->(@args));
81 }
82  
83 13 ▶ return $wantarray ? @res : $res[0];
Branch: 1 branch
BranchTrueFalseTotal
$wantarray ? : 0 13 13
84 2 };
85 }
86  
87 13 ▶sub _add_caller_to_report {
Subroutine _add_caller_to_report: called CC=4 SCAR=13.9
88 13 my ($params, $report) = @_;
89  
90 13 my $i = 0;
91 13 ▶ while (my @caller = caller($i++)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@caller) = caller $i++) 66 0 66
92 66 ▶ next if $caller[0] eq 'Devel::MojoProf' or $caller[0] =~ $params->{ignore_caller};
Branch: 1 branch
BranchTrueFalseTotal
if $caller[0] eq "Devel::MojoProf" or $caller[0] =~ /$params->{"ignore_caller"}/u 53 13 66
Condition100%3/3
$caller[0] eq "Devel::MojoProf" or $caller[0] =~ /$params->{"ignore_caller"}/u
l!l&&r!l&&!r
134013
MC/DC100%2/2
$caller[0] eq "Devel::MojoProf" or $caller[0] =~ /$params->{"ignore_caller"}/u
$caller[0] eq "Devel::MojoProf" $caller[0] =~ /$params->{"ignore_caller"}/u
Truth table100%3/3
A or B
A= $caller[0] eq "Devel::MojoProf"B= $caller[0] =~ /$params->{"ignore_caller"}/u
ABresult
1X1
011
000
93 13 @$report{qw(file line)} = @caller[1, 2];
94 13 last;
95 }
96 }
97  
98 1 ▶sub _add_profiling_for_pg {
Subroutine _add_profiling_for_pg: called CC=2 SCAR=7.2
99 1 my $self = shift;
100 1 ▶ $self->add_profiling_for('Mojo::Pg::Database', query => \&_make_desc_for_db, query_p => \&_make_desc_for_db)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ensure_loaded("Mojo::Pg", 1) 0 1 1
101 if $self->_ensure_loaded('Mojo::Pg', 1);
102 }
103  
104 1 ▶sub _add_profiling_for_mysql {
Subroutine _add_profiling_for_mysql: called CC=2 SCAR=7.2
105 1 my $self = shift;
106 1 ▶ $self->add_profiling_for('Mojo::mysql::Database', query => \&_make_desc_for_db, query_p => \&_make_desc_for_db)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ensure_loaded("Mojo::mysql", 1) 0 1 1
107 if $self->_ensure_loaded('Mojo::mysql', 1);
108 }
109  
110 1 ▶sub _add_profiling_for_redis {
Subroutine _add_profiling_for_redis: called CC=2 SCAR=7.2
111 1 my $self = shift;
112 1 ▶ $self->add_profiling_for('Mojo::Redis::Connection', 'write_p', {ignore_caller => qr{^Mojo::Redis}})
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ensure_loaded("Mojo::Redis", 1) 0 1 1
113 if $self->_ensure_loaded('Mojo::Redis', 1);
114 }
115  
116 1 ▶sub _add_profiling_for_sqlite {
Subroutine _add_profiling_for_sqlite: called CC=2 SCAR=7.2
117 1 my $self = shift;
118 1 ▶ $self->add_profiling_for('Mojo::SQLite::Database', query => \&_make_desc_for_db)
Branch: 1 branch
BranchTrueFalseTotal
if $self->_ensure_loaded("Mojo::SQLite", 1) 0 1 1
119 if $self->_ensure_loaded('Mojo::SQLite', 1);
120 }
121  
122 2 ▶sub _add_profiling_for_ua {
Subroutine _add_profiling_for_ua: called CC=1 SCAR=0.0
123 2 shift->add_profiling_for('Mojo::UserAgent', start => \&_make_desc_for_ua);
124 }
125  
126 7 ▶sub _ensure_loaded {
Subroutine _ensure_loaded: called CC=4 SCAR=14.1
127 7 my ($self, $target, $no_warn) = @_;
128 7 ▶ return $target unless my $e = load_class $target;
Branch: 1 branch
BranchTrueFalseTotal
unless my $e = load_class($target) 2 5 7
129 5 ▶ die "[Devel::MojoProf] Could not load $target: $e" if ref $e;
Branch: 1 branch
BranchTrueFalseTotal
if ref $e 0 5 5
130 5 ▶ warn "[Devel::MojoProf] Could not find module $target\n" unless $no_warn;
Branch: 1 branch
BranchTrueFalseTotal
unless $no_warn 0 5 5
131 5 return;
132 }
133  
134 8 ▶sub _instance { ref $_[0] ? $_[0] : shift->singleton }
Subroutine _instance: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 5 8
135  
136 0 ▶sub _make_desc_for_db { $_[1] }
Subroutine _make_desc_for_db: not called CC=1 SCAR=6.9
137 13 ▶sub _make_desc_for_ua { sprintf '%s %s', $_[1]->req->method, $_[1]->req->url->to_abs }
Subroutine _make_desc_for_ua: called CC=1 SCAR=0.0
138  
139 13 ▶sub _report_for {
Subroutine _report_for: called CC=1 SCAR=0.0
140 13 my ($self, $report, $message) = @_;
141 13 @$report{qw(elapsed message)} = (tv_interval($report->{t0}), $message);
142 13 $self->{reporter}->report($report, $self);
143 }
144  
145 6 1;
146  
147 =encoding utf8
148  
149 =head1 NAME
150  
151 Devel::MojoProf - Profile blocking, non-blocking a promise based Mojolicious APIs
152  
153 =head1 SYNOPSIS
154  
155 $ perl -d:MojoProf myapp.pl
156 $ perl -d:MojoProf -e'Mojo::UserAgent->new->get("https://mojolicious.org")'
157 $ DEVEL_MOJOPROF_OUT_CSV=1 perl -d:MojoProf myapp.pl
158  
159 See L<Devel::MojoProf::Reporter/out_csv> for how C<DEVEL_MOJOPROF_OUT_CSV> works.
160  
161 =head1 DESCRIPTION
162  
163 L<Devel::MojoProf> can add profiling output for blocking, non-blocking and
164 promise based methods. It can be customized to log however you want, but the
165 default is to print a line like the one below to STDERR:
166  
167 0.00038ms [Mojo::Pg::Database::query_p] SELECT 1 as whatever at path/to/app.pl line 23
168  
169 =head1 ATTRIBUTES
170  
171 =head2 reporter
172  
173 my $obj = $prof->reporter;
174 my $prof = $prof->reporter($reporter_class->new);
175  
176 Holds a reporter object that is capable of creating reports by the measurements
177 done by C<$prof>. Holds by default an instance of L<Devel::MojoProf::Reporter>.
178  
179 =head1 METHODS
180  
181 =head2 add_profiling_for
182  
183 my $prof = $prof->add_profiling_for($moniker);
184 my $prof = $prof->add_profiling_for($class => $method1, $method2, ...);
185 my $prof = $prof->add_profiling_for($class => $method1 => $make_message, ...);
186 my $prof = $prof->add_profiling_for($class => $method1 => $make_message, ..., \%params);
187 my $prof = $prof->add_profiling_for($class => $method1 => $make_message, ..., \%params);
188 my $prof = Devel::MojoProf->add_profiling_for(...);
189  
190 Used to add profiling for either a C<$moniker> (short module identifier) or a
191 class and method. This method can also be called as a class method.
192  
193 The supported C<$moniker> are for now "mysql", "pg", "redis", "sqlite" and
194 "ua". See L</import> for more details.
195  
196 It is also possible to manually add support for other custom modules. Here is
197 an example:
198  
199 $prof->add_profiling_for("My::Cool::Class", "get_stuff" => sub {
200 my ($my_cool_obj, @args) = @_;
201 return "This will be the 'message' part in the report";
202 });
203  
204 The CODE ref passed in will get all the arguments that the C<get_suff()> method
205 gets, and the return value should be a string that becomes the C<message> part
206 in the C<$report> hash-ref passed to the L</reporter>.
207  
208 C<%params> is optional and can have the following keys:
209  
210 =over 2
211  
212 =item * ignore_caller
213  
214 Defaults to a regex holding the C<$class>, but can set to any class that you
215 want to skip to generate the C<class> key for the L</reporter> method.
216  
217 =back
218  
219 =head2 import
220  
221 use Devel::MojoProf (); # disable auto-detect
222 use Devel::MojoProf; # All of the modules from the list below
223 use Devel::MojoProf -mysql;
224 use Devel::MojoProf -pg;
225 use Devel::MojoProf -redis;
226 use Devel::MojoProf -sqlite;
227 use Devel::MojoProf -ua;
228 use Devel::MojoProf -pg, -redis, -ua; # Load multiple
229  
230 Used to automatically L</add_profiling_for> know modules. Currently supported
231 modules are L<Mojo::mysql>, L<Mojo::Pg>, L<Mojo::Redis>, L<Mojo::SQLite> and
232 L<Mojo::UserAgent>.
233  
234 Please submit a PR or create an issue if you think more modules should be
235 supported at L<https://github.com/jhthorsen/devel-mojoprof>.
236  
237 =head2 singleton
238  
239 my $prof = Devel::MojoProf->singleton;
240  
241 Used to retrive the singleton object that is used by L</add_profiling_for> when
242 called as a class method.
243  
244 =head1 AUTHOR
245  
246 Jan Henning Thorsen
247  
248 =head1 COPYRIGHT AND LICENSE
249  
250 Copyright (C) 2018, Jan Henning Thorsen.
251  
252 This program is free software, you can redistribute it and/or modify it
253 under the terms of the Artistic License version 2.0.
254  
255 =head1 SEE ALSO
256  
257 This module is inspired by L<Devel::KYTProf>.
258  
259 =cut
« MojoProf/Reporter.pm ↑ Summary