| 1 | package Mojolicious::Plugin::NYTProf; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | Mojolicious::Plugin::NYTProf - Auto handling of Devel::NYTProf in your Mojolicious app | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =for html | ||||||||||||||||||||||||||||
| 8 | <a href='https://travis-ci.org/Humanstate/mojolicious-plugin-nytprof?branch=master'><img src='https://travis-ci.org/Humanstate/mojolicious-plugin-nytprof.svg?branch=master' alt='Build Status' /></a> | ||||||||||||||||||||||||||||
| 9 | <a href='https://coveralls.io/r/Humanstate/mojolicious-plugin-nytprof?branch=master'><img src='https://coveralls.io/repos/Humanstate/mojolicious-plugin-nytprof/badge.png?branch=master' alt='Coverage Status' /></a> | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | =head1 VERSION | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0.23 | ||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | This plugin enables L<Mojolicious> to automatically generate Devel::NYTProf | ||||||||||||||||||||||||||||
| 18 | profiles and routes for your app, it has been inspired by | ||||||||||||||||||||||||||||
| 19 | L<Dancer::Plugin::NYTProf> | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | use Mojolicious::Lite; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | plugin NYTProf => { | ||||||||||||||||||||||||||||
| 26 | nytprof => { | ||||||||||||||||||||||||||||
| 27 | ... # see CONFIGURATION | ||||||||||||||||||||||||||||
| 28 | }, | ||||||||||||||||||||||||||||
| 29 | }; | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | app->start; | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Or | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | use Mojo::Base 'Mojolicious'; | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | ... | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | sub startup { | ||||||||||||||||||||||||||||
| 40 | my $self = shift; | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | ... | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | my $mojo_config = $self->plugin('Config'); | ||||||||||||||||||||||||||||
| 45 | $self->plugin(NYTProf => $mojo_config); | ||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | Then run your app - you should start your app with the env variables: | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | PERL5OPT='-d:NYTProf' | ||||||||||||||||||||||||||||
| 51 | NYTPROF=start=no | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | without this, things go a bit haywire (most obviously manifested as broken links | ||||||||||||||||||||||||||||
| 54 | in the report) because otherwise any code compiled before the C<plugin> call | ||||||||||||||||||||||||||||
| 55 | cannot be covered, as described in the docs: | ||||||||||||||||||||||||||||
| 56 | L<https://metacpan.org/pod/Devel::NYTProf#RUN-TIME-CONTROL-OF-PROFILING> | ||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | Profiles generated can be seen by visting /nytprof and reports | ||||||||||||||||||||||||||||
| 59 | will be generated on the fly when you click on a specific profile. | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 7 | ▶ | use Mojo::Base 'Mojolicious::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 67 | 7 | ▶ | use Time::HiRes 'gettimeofday'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 68 | 7 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 69 | 7 | ▶ | use File::Which; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 70 | 7 | ▶ | use File::Spec::Functions qw/catfile catdir/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 7 | our $VERSION = '0.23'; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | =head1 METHODS | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | =head2 register | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | Registers the plugin with your app - this will only do something if the nytprof | ||||||||||||||||||||||||||||
| 79 | key exists in your config hash | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | $self->register($app, \%config); | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 HOOKS AND Devel::NYTProf | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | The plugin adds hooks to control the level of profiling, Devel::NYTProf profiling | ||||||||||||||||||||||||||||
| 86 | is started using a before_routes hook and the stopped with an around_dispatch hook. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | The consequence of this is that you should see profiling only for your routes and | ||||||||||||||||||||||||||||
| 89 | rendering code and will not see most of the actual Mojolicious framework detail. | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | The caveat with the use of hooks is that some hooks can fire out of order, and when | ||||||||||||||||||||||||||||
| 92 | asynchronous code is used in your controllers you may see incomplete/odd profiling | ||||||||||||||||||||||||||||
| 93 | behaviour - you can play around with the hook configuration to try to fix this. | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | You can override the hooks used to control when the profiling runs, see the | ||||||||||||||||||||||||||||
| 96 | CONFIGURATION section below. | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | Here's what you can control in myapp.conf: | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | { | ||||||||||||||||||||||||||||
| 103 | # Devel::NYTProf will only be loaded, and profiling enabled, if the nytprof | ||||||||||||||||||||||||||||
| 104 | # key is present in your config file, so either remove it or comment it out | ||||||||||||||||||||||||||||
| 105 | # to completely disable profiling. | ||||||||||||||||||||||||||||
| 106 | nytprof => { | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # path to your nytprofhtml script (installed as part of Devel::NYTProf | ||||||||||||||||||||||||||||
| 109 | # distribution). the plugin will do its best to try to find this so this | ||||||||||||||||||||||||||||
| 110 | # is optional, just set if you have a none standard path | ||||||||||||||||||||||||||||
| 111 | nytprofhtml_path => '/path/to/nytprofhtml', | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # path to store Devel::NYTProf output profiles and generated html pages. | ||||||||||||||||||||||||||||
| 114 | # options, defaults to "/path/to/your/app/root/dir/nytprof" | ||||||||||||||||||||||||||||
| 115 | profiles_dir => '/path/to/nytprof/profiles/' | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | # set this to true to allow the plugin to run when in production mode | ||||||||||||||||||||||||||||
| 118 | # the default value is 0 so you can deploy your app to prod without | ||||||||||||||||||||||||||||
| 119 | # having to make any changes to config/plugin register | ||||||||||||||||||||||||||||
| 120 | allow_production => 0, | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | # Devel::NYTProf environment options, see the documentation at | ||||||||||||||||||||||||||||
| 123 | # https://metacpan.org/pod/Devel::NYTProf#NYTPROF-ENVIRONMENT-VARIABLE | ||||||||||||||||||||||||||||
| 124 | # for a complete list. N.B. you can't supply start or file as these | ||||||||||||||||||||||||||||
| 125 | # are used internally in the plugin so will be ignored if passed | ||||||||||||||||||||||||||||
| 126 | env => { | ||||||||||||||||||||||||||||
| 127 | trace => 1, | ||||||||||||||||||||||||||||
| 128 | log => "/path/to/foo/", | ||||||||||||||||||||||||||||
| 129 | .... | ||||||||||||||||||||||||||||
| 130 | }, | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | # when to enable Devel::NYTProf profiling - the pre_hook will run | ||||||||||||||||||||||||||||
| 133 | # to enable_profile and the post_hook will run to disable_profile | ||||||||||||||||||||||||||||
| 134 | # and finish_profile. the values show here are the defaults so you | ||||||||||||||||||||||||||||
| 135 | # do not need to provide these options | ||||||||||||||||||||||||||||
| 136 | # | ||||||||||||||||||||||||||||
| 137 | # bear in mind the caveats in the Mojolicious docs regarding hooks | ||||||||||||||||||||||||||||
| 138 | # and that they may not fire in the order you expect - this can | ||||||||||||||||||||||||||||
| 139 | # affect the NYTProf output and cause some things not to appear | ||||||||||||||||||||||||||||
| 140 | # (or appear in the wrong order). the defaults below should be | ||||||||||||||||||||||||||||
| 141 | # sufficient for profiling your code, however you can change these | ||||||||||||||||||||||||||||
| 142 | # | ||||||||||||||||||||||||||||
| 143 | # N.B. there is nothing stopping you reversing the order of the | ||||||||||||||||||||||||||||
| 144 | # hooks, which would cause the Mojolicious framework code to be | ||||||||||||||||||||||||||||
| 145 | # profiled, or providing hooks that are the same or even invalid. these | ||||||||||||||||||||||||||||
| 146 | # config options should probably be used with some care | ||||||||||||||||||||||||||||
| 147 | pre_hook => 'before_routes', | ||||||||||||||||||||||||||||
| 148 | post_hook => 'around_dispatch', | ||||||||||||||||||||||||||||
| 149 | }, | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =head1 nytprofhtml LOCATION | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | The plugin does its best to find the path to your nytprofhtml executable, if | ||||||||||||||||||||||||||||
| 155 | it cannot find it then it will die with an error. This also affects testing, | ||||||||||||||||||||||||||||
| 156 | and any tests will be skipped if they cannot find nytprofhtml allowing you to | ||||||||||||||||||||||||||||
| 157 | install the plugin - you will then need to make sure to set the path in your | ||||||||||||||||||||||||||||
| 158 | config using nytprofhtml_path | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =cut | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 88 | ▶ | sub register { | ||||||||||||||||||||||||||
|
Subroutine
register: called CC=13 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||
| 163 | 88 | my ($self, $app, $config) = @_; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 88 | ▶ | if (my $nytprof = $config->{nytprof}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 87 | ▶ | return if $app->mode eq 'production' and ! $nytprof->{allow_production}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$app->mode eq "production" and not $nytprof->{"allow_production"}
MC/DC100%2/2
$app->mode eq "production" and not $nytprof->{"allow_production"}
$app->mode eq "production"
not $nytprof->{"allow_production"}
Truth table100%3/3
A and not(B)
A= $app->mode eq "production"B= not $nytprof->{"allow_production"}
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 86 | my $nytprofhtml_path; | |||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 86 | ▶ | if ( $nytprofhtml_path = $nytprof->{nytprofhtml_path} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 172 | # no sanity checking here, if a path is configured we use it | ||||||||||||||||||||||||||||
| 173 | # and don't fall through to defaults | ||||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||
| 175 | 85 | $nytprofhtml_path = _find_nytprofhtml(); | |||||||||||||||||||||||||||
| 176 | } | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 86 | ▶ | $nytprofhtml_path && -e $nytprofhtml_path | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$nytprofhtml_path and -e $nytprofhtml_path
MC/DC50%1/2
$nytprofhtml_path and -e $nytprofhtml_path
$nytprofhtml_path
-e $nytprofhtml_path
Truth table66%2/3
A and B
A= $nytprofhtml_pathB= -e $nytprofhtml_path
| |||||||||||||||||||||||||||||
| 179 | or die "Could not find nytprofhtml script. Ensure it's in your path, " | ||||||||||||||||||||||||||||
| 180 | . "or set the nytprofhtml_path option in your config."; | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | # Devel::NYTProf will create an nytprof.out file immediately so | ||||||||||||||||||||||||||||
| 183 | # we need to assign a tmp file and disable profiling from start | ||||||||||||||||||||||||||||
| 184 | 85 | ▶ | my $prof_dir = $nytprof->{profiles_dir} || 'nytprof'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$nytprof->{'profiles_dir'} || 'nytprof'
MC/DC0%0/1
$nytprof->{'profiles_dir'} || 'nytprof'
$nytprof->{'profiles_dir'}
Truth table50%1/2
A
A= $nytprof->{'profiles_dir'}
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 85 | foreach my $dir ($prof_dir,catfile($prof_dir,'profiles')) { | |||||||||||||||||||||||||||
| 187 | 170 | ▶ | if (! -d $dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 0 | ▶ | mkdir $dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | or die "$dir does not exist and cannot create - $!"; | ||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | # disable config option is undocumented, it allows testing where we | ||||||||||||||||||||||||||||
| 194 | # don't actually load or run Devel::NYTProf | ||||||||||||||||||||||||||||
| 195 | 85 | ▶ | if (!$nytprof->{disable}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 196 | # https://metacpan.org/pod/Devel::NYTProf#NYTPROF-ENVIRONMENT-VARIABLE | ||||||||||||||||||||||||||||
| 197 | # options for Devel::NYTProf - any can be passed but will always set | ||||||||||||||||||||||||||||
| 198 | # the start and file options here | ||||||||||||||||||||||||||||
| 199 | 4 | $nytprof->{env}{start} = 'no'; | |||||||||||||||||||||||||||
| 200 | 4 | s/([:=])/\\$1/g for grep{ defined() } values %{ $nytprof->{env} }; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | $ENV{NYTPROF} = join( ':', | ||||||||||||||||||||||||||||
| 203 | 6 | map { "$_=" . $nytprof->{env}{$_} } | |||||||||||||||||||||||||||
| 204 | 4 | keys %{ $nytprof->{env} } | |||||||||||||||||||||||||||
| 205 | ); | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | 4 | require Devel::NYTProf; | |||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | 85 | $self->_add_hooks($app, $config, $nytprofhtml_path); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||
| 214 | 92 | ▶ | sub _find_nytprofhtml { | ||||||||||||||||||||||||||
|
Subroutine
_find_nytprofhtml: called CC=4 SCAR=16.4
| |||||||||||||||||||||||||||||
| 215 | # fall back, assume nytprofhtml_path in same dir as perl | ||||||||||||||||||||||||||||
| 216 | 92 | my $nytprofhtml_path = $^X; | |||||||||||||||||||||||||||
| 217 | 92 | $nytprofhtml_path =~ s/w?perl[\d\.]*(?:\.exe)?$/nytprofhtml/; | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | 92 | ▶ | if ( ! -e $nytprofhtml_path ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 220 | # last ditch attempt to find nytprofhtml, use File::Which | ||||||||||||||||||||||||||||
| 221 | # (last ditch in that it may return a different nytprofhtml | ||||||||||||||||||||||||||||
| 222 | # that is using a differently configured perl, e.g. system, | ||||||||||||||||||||||||||||
| 223 | # this may die with incompat config errorrs but at least try) | ||||||||||||||||||||||||||||
| 224 | 0 | $nytprofhtml_path = File::Which::which('nytprofhtml'); | |||||||||||||||||||||||||||
| 225 | } | ||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||
| 227 | 92 | ▶ | return $nytprofhtml_path && -e $nytprofhtml_path | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$nytprofhtml_path and -e $nytprofhtml_path
MC/DC0%0/2
$nytprofhtml_path and -e $nytprofhtml_path
$nytprofhtml_path
-e $nytprofhtml_path
Truth table33%1/3
A and B
A= $nytprofhtml_pathB= -e $nytprofhtml_path
| |||||||||||||||||||||||||||||
| 228 | ? $nytprofhtml_path : undef; | ||||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | 85 | ▶ | sub _add_hooks { | ||||||||||||||||||||||||||
|
Subroutine
_add_hooks: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 232 | 85 | my ($self, $app, $config, $nytprofhtml_path) = @_; | |||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | 85 | my $nytprof = $config->{nytprof}; | |||||||||||||||||||||||||||
| 235 | 85 | ▶ | my $prof_dir = $nytprof->{profiles_dir} || 'nytprof'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$nytprof->{'profiles_dir'} || 'nytprof'
MC/DC0%0/1
$nytprof->{'profiles_dir'} || 'nytprof'
$nytprof->{'profiles_dir'}
Truth table50%1/2
A
A= $nytprof->{'profiles_dir'}
| |||||||||||||||||||||||||||||
| 236 | 85 | ▶ | my $pre_hook = $nytprof->{pre_hook} || 'before_routes'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$nytprof->{'pre_hook'} || 'before_routes'
MC/DC100%1/1
$nytprof->{'pre_hook'} || 'before_routes'
$nytprof->{'pre_hook'}
Truth table100%2/2
A
A= $nytprof->{'pre_hook'}
| |||||||||||||||||||||||||||||
| 237 | 85 | ▶ | my $post_hook = $nytprof->{post_hook} || 'around_dispatch'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$nytprof->{'post_hook'} || 'around_dispatch'
MC/DC100%1/1
$nytprof->{'post_hook'} || 'around_dispatch'
$nytprof->{'post_hook'}
Truth table100%2/2
A
A= $nytprof->{'post_hook'}
| |||||||||||||||||||||||||||||
| 238 | 85 | ▶ | my $disable = $nytprof->{disable} || 0; | ||||||||||||||||||||||||||
|
Condition100%2/2
$nytprof->{'disable'} || 0
MC/DC100%1/1
$nytprof->{'disable'} || 0
$nytprof->{'disable'}
Truth table100%2/2
A
A= $nytprof->{'disable'}
| |||||||||||||||||||||||||||||
| 239 | 85 | my $log = $app->log; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # add the nytprof/html directory to the static paths | ||||||||||||||||||||||||||||
| 242 | # so we can serve these without having to add routes | ||||||||||||||||||||||||||||
| 243 | 85 | push @{$app->static->paths},catfile($prof_dir,'html'); | |||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | # put the actual profile files into a profiles sub directory | ||||||||||||||||||||||||||||
| 246 | # to avoid confusion with the *dirs* in nytprof/html | ||||||||||||||||||||||||||||
| 247 | 85 | my $prof_sub_dir = catfile( $prof_dir,'profiles' ); | |||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 2388 | ▶ | $app->hook($pre_hook => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=10 SCAR=24.4
| |||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||
| 251 | # figure args based on what the hook is | ||||||||||||||||||||||||||||
| 252 | 2388 | my ($tx, $app, $next, $c, $path); | |||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | 2388 | ▶ | if ($pre_hook eq 'after_build_tx') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 255 | 693 | ($tx, $app) = @_[0,1]; | |||||||||||||||||||||||||||
| 256 | 693 | $path = $pre_hook; # TODO - need better identifier for this? | |||||||||||||||||||||||||||
| 257 | } elsif ($pre_hook =~ /around/) { | ||||||||||||||||||||||||||||
| 258 | 0 | ($next, $c) = @_[0,1]; | |||||||||||||||||||||||||||
| 259 | } else { | ||||||||||||||||||||||||||||
| 260 | 1695 | $c = $_[0]; | |||||||||||||||||||||||||||
| 261 | 1695 | $path = $c->req->url->to_string; | |||||||||||||||||||||||||||
| 262 | 1695 | ▶ | return if $c->stash->{'mojo.static'}; # static files | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 2385 | ▶ | return if $path =~ m{^/nytprof}; # viewing profiles | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 266 | 2381 | $path =~ s!^/!!g; | |||||||||||||||||||||||||||
| 267 | 2381 | $path =~ s!/!-!g; | |||||||||||||||||||||||||||
| 268 | 2381 | ▶ | $path =~ s![:?]!-!g if $^O eq 'MSWin32'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 2381 | $path =~ s!\?.*$!!g; # remove URL query params | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 2381 | my ($sec, $usec) = gettimeofday; | |||||||||||||||||||||||||||
| 272 | 2381 | my $profile = catfile($prof_sub_dir,"nytprof_out_${sec}_${usec}_${path}_$$"); | |||||||||||||||||||||||||||
| 273 | 2381 | ▶ | if($^O eq 'MSWin32' && length($profile)>259){ | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O eq "MSWin32" and length $profile > 259
MC/DC0%0/2
$^O eq "MSWin32" and length $profile > 259
$^O eq "MSWin32"
length $profile > 259
Truth table33%1/3
A and B
A= $^O eq "MSWin32"B= length $profile > 259
| |||||||||||||||||||||||||||||
| 274 | 0 | my $overflow = length($profile) - 259; | |||||||||||||||||||||||||||
| 275 | 0 | $path = substr($path, 0,length($path) - $overflow -1); | |||||||||||||||||||||||||||
| 276 | 0 | $profile = catfile($prof_sub_dir,"nytprof_out_${sec}_${usec}_${path}_$$"); | |||||||||||||||||||||||||||
| 277 | } | ||||||||||||||||||||||||||||
| 278 | 2381 | $log->debug( 'starting NYTProf' ); | |||||||||||||||||||||||||||
| 279 | # note that we are passing a custom file to enable_profile, this results in | ||||||||||||||||||||||||||||
| 280 | # a timing bug causing multiple calls to this plugin (in the order of 10^5) | ||||||||||||||||||||||||||||
| 281 | # to gradually slow down. see GH #5 | ||||||||||||||||||||||||||||
| 282 | 2381 | ▶ | DB::enable_profile( $profile ) if ! $disable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 2381 | ▶ | return $next->() if $pre_hook =~ /around/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | 85 | }); | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 2243 | ▶ | $app->hook($post_hook => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 287 | # first arg is $next if the hook matches around | ||||||||||||||||||||||||||||
| 288 | 2243 | ▶ | shift->() if $post_hook =~ /around/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | 2243 | ▶ | DB::finish_profile() if ! $disable; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 290 | 2243 | $log->debug( 'finished NYTProf' ); | |||||||||||||||||||||||||||
| 291 | 85 | }); | |||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | $app->routes->get('/nytprof/profiles/:file' | ||||||||||||||||||||||||||||
| 294 | => [file => qr/nytprof_out_\d+_\d+.*/] | ||||||||||||||||||||||||||||
| 295 | 2 | ▶ | => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 296 | 2 | $log->debug( "generating profile for $nytprofhtml_path" ); | |||||||||||||||||||||||||||
| 297 | 2 | _generate_profile(@_,$prof_dir,$nytprofhtml_path) | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | 85 | ); | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 85 | ▶ | $app->routes->get('/nytprof' => sub { _list_profiles(@_,$prof_sub_dir) }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 2 | ▶ | sub _list_profiles { | ||||||||||||||||||||||||||
|
Subroutine
_list_profiles: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 305 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 306 | 2 | my $prof_dir = shift; | |||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 2 | my @profiles = _profiles($prof_dir); | |||||||||||||||||||||||||||
| 309 | 2 | $self->app->log->debug( scalar( @profiles ) . ' profiles found' ); | |||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # could use epl here, but users might be using a different Template engine | ||||||||||||||||||||||||||||
| 312 | 2 | ▶ | my $list = @profiles | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | ? '<p>Select a profile run output from the list to view the HTML reports as produced by <tt>Devel::NYTProf</tt>.</p><ul>' | ||||||||||||||||||||||||||||
| 314 | : '<p>No profiles found</p>'; | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | 2 | foreach (@profiles) { | |||||||||||||||||||||||||||
| 317 | 3 | $list .= qq{ | |||||||||||||||||||||||||||
| 318 | <li> | ||||||||||||||||||||||||||||
| 319 | <a href="$_->{url}">$_->{label}</a> | ||||||||||||||||||||||||||||
| 320 | (PID $_->{pid}, $_->{created}, $_->{duration}) | ||||||||||||||||||||||||||||
| 321 | </li> | ||||||||||||||||||||||||||||
| 322 | }; | ||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | 2 | ▶ | $list .= '</ul>' if $list !~ /No profiles found/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | 2 | my $html = <<"EndOfEp"; | |||||||||||||||||||||||||||
| 328 | <html> | ||||||||||||||||||||||||||||
| 329 | <head> | ||||||||||||||||||||||||||||
| 330 | <title>NYTProf profile run list</title> | ||||||||||||||||||||||||||||
| 331 | </head> | ||||||||||||||||||||||||||||
| 332 | <body> | ||||||||||||||||||||||||||||
| 333 | <h1>Profile run list</h1> | ||||||||||||||||||||||||||||
| 334 | $list | ||||||||||||||||||||||||||||
| 335 | </body> | ||||||||||||||||||||||||||||
| 336 | </html> | ||||||||||||||||||||||||||||
| 337 | EndOfEp | ||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||
| 339 | 2 | $self->render(text => $html); | |||||||||||||||||||||||||||
| 340 | } | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 7 | ▶ | sub _profiles { | ||||||||||||||||||||||||||
|
Subroutine
_profiles: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 343 | 7 | my $prof_dir = shift; | |||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | 7 | require Devel::NYTProf::Data; | |||||||||||||||||||||||||||
| 346 | 7 | ▶ | opendir my $dirh, $prof_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 347 | or die "Unable to open profiles dir $prof_dir - $!"; | ||||||||||||||||||||||||||||
| 348 | 7 | my @files = grep { /^nytprof_out/ } readdir $dirh; | |||||||||||||||||||||||||||
| 349 | 7 | closedir $dirh; | |||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | 7 | my @profiles; | |||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | 7 | for my $file ( sort { | |||||||||||||||||||||||||||
| 354 | 6 | (stat catfile($prof_dir,$b))[10] <=> (stat catfile($prof_dir,$a))[10] | |||||||||||||||||||||||||||
| 355 | } @files ) { | ||||||||||||||||||||||||||||
| 356 | 8 | my $profile; | |||||||||||||||||||||||||||
| 357 | 8 | my $filepath = catfile($prof_dir,$file); | |||||||||||||||||||||||||||
| 358 | 8 | my $label = $file; | |||||||||||||||||||||||||||
| 359 | 8 | $label =~ s{nytprof_out_(\d+)_(\d+)_}{}; | |||||||||||||||||||||||||||
| 360 | 8 | my ($sec, $usec) = ($1,$2); | |||||||||||||||||||||||||||
| 361 | 8 | $label =~ s{\.}{/}g; | |||||||||||||||||||||||||||
| 362 | 8 | $label =~ s{/(\d+)$}{}; | |||||||||||||||||||||||||||
| 363 | 8 | my $pid = $1; | |||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||
| 365 | 8 | my ($nytprof,$duration); | |||||||||||||||||||||||||||
| 366 | 8 | eval { $nytprof = Devel::NYTProf::Data->new({filename => $filepath}); }; | |||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | $profile->{duration} = $nytprof && $nytprof->attributes->{profiler_duration} | ||||||||||||||||||||||||||||
| 369 | ? sprintf('%.4f secs', $nytprof->attributes->{profiler_duration}) | ||||||||||||||||||||||||||||
| 370 | 8 | ▶ | : '??? seconds - corrupt profile data?'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$nytprof and $nytprof->attributes->{"profiler_duration"}
MC/DC50%1/2
$nytprof and $nytprof->attributes->{"profiler_duration"}
$nytprof
$nytprof->attributes->{"profiler_duration"}
Truth table66%2/3
A and B
A= $nytprofB= $nytprof->attributes->{"profiler_duration"}
| |||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||
| 372 | 8 | @{$profile}{qw/file url pid created label/} | |||||||||||||||||||||||||||
| 373 | = ($file,"/nytprof/profiles/$file",$pid,scalar localtime($sec),$label); | ||||||||||||||||||||||||||||
| 374 | 8 | push(@profiles,$profile); | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 7 | return @profiles; | |||||||||||||||||||||||||||
| 378 | } | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 2 | ▶ | sub _generate_profile { | ||||||||||||||||||||||||||
|
Subroutine
_generate_profile: called CC=10 SCAR=25.5
| |||||||||||||||||||||||||||||
| 381 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 382 | 2 | my $htmldir = my $prof_dir = shift; | |||||||||||||||||||||||||||
| 383 | 2 | my $nytprofhtml_path = shift; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | 2 | my $file = $self->stash('file'); | |||||||||||||||||||||||||||
| 386 | 2 | my $profile = catfile($prof_dir,'profiles',$file); | |||||||||||||||||||||||||||
| 387 | 2 | ▶ | return $self->reply->not_found if !-f $profile; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | 1 | foreach my $sub_dir ( | |||||||||||||||||||||||||||
| 390 | $htmldir, | ||||||||||||||||||||||||||||
| 391 | catfile($htmldir,'html'), | ||||||||||||||||||||||||||||
| 392 | catfile($htmldir,'html',$file), | ||||||||||||||||||||||||||||
| 393 | ) { | ||||||||||||||||||||||||||||
| 394 | 3 | ▶ | if (! -d $sub_dir) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | 1 | ▶ | mkdir $sub_dir | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 396 | or die "$sub_dir does not exist and cannot create - $!"; | ||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 1 | $htmldir = catfile($htmldir,'html',$file); | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | 1 | ▶ | if (! -f catfile($htmldir, 'index.html')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 403 | 1 | system($nytprofhtml_path, "--file=$profile", "--out=$htmldir"); | |||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 1 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 406 | 0 | die "'$nytprofhtml_path' failed to execute: $!"; | |||||||||||||||||||||||||||
| 407 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 408 | 0 | ▶ | die sprintf "'%s' died with signal %d, %s coredump", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 409 | $nytprofhtml_path,,($? & 127),($? & 128) ? 'with' : 'without'; | ||||||||||||||||||||||||||||
| 410 | } elsif ($? != 0) { | ||||||||||||||||||||||||||||
| 411 | 0 | die sprintf "'%s' exited with value %d", | |||||||||||||||||||||||||||
| 412 | $nytprofhtml_path, $? >> 8; | ||||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 1 | $self->redirect_to("/${file}/index.html"); | |||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | Lee Johnson - C<leejo@cpan.org> | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | This library is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||||||||||
| 426 | the same terms as Perl itself. If you would like to contribute documentation | ||||||||||||||||||||||||||||
| 427 | please raise an issue / pull request: | ||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||
| 429 | https://github.com/Humanstate/mojolicious-plugin-nytprof | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | =cut | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 7 | 1; | |||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | # vim: ts=2:sw=2:et | ||||||||||||||||||||||||||||