root/Attribute-Profiled/trunk/lib/Attribute/Profiled.pm

Revision 254 (checked in by miyagawa, 19 years ago)

0.02

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 package Attribute::Profiled;
2
3 use 5.006;
4 use strict;
5 use warnings;
6
7 our $VERSION = '0.02';
8
9 use Attribute::Handlers;
10
11 our $_Profiler;
12
13 sub UNIVERSAL::Profiled : ATTR(CODE) {
14     my($package, $symbol, $referent, $attr, $data, $phase) = @_;
15     my $meth = *{$symbol}{NAME};
16     no warnings 'redefine';
17
18     *{$symbol} = sub {
19         unless ($_Profiler) {
20             $_Profiler = Benchmark::Timer::ReportOnDestroy->new;
21         }
22         $_Profiler->start("$package\::$meth");
23         my @ret = wantarray ? $referent->(@_) : scalar $referent->(@_);
24         $_Profiler->stop("$package\::$meth");
25         return wantarray ? @ret : $ret[0];
26     };
27 }
28
29 package Benchmark::Timer::ReportOnDestroy;
30 use base qw(Benchmark::Timer);
31
32 sub DESTROY {
33     my $self = shift;
34     $self->report;
35 }
36
37
38 1;
39 __END__
40
41 =head1 NAME
42
43 Attribute::Profiled - Profiles specific methods in class
44
45 =head1 SYNOPSIS
46
47   package SomeClass;
48   use Attribute::Profiled;
49
50   sub long_running_method : Profiled { }
51
52 =head1 DESCRIPTION
53
54 Attribute::Profiled provides a way to profile specific methods with
55 attributes. This module uses Benchmark::Timer to profile elapsed times
56 for your calls to the methods with Profiled attribute on.
57
58 Profiling report will be printed to STDERR at the end of program
59 execution.
60
61 =head1 TODO
62
63 =over 4
64
65 =item *
66
67 Options where to print profiling report.
68
69 =item *
70
71 Allows public way to get reports in any timing other than the end of
72 execution. Currently you can do it by explicitly calling report() on
73 C<$Attribute::Profiled::_Profiler>.
74
75 =item *
76
77 Currently it's not caller sensitive (doesn't use goto).
78
79 =back
80
81 =head1 AUTHOR
82
83 Tatsuhiko Miyagawa E<lt>miyagawa@bulknews.netE<gt>
84
85 This library is free software; you can redistribute it and/or
86 modify it under the same terms as Perl itself.
87
88 =head1 SEE ALSO
89
90 L<Attribute::Handlers>, L<Benchmark::Timer>
91
92 =cut
Note: See TracBrowser for help on using the browser.