| 1 | package DashProfiler::Auto; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use Carp; | ||
| 6 | |||
| 7 | use base qw(DashProfiler::Import); | ||
| 8 | |||
| 9 | our $VERSION = sprintf("1.%06d", q$Revision: 45 $ =~ /(\d+)/o); | ||
| 10 | |||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | DashProfiler::Auto - Creates and imports a predeclared DashProfiler sampler | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | Measure the time spent between creating a sample object and destroying it: | ||
| 19 | |||
| 20 | $ perl -MDashProfiler::Auto -w -e '$a=auto_profiler("foobar"); sleep 1; undef $a' | ||
| 21 | auto > -e > foobar: dur=1.000231 count=1 (max=1.000231 avg=1.000231) | ||
| 22 | auto > other > other: dur=0.000451 count=1 (max=0.000451 avg=0.000451) | ||
| 23 | |||
| 24 | The leading "auto > -e > foobar" portion shows the name of the profiler (auto), | ||
| 25 | the name of the file that called it (in this case "-e" because the code was | ||
| 26 | given on the command line), and finally the argument given to auto_profiler(). | ||
| 27 | |||
| 28 | The time shown as "auto > other > other" is all the 'other' time spent by the | ||
| 29 | program that isn't included in the samples taken. | ||
| 30 | |||
| 31 | This next example shows use of the 'C<context2>' parameter to auto_profiler() | ||
| 32 | and also how to samples can overlap: | ||
| 33 | |||
| 34 | $ perl -MDashProfiler::Auto -w -e ' | ||
| 35 | sub fib { | ||
| 36 | my $n = shift; | ||
| 37 | return $n if $n < 2; | ||
| 38 | my $s = auto_profiler($n, undef, 1); | ||
| 39 | fib($n-1) + fib($n-2); | ||
| 40 | } | ||
| 41 | fib(7) | ||
| 42 | ' | ||
| 43 | auto > -e > 2: dur=0.000054 count=8 (max=0.000013 avg=0.000002) | ||
| 44 | auto > -e > 3: dur=0.000137 count=5 (max=0.000046 avg=0.000009) | ||
| 45 | auto > -e > 4: dur=0.000197 count=3 (max=0.000085 avg=0.000028) | ||
| 46 | auto > -e > 5: dur=0.000245 count=2 (max=0.000139 avg=0.000069) | ||
| 47 | auto > -e > 6: dur=0.000226 count=1 (max=0.000226 avg=0.000226) | ||
| 48 | auto > -e > 7: dur=0.000370 count=1 (max=0.000370 avg=0.000370) | ||
| 49 | |||
| 50 | The timing for "other" isn't shown because if any samples do overlap then | ||
| 51 | the C<period_exclusive> summary is disabled. | ||
| 52 | |||
| 53 | =head1 DESCRIPTION | ||
| 54 | |||
| 55 | The DashProfiler::Auto is designed for quick temporary use of DashProfiler. | ||
| 56 | It avoids the need to create a profile by creating one for you with a typical | ||
| 57 | configuration, and defining a I<sample period> from the time the module was | ||
| 58 | loaded to the time the program exits. | ||
| 59 | |||
| 60 | =head2 Profiler Configuration | ||
| 61 | |||
| 62 | The default configuration is: | ||
| 63 | |||
| 64 | my $auto = DashProfiler->add_profile( auto => { | ||
| 65 | period_exclusive => 'other', | ||
| 66 | flush_hook => sub { | ||
| 67 | my ($self, $dbi_profile_name) = @_; | ||
| 68 | warn $_ for $self->profile_as_text($dbi_profile_name); | ||
| 69 | return $self->reset_profile_data($dbi_profile_name); | ||
| 70 | }, | ||
| 71 | }); | ||
| 72 | |||
| 73 | B<*> C<period_exclusive> enables the automatic addition of a sample which measures the time I<not> accounted for by the samples. See L</Sample Periods> below. | ||
| 74 | |||
| 75 | B<*> C<flush_hook> uses warn() to write out the profile data, then resets (discards) the data. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | my $auto = DashProfiler->add_profile( auto => { | ||
| 80 | period_exclusive => 'other', | ||
| 81 | profile_as_text_args => { | ||
| 82 | separator => " > ", | ||
| 83 | }, | ||
| 84 | flush_hook => sub { | ||
| 85 | my ($self, $dbi_profile_name) = @_; | ||
| 86 | warn $_ for $self->profile_as_text($dbi_profile_name); | ||
| 87 | return $self->reset_profile_data($dbi_profile_name); | ||
| 88 | }, | ||
| 89 | }); | ||
| 90 | |||
| 91 | =head2 Sample Periods | ||
| 92 | |||
| 93 | When the modle is loaded it creates the C<auto> profile and calls | ||
| 94 | |||
| 95 | $auto->start_sample_period(); | ||
| 96 | |||
| 97 | It also defines an C<END> block which does | ||
| 98 | |||
| 99 | $auto->end_sample_period(); | ||
| 100 | $auto->flush; | ||
| 101 | |||
| 102 | so the sample period runs from the time the module was loaded (typically when | ||
| 103 | the program started running) through to the time the program exits. | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | $auto->start_sample_period(); | ||
| 108 | |||
| 109 | END { | ||
| 110 | $auto->end_sample_period(); | ||
| 111 | $auto->flush; | ||
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | =head2 Imported Samplers | ||
| 116 | |||
| 117 | Using DashProfile::Auto in a file like this: | ||
| 118 | |||
| 119 | use DashProfiler::Auto; | ||
| 120 | |||
| 121 | is almost identical to this: | ||
| 122 | |||
| 123 | use DashProfiler::Import auto_profiler => [ basename(__FILE__) ]; | ||
| 124 | |||
| 125 | =cut | ||
| 126 | |||
| 127 | sub import { | ||
| 128 | my $class = shift; | ||
| 129 | |||
| 130 | croak "DashProfile::Auto doesn't support explicit imports" | ||
| 131 | if @_; | ||
| 132 | |||
| 133 | local $DashProfiler::Import::ExportLevel = $DashProfiler::Import::ExportLevel + 1; | ||
| 134 | |||
| 135 | my $caller_file = (caller)[1]; | ||
| 136 | $caller_file =~ s:.*[/\\]::; # delete everything upto and including the last slash or backslash | ||
| 137 | $class->SUPER::import( auto_profiler => [ $caller_file ] ); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | 1; | ||
| 142 | |||
| 143 | =head1 AUTHOR | ||
| 144 | |||
| 145 | DashProfiler by Tim Bunce, L<http://www.tim.bunce.name> and | ||
| 146 | L<http://blog.timbunce.org> | ||
| 147 | |||
| 148 | =head1 COPYRIGHT | ||
| 149 | |||
| 150 | The DashProfiler distribution is Copyright (c) 2007-2008 Tim Bunce. Ireland. | ||
| 151 | All rights reserved. | ||
| 152 | |||
| 153 | You may distribute under the terms of either the GNU General Public | ||
| 154 | License or the Artistic License, as specified in the Perl README file. | ||
| 155 | |||
| 156 | =cut | ||
| 157 |