package Aspect::Library::Profiler; use strict; use warnings; use Carp; use Aspect; use base 'Aspect::Modular'; my $Timer = Benchmark::Timer::ReportOnDestroy->new; sub get_advice { my ($self, $pointcut) = @_; my $before = before { $Timer->start(shift->sub_name) } $pointcut; my $after = after { $Timer->stop (shift->sub_name) } $pointcut; return ($before, $after); } package Benchmark::Timer::ReportOnDestroy; use base qw(Benchmark::Timer); sub DESTROY { shift->report } 1; __END__ =head1 NAME Aspect::Library::Profiler - reusable method call profiling aspect =head1 SYNOPSIS # profile all subs on SlowObject aspect Profiler => call qr/^SlowObject::/; # will be profiled SlowObject->foo; # will not FastObject->bar; =head1 SUPER L =head1 DESCRIPTION This class implements a reusable aspect that profiles subroutine calls. It uses C to profile elapsed times for your calls to the affected methods. The profiling report will be printed to C at the end of program execution. The design comes from C by Tatsuhiko Miyagawa. =head1 WHY +-------------+ | A | +-------------+ | X -> Y <- Z | +-^-----------+ Suppose you want to profile some code, call it C, part of a larger program, called C. So you run your program under a profiler, and notice most of the time is spent not in C, but in C. C uses C, but so does C. You only want to profile how C uses C, not how C uses C. This is where this aspect can help- you can install a profiling aspect with a C pointcut, to profile only usage of C by code in the call flow of C. =head1 SEE ALSO See the L pods for a guide to the Aspect module. You can find an example of using this aspect in the C directory of the distribution. =cut