| 1 | package Dancer::Plugin::NYTProf; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use Capture::Tiny ':all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use Dancer::Plugin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use base 'Dancer::Plugin'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use Dancer qw(:syntax); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Dancer::FileUtils; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use File::stat; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 2 | ▶ | use File::Temp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use File::Which; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | our $VERSION = '0.40'; | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Dancer::Plugin::NYTProf - easy Devel::NYTProf profiling for Dancer apps | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | package MyApp; | ||||||||||||||||||||||||||||
| 23 | use Dancer ':syntax'; | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # enables profiling and "/nytprof" | ||||||||||||||||||||||||||||
| 26 | use Dancer::Plugin::NYTProf; | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | Or, if you want to enable it only under development environment (as you should!), | ||||||||||||||||||||||||||||
| 29 | you can do something like: | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | package MyApp; | ||||||||||||||||||||||||||||
| 32 | use Dancer ':syntax'; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | # enables profiling and "/nytprof" | ||||||||||||||||||||||||||||
| 35 | if (setting('environment') eq 'development') { | ||||||||||||||||||||||||||||
| 36 | eval 'use Dancer::Plugin::NYTProf'; | ||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | A plugin to provide easy profiling for Dancer applications, using the venerable | ||||||||||||||||||||||||||||
| 42 | L<Devel::NYTProf>. | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | By simply loading this plugin, you'll have the detailed, helpful profiling | ||||||||||||||||||||||||||||
| 45 | provided by Devel::NYTProf. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | Each individual request to your app is profiled. Going to the URL | ||||||||||||||||||||||||||||
| 48 | C</nytprof> in your app will present a list of profiles; selecting one will | ||||||||||||||||||||||||||||
| 49 | invoke C<nytprofhtml> to generate the HTML reports (unless they already exist), | ||||||||||||||||||||||||||||
| 50 | then serve them up. | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | B<WARNING> This is an early version of this code which is still in development. | ||||||||||||||||||||||||||||
| 53 | In general this isn't a plugin I'd advise to use in a production environment | ||||||||||||||||||||||||||||
| 54 | anyway, but in particular, it uses C<system> to execute C<nytprofhtml>, and I | ||||||||||||||||||||||||||||
| 55 | need to very carefully re-examine the code to make sure that user input cannot | ||||||||||||||||||||||||||||
| 56 | be used to nefarious effect. You are recommended to only use this in your | ||||||||||||||||||||||||||||
| 57 | development environment. | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head1 CONFIGURATION | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | The plugin will work by default without any configuration required - it will | ||||||||||||||||||||||||||||
| 62 | default to writing profiling data into a dir named C<profdir> within your Dancer | ||||||||||||||||||||||||||||
| 63 | application's C<appdir>, present profiling output at C</nytprof> (not yet | ||||||||||||||||||||||||||||
| 64 | configurable), and profile all requests. | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | Below is an example of the options you can configure: | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | plugins: | ||||||||||||||||||||||||||||
| 69 | NYTProf: | ||||||||||||||||||||||||||||
| 70 | enabled: 1 | ||||||||||||||||||||||||||||
| 71 | profdir: '/tmp/profiledata' | ||||||||||||||||||||||||||||
| 72 | nytprofhtml_path: '/usr/local/bin/nytprofhtml' | ||||||||||||||||||||||||||||
| 73 | show_durations: 1 | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | =head2 profdir | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | Where to store profiling data. Defaults to: C<$appdir/nytprof> | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =head2 nytprofhtml_path | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Path to the C<nytprofhtml> script that comes with L<Devel::NYTProf>. Defaults to | ||||||||||||||||||||||||||||
| 82 | the first one we can find in your PATH environment. You should only need to | ||||||||||||||||||||||||||||
| 83 | change this in very specific environments, where C<nytprofhtml> can't be found by | ||||||||||||||||||||||||||||
| 84 | this plugin. | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 enabled | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | Profiling comes with a penalty, and even in development environments you might | ||||||||||||||||||||||||||||
| 89 | want to enable/disable it via configuration file. This lets you do so. You can | ||||||||||||||||||||||||||||
| 90 | toggle this plugin by setting the C<enabled> option to 0 or 1. It is, of course, | ||||||||||||||||||||||||||||
| 91 | enabled by default. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | More configuration (such as the URL at which output is produced, and options to | ||||||||||||||||||||||||||||
| 94 | control which requests get profiled) will be added in a future version. (If | ||||||||||||||||||||||||||||
| 95 | there's something you'd like to see soon, do contact me and let me know - it'll | ||||||||||||||||||||||||||||
| 96 | likely get done a lot quicker then!) | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | =head2 show_durations | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | When listing profile runs, show the duration of each run, extracted from the | ||||||||||||||||||||||||||||
| 101 | profiling data. If you have a lot of profiled runs, this might get slow, so | ||||||||||||||||||||||||||||
| 102 | this option is provided if you don't need the profile durations displayed when | ||||||||||||||||||||||||||||
| 103 | listing profiles, preferring a faster list. Defaults to 1. | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =cut | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 2 | my $setting = plugin_setting; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # exit as quickly as possible if plugin is not enabled | ||||||||||||||||||||||||||||
| 111 | 2 | ▶ | return 1 if exists $setting->{enabled} && $setting->{enabled} != 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $setting->{"enabled"} and $setting->{"enabled"} != 1
MC/DC0%0/2
exists $setting->{"enabled"} and $setting->{"enabled"} != 1
exists $setting->{"enabled"}
$setting->{"enabled"} != 1
Truth table33%1/3
A and B
A= exists $setting->{"enabled"}B= $setting->{"enabled"} != 1
| |||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | # Work out where nytprof_html is, or die with a sensible error | ||||||||||||||||||||||||||||
| 114 | my $nytprofhtml_path = $setting->{nytprofhtml_path} | ||||||||||||||||||||||||||||
| 115 | 2 | ▶ | || File::Which::which('nytprofhtml') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$setting->{'nytprofhtml_path'} || File::Which::which('nytprofhtml')
MC/DC0%0/2
$setting->{'nytprofhtml_path'} || File::Which::which('nytprofhtml')
$setting->{'nytprofhtml_path'}
File::Which::which('nytprofhtml')
Truth table33%1/3
A || B
A= $setting->{'nytprofhtml_path'}B= File::Which::which('nytprofhtml')
| |||||||||||||||||||||||||||||
| 116 | or die "Could not find nytprofhtml script. Ensure it's in your path, " | ||||||||||||||||||||||||||||
| 117 | . "or set the nytprofhtml_path option in your config."; | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | # Need to load Devel::NYTProf at runtime after setting env var, as it will | ||||||||||||||||||||||||||||
| 121 | # insist on creating an nytprof.out file immediately - even if we tell it not to | ||||||||||||||||||||||||||||
| 122 | # start profiling. | ||||||||||||||||||||||||||||
| 123 | # Dirty workaround: get a temp file, then let Devel::NYTProf use that, with | ||||||||||||||||||||||||||||
| 124 | # addpid enabled so that it will append the PID too (so the filename won't | ||||||||||||||||||||||||||||
| 125 | # exist), load Devel::NYTProf, then unlink the file. | ||||||||||||||||||||||||||||
| 126 | # This is dirty, hacky shit that needs to die, but should make things work for | ||||||||||||||||||||||||||||
| 127 | # now. | ||||||||||||||||||||||||||||
| 128 | 2 | my $tempfh = File::Temp->new; | |||||||||||||||||||||||||||
| 129 | 2 | my $file = $tempfh->filename; | |||||||||||||||||||||||||||
| 130 | 2 | $tempfh = undef; # let the file get deleted | |||||||||||||||||||||||||||
| 131 | 2 | $ENV{NYTPROF} = "start=no:file=$file"; | |||||||||||||||||||||||||||
| 132 | 2 | require Devel::NYTProf; | |||||||||||||||||||||||||||
| 133 | 2 | unlink $file; | |||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | hook 'before' => sub { | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | my $path = request->path; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=8 SCAR=42.8
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | # Make sure that the directories we need to put profiling data in exist, | ||||||||||||||||||||||||||||
| 139 | # first: | ||||||||||||||||||||||||||||
| 140 | 0 | ▶ | $setting->{profdir} ||= Dancer::FileUtils::path( | ||||||||||||||||||||||||||
|
Condition0%0/3
$setting->{"profdir"} ||= Dancer::FileUtils::path(setting("appdir"), "nytprof")
MC/DC0%0/2
$setting->{"profdir"} ||= Dancer::FileUtils::path(setting("appdir"), "nytprof")
$setting->{"profdir"}
Dancer::FileUtils::path(setting("appdir"), "nytprof")
Truth table0%0/3
A ||= B
A= $setting->{"profdir"}B= Dancer::FileUtils::path(setting("appdir"), "nytprof")
| |||||||||||||||||||||||||||||
| 141 | setting('appdir'), 'nytprof' | ||||||||||||||||||||||||||||
| 142 | ); | ||||||||||||||||||||||||||||
| 143 | 0 | ▶ | if (! -d $setting->{profdir}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 144 | mkdir $setting->{profdir} | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | or die "$setting->{profdir} does not exist and cannot create - $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | 0 | ▶ | if (!-d Dancer::FileUtils::path($setting->{profdir}, 'html')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | mkdir Dancer::FileUtils::path($setting->{profdir}, 'html') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | or die "Could not create html dir."; | ||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | # Go no further if this request was to view profiling output: | ||||||||||||||||||||||||||||
| 153 | 0 | ▶ | return if $path =~ m{^/nytprof}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | ▶ | return if $path =~ m{^/nytprof}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # Now, fix up the path into something we can use for a filename: | ||||||||||||||||||||||||||||
| 157 | 0 | $path =~ s{^/}{}; | |||||||||||||||||||||||||||
| 158 | 0 | $path =~ s{/}{_s_}g; | |||||||||||||||||||||||||||
| 159 | 0 | $path =~ s{[^a-z0-9]}{_}gi; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Start profiling, and let the request continue | ||||||||||||||||||||||||||||
| 162 | DB::enable_profile( | ||||||||||||||||||||||||||||
| 163 | 0 | Dancer::FileUtils::path($setting->{profdir}, "nytprof.out.$path.$$") | |||||||||||||||||||||||||||
| 164 | ); | ||||||||||||||||||||||||||||
| 165 | 2 | }; | |||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | hook 'after' => sub { | ||||||||||||||||||||||||||||
| 168 | 0 | ▶ | DB::disable_profile(); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 169 | 0 | DB::finish_profile(); | |||||||||||||||||||||||||||
| 170 | 2 | }; | |||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | get '/nytprof' => sub { | ||||||||||||||||||||||||||||
| 173 | 0 | ▶ | require Devel::NYTProf::Data; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||
| 174 | opendir my $dirh, $setting->{profdir} | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | or die "Unable to open profiles dir $setting->{profdir} - $!"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 0 | my @files = grep { /^nytprof\.out/ } readdir $dirh; | |||||||||||||||||||||||||||
| 177 | 0 | closedir $dirh; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | # HTML + CSS here is a bit ugly, but I want this to be usable as a | ||||||||||||||||||||||||||||
| 180 | # single-file plugin that Just Works, without needing to copy over templates | ||||||||||||||||||||||||||||
| 181 | # / CSS etc. | ||||||||||||||||||||||||||||
| 182 | 0 | my $html = <<LISTSTART; | |||||||||||||||||||||||||||
| 183 | <html><head><title>NYTProf profile run list</title> | ||||||||||||||||||||||||||||
| 184 | <style> | ||||||||||||||||||||||||||||
| 185 | * { font-family: Verdana, Arial, Helvetica, sans-serif; } | ||||||||||||||||||||||||||||
| 186 | </style> | ||||||||||||||||||||||||||||
| 187 | </head> | ||||||||||||||||||||||||||||
| 188 | <body> | ||||||||||||||||||||||||||||
| 189 | <h1>Profile run list</h1> | ||||||||||||||||||||||||||||
| 190 | <p>Select a profile run output from the list to view the HTML reports as | ||||||||||||||||||||||||||||
| 191 | produced by <tt>Devel::NYTProf</tt>.</p> | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | <ul> | ||||||||||||||||||||||||||||
| 194 | LISTSTART | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 0 | for my $file ( | |||||||||||||||||||||||||||
| 197 | sort { | ||||||||||||||||||||||||||||
| 198 | (stat Dancer::FileUtils::path($setting->{profdir},$b))->ctime | ||||||||||||||||||||||||||||
| 199 | <=> | ||||||||||||||||||||||||||||
| 200 | 0 | (stat Dancer::FileUtils::path($setting->{profdir},$a))->ctime | |||||||||||||||||||||||||||
| 201 | } @files | ||||||||||||||||||||||||||||
| 202 | ) { | ||||||||||||||||||||||||||||
| 203 | 0 | my $fullfilepath = Dancer::FileUtils::path($setting->{profdir}, $file); | |||||||||||||||||||||||||||
| 204 | 0 | my $label = $file; | |||||||||||||||||||||||||||
| 205 | 0 | $label =~ s{nytprof\.out\.}{}; | |||||||||||||||||||||||||||
| 206 | 0 | $label =~ s{_s_}{/}g; | |||||||||||||||||||||||||||
| 207 | 0 | $label =~ s{\.(\d+)$}{}; | |||||||||||||||||||||||||||
| 208 | 0 | my $pid = $1; # refactor this crap | |||||||||||||||||||||||||||
| 209 | 0 | my $created = scalar localtime( (stat $fullfilepath)->ctime ); | |||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | # read the profile to find out the duration of the profiled request. | ||||||||||||||||||||||||||||
| 212 | # Done in an eval to catch errors (e.g. if a profile run died mid-way, | ||||||||||||||||||||||||||||
| 213 | # the data will be incomplete | ||||||||||||||||||||||||||||
| 214 | 0 | my ($profile,$duration); | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 0 | ▶ | if (!defined $setting->{show_durations} || $setting->{show_durations}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $setting->{"show_durations"} or $setting->{"show_durations"}
MC/DC0%0/2
not defined $setting->{"show_durations"} or $setting->{"show_durations"}
not defined $setting->{"show_durations"}
$setting->{"show_durations"}
Truth table0%0/3
not(A) or B
A= not defined $setting->{"show_durations"}B= $setting->{"show_durations"}
| |||||||||||||||||||||||||||||
| 217 | 0 | eval { | |||||||||||||||||||||||||||
| 218 | my ($stdout, $stderr, @result) = Capture::Tiny::capture { | ||||||||||||||||||||||||||||
| 219 | 0 | ▶ | $profile = Devel::NYTProf::Data->new( | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 220 | { filename => $fullfilepath }, | ||||||||||||||||||||||||||||
| 221 | ); | ||||||||||||||||||||||||||||
| 222 | 0 | }; | |||||||||||||||||||||||||||
| 223 | }; | ||||||||||||||||||||||||||||
| 224 | 0 | ▶ | if ($profile) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 225 | $duration = sprintf '%.4f secs', | ||||||||||||||||||||||||||||
| 226 | 0 | $profile->attributes->{profiler_duration}; | |||||||||||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||||||||||
| 228 | 0 | $duration = '??? seconds - corrupt profile data?'; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 0 | $pid = "PID $pid"; | |||||||||||||||||||||||||||
| 232 | 0 | my $url = request->uri_for("/nytprof/$file")->as_string; | |||||||||||||||||||||||||||
| 233 | $html .= qq{<li><a href="$url"">$label</a> (} | ||||||||||||||||||||||||||||
| 234 | 0 | . join(',', grep { defined $_ } ($pid, $created, $duration)) | |||||||||||||||||||||||||||
| 235 | . qq{)</li>}; | ||||||||||||||||||||||||||||
| 236 | } | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 0 | my $nytversion = $Devel::NYTProf::VERSION; | |||||||||||||||||||||||||||
| 239 | 0 | $html .= <<LISTEND; | |||||||||||||||||||||||||||
| 240 | </ul> | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | <p>Generated by <a href="http://github.com/bigpresh/Dancer-Plugin-NYTProf"> | ||||||||||||||||||||||||||||
| 243 | Dancer::Plugin::NYTProf</a> v$VERSION | ||||||||||||||||||||||||||||
| 244 | (using <a href="http://metacpan.org/dist/Devel::NYTProf"> | ||||||||||||||||||||||||||||
| 245 | Devel::NYTProf</a> v$nytversion)</p> | ||||||||||||||||||||||||||||
| 246 | </body> | ||||||||||||||||||||||||||||
| 247 | </html> | ||||||||||||||||||||||||||||
| 248 | LISTEND | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | 0 | return $html; | |||||||||||||||||||||||||||
| 251 | 2 | }; | |||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||
| 254 | # Serve up HTML reports | ||||||||||||||||||||||||||||
| 255 | get '/nytprof/html/**' => sub { | ||||||||||||||||||||||||||||
| 256 | 0 | ▶ | my ($path) = splat; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 257 | send_file Dancer::FileUtils::path( | ||||||||||||||||||||||||||||
| 258 | 0 | $setting->{profdir}, 'html', map { _safe_filename($_) } @$path | |||||||||||||||||||||||||||
| 259 | ), system_path => 1; | ||||||||||||||||||||||||||||
| 260 | 2 | }; | |||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | get '/nytprof/:filename' => sub { | ||||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | my $profiledata = Dancer::FileUtils::path( | ||||||||||||||||||||||||||||
| 265 | 0 | ▶ | $setting->{profdir}, _safe_filename(param('filename')) | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=7 SCAR=40.3
| |||||||||||||||||||||||||||||
| 266 | ); | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 0 | ▶ | if (!-f $profiledata) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 269 | 0 | send_error 'not_found'; | |||||||||||||||||||||||||||
| 270 | 0 | return "No such profile run found."; | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||
| 273 | # See if we already have the HTML for this run stored; if not, invoke | ||||||||||||||||||||||||||||
| 274 | # nytprofhtml to generate it | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | # Right, do we already have generated HTML for this one? If so, use it | ||||||||||||||||||||||||||||
| 277 | my $htmldir = Dancer::FileUtils::path( | ||||||||||||||||||||||||||||
| 278 | 0 | $setting->{profdir}, 'html', _safe_filename(param('filename')) | |||||||||||||||||||||||||||
| 279 | ); | ||||||||||||||||||||||||||||
| 280 | 0 | ▶ | if (! -f Dancer::FileUtils::path($htmldir, 'index.html')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | # TODO: scrutinise this very carefully to make sure it's not | ||||||||||||||||||||||||||||
| 282 | # exploitable | ||||||||||||||||||||||||||||
| 283 | 0 | system($nytprofhtml_path, "--file=$profiledata", "--out=$htmldir"); | |||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | 0 | ▶ | if ($? == -1) { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 286 | 0 | die "'$nytprofhtml_path' failed to execute: $!"; | |||||||||||||||||||||||||||
| 287 | } elsif ($? & 127) { | ||||||||||||||||||||||||||||
| 288 | 0 | ▶ | die sprintf "'%s' died with signal %d, %s coredump", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 289 | $nytprofhtml_path,, | ||||||||||||||||||||||||||||
| 290 | ($? & 127), | ||||||||||||||||||||||||||||
| 291 | ($? & 128) ? 'with' : 'without'; | ||||||||||||||||||||||||||||
| 292 | } elsif ($? != 0) { | ||||||||||||||||||||||||||||
| 293 | 0 | die sprintf "'%s' exited with value %d", | |||||||||||||||||||||||||||
| 294 | $nytprofhtml_path, $? >> 8; | ||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | } | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | # Redirect off to view it: | ||||||||||||||||||||||||||||
| 299 | 0 | return redirect '/nytprof/html/' | |||||||||||||||||||||||||||
| 300 | . param('filename') . '/index.html'; | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | 2 | }; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # Rudimentary security - remove any directory traversal or poison null | ||||||||||||||||||||||||||||
| 306 | # attempts. We're dealing with user input here, and if they're a sneaky | ||||||||||||||||||||||||||||
| 307 | # bastard, they could convince us to send a file we shouldn't, or have | ||||||||||||||||||||||||||||
| 308 | # nytprofhtml write its output to somewhere it shouldn't. We don't want that. | ||||||||||||||||||||||||||||
| 309 | sub _safe_filename { | ||||||||||||||||||||||||||||
| 310 | 0 | ▶ | my $filename = shift; | ||||||||||||||||||||||||||
|
Subroutine
_safe_filename: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 311 | 0 | $filename =~ s/\\//g; | |||||||||||||||||||||||||||
| 312 | 0 | $filename =~ s/\0//g; | |||||||||||||||||||||||||||
| 313 | 0 | $filename =~ s/\.\.//g; | |||||||||||||||||||||||||||
| 314 | 0 | $filename =~ s/[\/]//g; | |||||||||||||||||||||||||||
| 315 | 0 | return $filename; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | David Precious, C<< <davidp at preshweb.co.uk> >> | ||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | Stefan Hornburg (racke) | ||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||
| 327 | Neil Hooey (nhooey) | ||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||
| 329 | J. Bobby Lopez (jbobbylopez) | ||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | leejo | ||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | Breno G. de Oliveira (garu) | ||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||
| 336 | =head1 BUGS | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | Please report any bugs or feature requests at | ||||||||||||||||||||||||||||
| 339 | L<http://github.com/bigpresh/Dancer-Plugin-NYTProf/issues>. | ||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | =head1 CONTRIBUTING | ||||||||||||||||||||||||||||
| 342 | |||||||||||||||||||||||||||||
| 343 | This module is developed on GitHub: | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | L<http://github.com/bigpresh/Dancer-Plugin-NYTProf> | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | Bug reports, suggestions and pull requests all welcomed! | ||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||
| 349 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||
| 351 | L<Dancer> | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | L<Devel::NYTProf> | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | L<Plack::Middleware::Debug::Profiler::NYTProf> | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =head1 LICENSE AND COPYRIGHT | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | Copyright 2011-2014 David Precious. | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||
| 363 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||
| 364 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | =cut | ||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 2 | 1; # Sam Kington didn't like that this said "End of Dancer::Plugin::NYTProf", | |||||||||||||||||||||||||||
| 372 | # as it's fairly obvious. So, just for Sam's pleasure, | ||||||||||||||||||||||||||||
| 373 | # "It's the end of the world as we know it!" ... or something. | ||||||||||||||||||||||||||||