views:

45

answers:

2

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?

A: 

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

mpeters
What's the advantage of using those? curiously that Profiler mentions Benchmark::Timer is there any advantage to using those over Benchmark::Timer?
xenoterracide
You can add them and remove them in various parts of your program without having to actually change your program code (it's Aspect Oriented Programming). See http://use.perl.org/~Alias/journal/40457It also mentions Aspect::Library::NYTProf which might be what you're looking for.
mpeters
A: 

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

hobbs