How can I profile a routine without using modules?

I am tempted to redo this question: 'Look at this brick. What type of house does it belong to?'

Here's the situation: I was asked to profile some routines that have access to neither profilers (even Devel::DProf) nor Time::HiRes. The goal of this exercise is to “find” bottlenecks.

At the moment, I am deploying print statements at the beginning and end of each subsection that log entries and file outputs, as well as the result of the function time. Not perfect, but it's the best I can do, given the circumstances. At least this will allow me to see how many times each south is called.

The code runs on Unix. The closest I see to my need is perlfaq8 , but that doesn’t seem to help (I don’t know how to make a syscall, and I wonder if this will affect the time of the code unpredictably).

Not your typical everyday question so ...

+3
source share
4 answers

This method should work.

Basically, the idea is that if you run Perl with a flag -d, it will go into the debugger. Then, when you run the program, ctrl-Break or ctrl-C should make it pause in the middle of what it does. You can then enter Tto show the stack, and examine any other variables, if you like, before continuing.

10 20 . ( , ), , , , .

, ( , ) 20% , 20 , 4 , 1,8 . , , , - 20% .

, .

, , "" . . - .

+7
+1

, . , . - . perldebguts , , .

, , Devel:: NYTProf , , , , .

, " ", . , , ( ) . " ", , .

-, , , ? ? (, - , - , )?

? , , , , , ?

0

, - , R, , Excel - (CSV ). - . Perl 5,7 ( Time: HiRes ), syscall, , Time:: HiRes .

INIT {
    sub wrap_sub {
        no strict 'refs';
        my $sub = shift;
        my $subref = *{$sub}{CODE};
        return sub {
            local *__ANON__ = "wrapped_$sub";
            my $fsecs = Time::HiRes::gettimeofday();
            print STDERR "$sub,$fsecs,";
            if (wantarray) {
               @return = eval { $subref->(@_) } or die $@;
            } else {
               $return[0] = eval { $subref->(@_) } or die $@;
            }

            $fsecs = Time::HiRes::gettimeofday();
            print STDERR "$fsecs\n";
            return wantarray ? @return : $return[0];
        };
    }
    require Time::HiRes;
    my @subs = qw{the subs you want to profile};
    no strict 'refs';
    no warnings 'redefine';
    foreach my $sub (@subs) {
        *{$sub} = wrap_sub($sub);
    }    
}

", " , , open() ed file STDERR, , , script ( Unix, bourne, korn bash),

perl ./myscript.pl 2>myscript.profile
0

Source: https://habr.com/ru/post/1742672/


All Articles