| 1 |
#!/usr/bin/perl |
| 2 |
########################################################## |
| 3 |
# This script is part of the Devel::NYTProf distribution |
| 4 |
# |
| 5 |
# Copyright, contact and other information can be found |
| 6 |
# at the bottom of this file, or by going to: |
| 7 |
# http://metacpan.org/release/Devel-NYTProf/ |
| 8 |
# |
| 9 |
########################################################## |
| 10 |
|
| 11 |
use warnings; |
| 12 |
use strict; |
| 13 |
|
| 14 |
use Carp; |
| 15 |
use Getopt::Long; |
| 16 |
|
| 17 |
use Devel::NYTProf::Reader; |
| 18 |
|
| 19 |
our $VERSION = '6.15'; |
| 20 |
|
| 21 |
use constant NUMERIC_PRECISION => 5; |
| 22 |
|
| 23 |
my %delimiters = ( |
| 24 |
comma => ",", |
| 25 |
tab => "\t", |
| 26 |
); |
| 27 |
|
| 28 |
my %opt = ( |
| 29 |
file => 'nytprof.out', |
| 30 |
out => 'nytprof', |
| 31 |
delim => 'comma', |
| 32 |
); |
| 33 |
GetOptions(\%opt, qw/file|f=s delete|d out|o=s help|h delim=s annotated|a/) |
| 34 |
or do { |
| 35 |
usage(); |
| 36 |
exit 1; |
| 37 |
}; |
| 38 |
|
| 39 |
if (defined($opt{help})) { |
| 40 |
usage(); |
| 41 |
exit; |
| 42 |
} |
| 43 |
|
| 44 |
$opt{delim} = $delimiters{ $opt{delim} } if exists $delimiters{ $opt{delim} }; |
| 45 |
|
| 46 |
# handle file selection option |
| 47 |
if (!-r $opt{file}) { |
| 48 |
die "$0: Unable to access $opt{file}\n"; |
| 49 |
} |
| 50 |
|
| 51 |
# handle output location |
| 52 |
if (!-e $opt{out}) { |
| 53 |
|
| 54 |
# will be created |
| 55 |
} |
| 56 |
elsif (!-d $opt{out}) { |
| 57 |
die "$0: Specified output directory `$opt{out}' is a file. whoops!\n"; |
| 58 |
} |
| 59 |
elsif (!-w $opt{out}) { |
| 60 |
die "$0: Unable to write to output directory `$opt{out}'\n"; |
| 61 |
} |
| 62 |
|
| 63 |
# handle deleting old db's |
| 64 |
if (defined($opt{'delete'})) { |
| 65 |
_delete(); |
| 66 |
} |
| 67 |
|
| 68 |
print "Generating CSV report...\n"; |
| 69 |
my $reporter = new Devel::NYTProf::Reader($opt{file}); |
| 70 |
|
| 71 |
# place to store this |
| 72 |
$reporter->output_dir($opt{out}); |
| 73 |
|
| 74 |
$reporter->set_param(mk_report_source_line => sub { |
| 75 |
my ($linenum, $line, $stats_for_line, $statistics, $profile, $filestr) = @_; |
| 76 |
$line =~ s/^\s*//; # trim leading spaces |
| 77 |
my $delim = $opt{delim}; |
| 78 |
|
| 79 |
my $time = $stats_for_line->{'time'} || 0; |
| 80 |
my $calls = $stats_for_line->{'calls'} || 0; |
| 81 |
$time += $stats_for_line->{evalcall_stmts_time_nested} || 0; |
| 82 |
#$calls ||= 1 if exists $stats_for_line->{evalcall_stmts_time_nested}; |
| 83 |
|
| 84 |
my $text = sprintf("%f%s%g%s%f%s%s\n", |
| 85 |
$time, $delim, |
| 86 |
$calls, $delim, |
| 87 |
($calls) ? $time/$calls : 0, $delim, |
| 88 |
$line, |
| 89 |
); |
| 90 |
return $text unless $opt{annotated}; |
| 91 |
|
| 92 |
# srcline |
| 93 |
$text = "srcline$delim$text"; |
| 94 |
|
| 95 |
return $text; |
| 96 |
}); |
| 97 |
|
| 98 |
$reporter->set_param(mk_report_xsub_line => sub { "" }); |
| 99 |
|
| 100 |
# generate the files |
| 101 |
$reporter->report(); |
| 102 |
|
| 103 |
# Delete the previous database if it exists |
| 104 |
sub _delete { |
| 105 |
if (-d $opt{out}) { |
| 106 |
print "Deleting $opt{out}\n"; |
| 107 |
unlink glob($opt{out} . "/*"); |
| 108 |
unlink glob($opt{out} . "/.*"); |
| 109 |
rmdir $opt{out} or confess "Delete of $opt{out} failed: $!\n"; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
sub usage { |
| 114 |
print <<END |
| 115 |
usage: [perl] nytprofcsv [opts] |
| 116 |
--file <file>, -f <file> Use the specified file as Devel::NYTProf database |
| 117 |
file. [default: ./nytprof.out] |
| 118 |
--out <dir>, -o <dir> Place generated files here [default: ./nytprof] |
| 119 |
--delete, -d Delete the old fprofhtml output [uses --out] |
| 120 |
--help, -h Print this message |
| 121 |
|
| 122 |
This script is part of the Devel::NYTProf package |
| 123 |
See https://metacpan.org/pod/Devel::NYTProf |
| 124 |
END |
| 125 |
} |
| 126 |
|
| 127 |
__END__ |
| 128 |
|
| 129 |
=head1 NAME |
| 130 |
|
| 131 |
nytprofcsv - (DEPRECATED) L<Devel::NYTProf::Reader> CSV format implementation |
| 132 |
|
| 133 |
=head1 SYNOPSIS |
| 134 |
|
| 135 |
$ nytprofcsv [-h] [-d] [-o <output directory>] [-f <input file>] |
| 136 |
|
| 137 |
perl -d:NYTProf some_perl_app.pl |
| 138 |
nytprofcsv |
| 139 |
Generating CSV Output... |
| 140 |
|
| 141 |
=head1 NOTE |
| 142 |
|
| 143 |
B<nytprofcsv is deprecated and will be removed in a future release.> |
| 144 |
|
| 145 |
=head1 DESCRIPTION |
| 146 |
|
| 147 |
C<nytprofcsv> is a script that implements L<Devel::NYTProf::Reader> to |
| 148 |
create comma-separated value formatted reports from L<Devel::NYTProf> |
| 149 |
databases. |
| 150 |
|
| 151 |
See the L<Devel::NYTProf> Perl code profiler for more information. |
| 152 |
|
| 153 |
=head1 COMMAND-LINE OPTIONS |
| 154 |
|
| 155 |
These are the command line options understood by C<nytprofcsv> |
| 156 |
|
| 157 |
=over 4 |
| 158 |
|
| 159 |
=item -f, --file <filename> |
| 160 |
|
| 161 |
Specifies the location of the input file. The input file must be the |
| 162 |
output of L<fprofpp>. Default: nytprof.out |
| 163 |
|
| 164 |
=item -o, --out <dir> |
| 165 |
|
| 166 |
Where to place the generated report. Default: ./nytprof/ |
| 167 |
|
| 168 |
=item -d, --delete |
| 169 |
|
| 170 |
Purge any existing database located at whatever -o (above) is set to |
| 171 |
|
| 172 |
=item -h, --help |
| 173 |
|
| 174 |
Print the help message |
| 175 |
|
| 176 |
=back |
| 177 |
|
| 178 |
=head1 SAMPLE OUTPUT |
| 179 |
|
| 180 |
# Profile data generated by Devel::NYTProf::Reader v.0.01 |
| 181 |
# Format: time,calls,time/call,code |
| 182 |
0,0,0,#-------------------------------------------------------------------- |
| 183 |
0,0,0,# My New Source File! |
| 184 |
0,0,0,#-------------------------------------------------------------------- |
| 185 |
0,0,0, |
| 186 |
0,0,0,package NYT::Feeds::Util; |
| 187 |
0.00047,3,0.000156666666666667,use Date::Calc qw(Add_Delta_DHMS); |
| 188 |
0.00360,3,0.0012,use HTML::Entities; |
| 189 |
0.00212,3,0.000706666666666667,use Encode; |
| 190 |
0.00248,3,0.000826666666666667,use utf8; |
| 191 |
0.00468,3,0.00156,use strict; |
| 192 |
0,0,0, |
| 193 |
0.00000,1,0,require Exporter; |
| 194 |
... that's enough, get the picture? ... |
| 195 |
|
| 196 |
Note: The format line indicates what fields the numbers correspond to |
| 197 |
|
| 198 |
Note2: If the source file is modified between profiling and report generation, |
| 199 |
the source might be misaligned |
| 200 |
|
| 201 |
=head1 SEE ALSO |
| 202 |
|
| 203 |
Mailing list and discussion at L<http://groups.google.com/group/develnytprof-dev> |
| 204 |
|
| 205 |
Public Github Repository and hacking instructions at L<https://github.com/timbunce/devel-nytprof/> |
| 206 |
|
| 207 |
L<Devel::NYTProf> |
| 208 |
L<Devel::NYTProf::Reader> |
| 209 |
L<nytprofhtml> is an HTML implementation of L<Devel::NYTProf::Reader> |
| 210 |
|
| 211 |
=head1 AUTHOR |
| 212 |
|
| 213 |
Adam Kaplan, akaplan at nytimes dotcom |
| 214 |
|
| 215 |
=head1 COPYRIGHT AND LICENSE |
| 216 |
|
| 217 |
This program is free software; you can redistribute it and/or modify |
| 218 |
it under the same terms as Perl itself, either Perl version 5.8.8 or, |
| 219 |
at your option, any later version of Perl 5 you may have available. |
| 220 |
|
| 221 |
=cut |