| 1 | package Devel::NYTProf::Run; | ||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||
| 3 | # vim: ts=8 sw=4 expandtab: | ||||||||||||||||||||||||||||||||
| 4 | ########################################################## | ||||||||||||||||||||||||||||||||
| 5 | # This script is part of the Devel::NYTProf distribution | ||||||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||||||
| 7 | # Copyright, contact and other information can be found | ||||||||||||||||||||||||||||||||
| 8 | # at the bottom of this file, or by going to: | ||||||||||||||||||||||||||||||||
| 9 | # http://metacpan.org/release/Devel-NYTProf/ | ||||||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||||||
| 11 | ########################################################### | ||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||
| 15 | Devel::NYTProf::Run - Invoke NYTProf on a piece of code and return the profile | ||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||
| 19 | This module is experimental and subject to change. | ||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||
| 21 | =cut | ||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||
| 23 | 44 | ▶ | use warnings; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 24 | 44 | ▶ | use strict; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||
| 26 | 44 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||
| 28 | 44 | ▶ | use Carp; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 29 | 44 | ▶ | use Config qw(%Config); | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 30 | 44 | ▶ | use Devel::NYTProf::Data; | ||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||
| 32 | 44 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||||||
| 33 | profile_this | ||||||||||||||||||||||||||||||||
| 34 | perl_command_words | ||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||
| 38 | 44 | my $this_perl = $^X; | |||||||||||||||||||||||||||||||
| 39 | 44 | ▶ | $this_perl .= $Config{_exe} if $^O ne 'VMS' and $this_perl !~ m/$Config{_exe}$/i; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$^O ne "VMS" and not $this_perl =~ /$Devel::NYTProf::Run::Config{"_exe"}$/i
MC/DC0%0/2
$^O ne "VMS" and not $this_perl =~ /$Devel::NYTProf::Run::Config{"_exe"}$/i
$^O ne "VMS"
not $this_perl =~ /$Devel::NYTProf::Run::Config{"_exe"}$/i
Truth table33%1/3
A and not(B)
A= $^O ne "VMS"B= not $this_perl =~ /$Devel::NYTProf::Run::Config{"_exe"}$/i
| |||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||
| 42 | 572 | ▶ | sub perl_command_words { | ||||||||||||||||||||||||||||||
|
Subroutine
perl_command_words: called CC=5 SCAR=20.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 43 | 572 | my %opt = @_; | |||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||
| 45 | 572 | my @perl = ($this_perl); | |||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||
| 47 | # testing just $Config{usesitecustomize} isn't reliable for perl 5.11.x | ||||||||||||||||||||||||||||||||
| 48 | 572 | ▶ | if (($Config{usesitecustomize}||'') eq 'define' | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition40%2/5
($Devel::NYTProf::Run::Config{"usesitecustomize"} || "") eq "define" or $Devel::NYTProf::Run::Config{"ccflags"} =~ /(?<!\w)-DUSE_SITECUSTOMIZE\b/
$Devel::NYTProf::Run::Config{'usesitecustomize'} || ''
MC/DC0%0/2
($Devel::NYTProf::Run::Config{"usesitecustomize"} || "") eq "define" or $Devel::NYTProf::Run::Config{"ccflags"} =~ /(?<!\w)-DUSE_SITECUSTOMIZE\b/
$Devel::NYTProf::Run::Config{'usesitecustomize'}
$Devel::NYTProf::Run::Config{"ccflags"} =~ /(?<!\w)-DUSE_SITECUSTOMIZE\b/
Truth table33%1/3
A or B
A= $Devel::NYTProf::Run::Config{'usesitecustomize'}B= $Devel::NYTProf::Run::Config{"ccflags"} =~ /(?<!\w)-DUSE_SITECUSTOMIZE\b/
| |||||||||||||||||||||||||||||||||
| 49 | or $Config{ccflags} =~ /(?<!\w)-DUSE_SITECUSTOMIZE\b/ | ||||||||||||||||||||||||||||||||
| 50 | ) { | ||||||||||||||||||||||||||||||||
| 51 | 0 | ▶ | push @perl, '-f' if $opt{skip_sitecustomize}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||
| 54 | 572 | return @perl; | |||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||
| 58 | # croaks on failure to execute | ||||||||||||||||||||||||||||||||
| 59 | # carps, not croak, if process has non-zero exit status | ||||||||||||||||||||||||||||||||
| 60 | # Devel::NYTProf::Data->new may croak, e.g., if data truncated | ||||||||||||||||||||||||||||||||
| 61 | 86 | ▶ | sub profile_this { | ||||||||||||||||||||||||||||||
|
Subroutine
profile_this: called CC=11 SCAR=31.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||
| 62 | 86 | my %opt = @_; | |||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||
| 64 | 86 | ▶ | my $out_file = $opt{out_file} || 'nytprof.out'; | ||||||||||||||||||||||||||||||
|
Condition50%1/2
$opt{'out_file'} || 'nytprof.out'
MC/DC0%0/1
$opt{'out_file'} || 'nytprof.out'
$opt{'out_file'}
Truth table50%1/2
A
A= $opt{'out_file'}
| |||||||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||||||
| 66 | 86 | my @perl = (perl_command_words(%opt), '-d:NYTProf'); | |||||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||||||
| 68 | warn sprintf "profile_this() using %s with NYTPROF=%s\n", | ||||||||||||||||||||||||||||||||
| 69 | join(" ", @perl), $ENV{NYTPROF} || '' | ||||||||||||||||||||||||||||||||
| 70 | 86 | ▶ | if $opt{verbose}; | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$ENV{'NYTPROF'} || ''
MC/DC0%0/1
$ENV{'NYTPROF'} || ''
$ENV{'NYTPROF'}
Truth table0%0/2
A
A= $ENV{'NYTPROF'}
| |||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||
| 72 | # ensure child has same libs as us (e.g., if we were run with perl -Mblib) | ||||||||||||||||||||||||||||||||
| 73 | 86 | local $ENV{PERL5LIB} = join($Config{path_sep}, @INC); | |||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||
| 75 | 86 | ▶ | if (my $src_file = $opt{src_file}) { | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 76 | 0 | ▶ | system(@perl, $src_file) == 0 | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 77 | or carp "Exit status $? from @perl $src_file"; | ||||||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||||||
| 79 | elsif (my $src_code = $opt{src_code}) { | ||||||||||||||||||||||||||||||||
| 80 | 86 | my $cmd = join ' ', map qq{"$_"}, @perl; | |||||||||||||||||||||||||||||||
| 81 | 86 | ▶ | open my $fh, "| $cmd" | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 82 | or croak "Can't open pipe to $cmd"; | ||||||||||||||||||||||||||||||||
| 83 | 86 | print $fh $src_code; | |||||||||||||||||||||||||||||||
| 84 | 86 | ▶ | close $fh | ||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||
| 85 | or carp $! ? "Error closing $cmd pipe: $!" | ||||||||||||||||||||||||||||||||
| 86 | : "Exit status $? from $cmd"; | ||||||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||
| 89 | else { | ||||||||||||||||||||||||||||||||
| 90 | 0 | croak "Neither src_file or src_code was provided"; | |||||||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||
| 93 | # undocumented hack that's handy for testing | ||||||||||||||||||||||||||||||||
| 94 | 86 | ▶ | if ($opt{htmlopen}) { | ||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||
| 95 | 0 | my @nytprofhtml_open = ("perl", "nytprofhtml", "--open", "-file=$out_file"); | |||||||||||||||||||||||||||||||
| 96 | 0 | warn "Running @nytprofhtml_open\n"; | |||||||||||||||||||||||||||||||
| 97 | 0 | system @nytprofhtml_open; | |||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||
| 100 | 86 | my $profile = Devel::NYTProf::Data->new( { filename => $out_file } ); | |||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||
| 102 | 86 | unlink $out_file; | |||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||
| 104 | 86 | return $profile; | |||||||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||
| 107 | 44 | 1; | |||||||||||||||||||||||||||||||