Article 12913 of comp.lang.perl: Xref: feenix.metronet.com comp.lang.perl:12913 Newsgroups: comp.lang.perl Path: feenix.metronet.com!news.utdallas.edu!corpgate!bnrgate!nott!torn!howland.reston.ans.net!pipex!uunet!butch!enterprise!news From: schip@sgi428 (Jan Schipmolder) Subject: Re: Perl Profiling ? Message-ID: <1994Apr20.042209.3398@enterprise.rdd.lmsc.lockheed.com> Sender: news@enterprise.rdd.lmsc.lockheed.com (News Administrator) Organization: Lockheed Missiles & Space Co. X-Newsreader: Tin 1.1 PL4 References: <1994Apr18.180430.22260@lts.sel.alcatel.de> Date: Wed, 20 Apr 94 04:22:09 GMT Lines: 151 mohr@slsvett.lts.sel.alcatel.de (Thomas Mohr US/ESC 60/3/151 #71796) writes: : I'm looking for some profiling package on perl. Does anybody in netland I wrote this profiler. It works for me. I did not document the assumptions it makes about your coding style. But if you run it with the -h option it should give you some usage information. Hope this help. Toodeloo. ===== from here till next ===== #!/usr/local/bin/perl require 'getopts.pl'; &Getopts ( 'hpx' ) || ( &usage, exit ) ; &usage, exit if $opt_h; die "cannot use both p and x options\n" if $opt_p && $opt_x; die "must have exactly one of p or x options\n" if !$opt_p && !$opt_x; if ( $opt_x ) { # This is the part of plprof that creates a new script out of the one # offered on the command line. read_x: while ( <> ) { # keep reading lines till you see an isolated __END__, if any next read_x if /^\s*$/; # ignore blank lines next read_x if /^\s*\#/ && $. > 1 ; # ignore obvious comment lines last read_x if /^\s*__END__\s*$/; # On any line that ends with an {, append a statement that counts # the number of passages. if ( /{\s*$/ ) { $x = sprintf ( "{ \$_KOUNT[%d]++;", $_iKOUNT++ ); s/{\s*$/$x\n/; } print; } # Now, at the end of the input file, append statements that output # the accumulated counts to a file named a.perlprofile. print 'open ( _FILE, ">a.perlprofile" ) || ', "\n"; print 'die "cannot open >a.perlprofile\n";', "\n"; print ' for ( $_jKOUNT=0; $_jKOUNT<',$_iKOUNT,'; $_jKOUNT++ ) { ', "\n"; print ' print _FILE $_KOUNT [ $_jKOUNT ], "\n"; ', "\n"; print ' } ', "\n"; } elsif ( $opt_p ) { # This is the part of plprof that reads the original script (from the # command line) as well as the counts accumulated by the modified # script in a.perlprofile. open ( _FILE, " ) { s/\s*$//; # Don't bother with anything after the __END__ next read_p if /^\s*$/; # ignore blank lines next read_p if /^\s*\#/ && $. > 1; # ignore obvious comment lines last read_p if /^\s*__END__\s*$/; if ( /{\s*$/ ) { $kount = <_FILE>; printf ( "%5d %s\n", $kount, substr($_,0,75) ); } else { printf ( "%5s %s\n", ' ', substr($_,0,75) ); } } } else { die "coding error 0835\n" } sub usage { print ; } __END__ Name: plprof Purpose: profile a perl script (count number of executions of statements) Usage: to make the profile: plprof -x yourscript > anyname chmod 755 anyname anyname yourargs to print the profile plprof -p yourscript where: -x -- causes plprof to make anyname out of yourscript -p -- causes plprof to read stats from anyname and print them yourscript -- the script you want to profile anyname -- the temporary script you run instead of yourscript yourargs -- the arguments expected by yourscript Example: plprof -x dirSize > a.Dummy chmod +x a.Dummy a.Dummy alfa 3 beta plprof -p dirSize In this example, we want to see how many times each perl statement in dirSize gets executed. To that effect, we first convert dirSize to a.Dummy, which is a modified version of dirSize, having additional counting statements appended to any line in dirSize that ends with a left curly bracket. After making a.Dummy executable, you run it, feeding it the arguments you would normally feed to dirSize, "alfa 3 beta" in this case. While you are running a.Dummy, a file named a.perlprofile is being created, containing the stats needed for the second execution of plprof. Finally, you run plprof a second time, telling it the name of the script with which you started (dirSize). Plprof reads both a.perlprofile and dirSize, and prints a modified version of dirSize, one showing the stats accumulated by a.Dummy. In the left margin of the printout, on each line that ends with a left curly bracket, is shown the number of times that dirSize "passed through that bracket". Notes: (*) Should, but does not, check for lines in yourscript that start with "#". It makes no sense profiling comment lines ... -- jan b schipmolder ===== -- jan b schipmolder schip@lmsc.lockheed.com