Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use NYTProf in a library

I'm wondering if Devel::NYTProf can be used as a library in another library. I'd like to do something like the following

around 'somesub' => sub {
    my $orig = shift;
    my $self = shift;
    start-timing;
    $self->$orig(@_);
    end-timing;
    print '$time';
}

but from it's documentation I'm unable to determine if it can be used like this. Is it possible? could someone tell me the API calls that i'd do?

like image 404
xenoterracide Avatar asked Oct 14 '25 08:10

xenoterracide


1 Answers

The simplest, most reliable thing to do is:

  1. Add DB::enable_profile and DB::disable_profile calls in your library (you might want to check whether the subs are defined first, to avoid breakage when NYTProf isn't loaded).
  2. Start perl with -d:NYTProf and NYTPROF=start=no in the environment.

All of this is pretty clearly explained in the Devel::NYTProf docs.

You could try having your library conditionally load NYTProf, but the deal here is that only stuff compiled after NYTProf is loaded gets any tracepoints. That might sound perfectly okay, since you only want to profile your library, but it's not clear what will happen if your library calls out (or calls back) to any other code, and I didn't test it. It's probably a lot easier to make the simple version make do :)

like image 69
hobbs Avatar answered Oct 17 '25 09:10

hobbs