xenoterracide
xenoterracide

Reputation: 16855

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?

Upvotes: 4

Views: 503

Answers (2)

hobbs
hobbs

Reputation: 240030

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 :)

Upvotes: 6

mpeters
mpeters

Reputation: 4778

I don't think it can be used that way. But you might take a look at Aspect::Library::Profiler or Aspect::Library::Timer

Upvotes: -1

Related Questions