Archive Ensembl HomeArchive Ensembl Home
Eprof.pm
Go to the documentation of this file.
00001 =head1 LICENSE
00002 
00003   Copyright (c) 1999-2012 The European Bioinformatics Institute and
00004   Genome Research Limited.  All rights reserved.
00005 
00006   This software is distributed under a modified Apache license.
00007   For license details, please see
00008 
00009     http://www.ensembl.org/info/about/code_licence.html
00010 
00011 =head1 CONTACT
00012 
00013   Please email comments or questions to the public Ensembl
00014   developers list at <dev@ensembl.org>.
00015 
00016   Questions may also be sent to the Ensembl help desk at
00017   <helpdesk@ensembl.org>.
00018 
00019 =cut
00020 
00021 =head1 NAME
00022 
00023 Bio::EnsEMBL::Utils::Eprof - Bespoke Ensembl profiler
00024 
00025 =head1 SYNOPSIS
00026 
00027   use Bio::EnsEMBL::Utils::Eprof( 'eprof_start', 'eprof_end',
00028     'eprof_dump' );
00029 
00030   &eprof_start('function-a');
00031   # ... do something
00032   &eprof_end('function-a');
00033 
00034   &eprof_dump( \*STDERR );
00035 
00036   # there is an object based set for above as well, for running
00037   # multiple concurrent profilers
00038 
00039 =head1 DESCRIPTION
00040 
00041 This is an Ensembl profiler as we broke the Perl profilers.
00042 
00043 =head1 METHODS
00044 
00045 =cut
00046 
00047 package Bio::EnsEMBL::Utils::Eprof;
00048 
00049 use strict;
00050 use warnings;
00051 
00052 use Bio::EnsEMBL::Utils::Exception ('throw');
00053 use Bio::EnsEMBL::Utils::EprofStack;
00054 
00055 use base qw( Exporter );
00056 
00057 our @EXPORT_OK =
00058   ( 'eprof_start', 'eprof_end', 'eprof_dump', 'eprof_reset' );
00059 
00060 my $global;
00061 
00062 sub new {
00063   my ($proto) = @_;
00064 
00065   my $class = ref($proto) || $proto;
00066   my $self = bless( { '_tags' => {} }, $class );
00067 
00068   return $self;
00069 }
00070 
00071 =head2 eprof_start
00072 
00073  Title   : eprof_start
00074  Usage   :
00075  Function:
00076  Example :
00077  Returns : 
00078  Args    :
00079 
00080 
00081 =cut
00082 
00083 sub eprof_start {
00084   my ($tag) = @_;
00085 
00086   if ( !defined($global) ) {
00087     $global = Bio::EnsEMBL::Utils::Eprof->new();
00088   }
00089 
00090   $global->start($tag);
00091 }
00092 
00093 =head2 eprof_end
00094 
00095  Title   : eprof_end
00096  Usage   :
00097  Function:
00098  Example :
00099  Returns : 
00100  Args    :
00101 
00102 
00103 =cut
00104 
00105 sub eprof_end {
00106   my ($tag) = @_;
00107 
00108   if ( !defined($global) ) {
00109     $global = Bio::EnsEMBL::Utils::Eprof->new();
00110   }
00111 
00112   $global->end($tag);
00113 }
00114 
00115 sub eprof_dump {
00116   my ($fh) = @_;
00117 
00118   if ( !defined($global) ) { return }
00119 
00120   $global->dump($fh);
00121 }
00122 
00123 =head2 eprof_reset
00124 
00125  Title   : eprof_reset
00126  Usage   :
00127  Function:
00128  Example :
00129  Returns : 
00130  Args    :
00131 
00132 
00133 =cut
00134 
00135 sub eprof_reset { undef($global) }
00136 
00137 =head2 dump
00138 
00139  Title   : dump
00140  Usage   :
00141  Function:
00142  Example :
00143  Returns : 
00144  Args    :
00145 
00146 
00147 =cut
00148 
00149 sub dump {
00150   my ( $self, $fh ) = @_;
00151 
00152   my @tags = sort {
00153     $self->_tags()->{$a}->total_time()
00154       <=> $self->_tags()->{$b}->total_time()
00155   } keys %{ $self->_tags() };
00156 
00157   foreach my $tag (@tags) {
00158     my $st = $self->_tags->{$tag};
00159 
00160     if ( $st->number() == 0 ) { next }
00161 
00162     my $STD = '---';
00163 
00164     if ( $st->number() > 1 ) {
00165       my $SS =
00166         $st->total_time_time() -
00167         $st->total_time()*$st->total_time()/$st->number();
00168 
00169       if ( $SS > 0 ) {
00170         $STD = sprintf( "%6f",
00171                         sqrt( $SS/$st->number()/( $st->number() - 1 ) )
00172         );
00173       }
00174     }
00175 
00176     print( $fh sprintf( "Eprof: %20s  %6f  %6f  %d  %s  [%6f,%6f]\n",
00177                         $st->tag(), $st->total_time(),
00178                         $st->total_time()/$st->number(), $st->number(),
00179                         $STD, $st->min_time(),
00180                         $st->max_time() ) );
00181   } ## end foreach my $tag (@tags)
00182 } ## end sub dump
00183 
00184 =head2 start
00185 
00186  Title   : start
00187  Usage   : $eprof->start('this_tag');
00188  Function:
00189  Example :
00190  Returns : 
00191  Args    :
00192 
00193 
00194 =cut
00195 
00196 sub start {
00197   my ( $self, $tag ) = @_;
00198 
00199   if ( !defined($tag) ) {
00200     $self->throw("No tag, can't start.");
00201   }
00202 
00203   if ( !defined( $self->_tags()->{$tag} ) ) {
00204     $self->_tags()->{$tag} = Bio::EnsEMBL::Utils::EprofStack->new($tag);
00205   }
00206 
00207   $self->_tags()->{$tag}->push_stack();
00208 }
00209 
00210 =head2 end
00211 
00212  Title   : end
00213  Usage   : $eprof->end('this_tag');
00214  Function:
00215  Example :
00216  Returns : 
00217  Args    :
00218 
00219 
00220 =cut
00221 
00222 sub end {
00223   my ( $self, $tag ) = @_;
00224 
00225   if ( !defined($tag) ) {
00226     $self->throw("No tag, can't end.");
00227   }
00228 
00229   if ( !defined( $self->_tags()->{$tag} ) ) {
00230     $self->throw(
00231                 sprintf( "Ending with a nonexistant tag '%s'", $tag ) );
00232   }
00233 
00234   $self->_tags->{$tag}->pop_stack();
00235 }
00236 
00237 =head2 _tags
00238 
00239  Title   : _tags
00240  Usage   : $obj->_tags($newval)
00241  Function: 
00242  Returns : value of _tags
00243  Args    : newvalue (optional)
00244 
00245 
00246 =cut
00247 
00248 sub _tags {
00249   my ($obj) = @_;
00250   return $obj->{'_tags'};
00251 }
00252 
00253 1;
00254