ensembl-hive  2.7.0
Eprof.pm
Go to the documentation of this file.
1 =head1 LICENSE
2 
3 See the NOTICE file distributed with this work for additional information
4 regarding copyright ownership.
5 
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9 
10  http://www.apache.org/licenses/LICENSE-2.0
11 
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17 
18 =cut
19 
20 
21 =head1 CONTACT
22 
23  Please email comments or questions to the public Ensembl
24  developers list at <http://lists.ensembl.org/mailman/listinfo/dev>.
25 
26  Questions may also be sent to the Ensembl help desk at
27  <http://www.ensembl.org/Help/Contact>.
28 
29 =cut
30 
31 =head1 NAME
32 
33 Bio::EnsEMBL::Utils::Eprof - Bespoke Ensembl profiler
34 
35 =head1 SYNOPSIS
36 
37  use Bio::EnsEMBL::Utils::Eprof( 'eprof_start', 'eprof_end',
38  'eprof_dump' );
39 
40  &eprof_start('function-a');
41  # ... do something
42  &eprof_end('function-a');
43 
44  &eprof_dump( \*STDERR );
45 
46  # there is an object based set for above as well, for running
47  # multiple concurrent profilers
48 
49 =head1 DESCRIPTION
50 
51 This is an Ensembl profiler as we broke the Perl profilers.
52 
53 =head1 METHODS
54 
55 =cut
56 
57 package Bio::EnsEMBL::Utils::Eprof;
58 
59 use strict;
60 use warnings;
61 
62 use Bio::EnsEMBL::Utils::Exception ('throw');
64 
65 use base qw( Exporter );
66 
67 our @EXPORT_OK =
68  ( 'eprof_start', 'eprof_end', 'eprof_dump', 'eprof_reset' );
69 
70 my $global;
71 
72 sub new {
73  my ($proto) = @_;
74 
75  my $class = ref($proto) || $proto;
76  my $self = bless( { '_tags' => {} }, $class );
77 
78  return $self;
79 }
80 
81 =head2 eprof_start
82 
83  Title : eprof_start
84  Usage :
85  Function:
86  Example :
87  Returns :
88  Args :
89 
90 
91 =cut
92 
93 sub eprof_start {
94  my ($tag) = @_;
95 
96  if ( !defined($global) ) {
97  $global = Bio::EnsEMBL::Utils::Eprof->new();
98  }
99 
100  $global->start($tag);
101 }
102 
103 =head2 eprof_end
104 
105  Title : eprof_end
106  Usage :
107  Function:
108  Example :
109  Returns :
110  Args :
111 
112 
113 =cut
114 
115 sub eprof_end {
116  my ($tag) = @_;
117 
118  if ( !defined($global) ) {
119  $global = Bio::EnsEMBL::Utils::Eprof->new();
120  }
121 
122  $global->end($tag);
123 }
124 
125 sub eprof_dump {
126  my ($fh) = @_;
127 
128  if ( !defined($global) ) { return }
129 
130  $global->dump($fh);
131 }
132 
133 =head2 eprof_reset
134 
135  Title : eprof_reset
136  Usage :
137  Function:
138  Example :
139  Returns :
140  Args :
141 
142 
143 =cut
144 
145 sub eprof_reset { undef($global) }
146 
147 =head2 dump
148 
149  Title : dump
150  Usage :
151  Function:
152  Example :
153  Returns :
154  Args :
155 
156 
157 =cut
158 
159 sub dump {
160  my ( $self, $fh ) = @_;
161 
162  my @tags = sort {
163  $self->_tags()->{$a}->total_time()
164  <=> $self->_tags()->{$b}->total_time()
165  } keys %{ $self->_tags() };
166 
167  foreach my $tag (@tags) {
168  my $st = $self->_tags->{$tag};
169 
170  if ( $st->number() == 0 ) { next }
171 
172  my $STD = '---';
173 
174  if ( $st->number() > 1 ) {
175  my $SS =
176  $st->total_time_time() -
177  $st->total_time()*$st->total_time()/$st->number();
178 
179  if ( $SS > 0 ) {
180  $STD = sprintf( "%6f",
181  sqrt( $SS/$st->number()/( $st->number() - 1 ) )
182  );
183  }
184  }
185 
186  print( $fh sprintf( "Eprof: %20s %6f %6f %d %s [%6f,%6f]\n",
187  $st->tag(), $st->total_time(),
188  $st->total_time()/$st->number(), $st->number(),
189  $STD, $st->min_time(),
190  $st->max_time() ) );
191  } ## end foreach my $tag (@tags)
192 } ## end sub dump
193 
194 =head2 start
195 
196  Title : start
197  Usage : $eprof->start('this_tag');
198  Function:
199  Example :
200  Returns :
201  Args :
202 
203 
204 =cut
205 
206 sub start {
207  my ( $self, $tag ) = @_;
208 
209  if ( !defined($tag) ) {
210  $self->throw("No tag, can't start.");
211  }
212 
213  if ( !defined( $self->_tags()->{$tag} ) ) {
214  $self->_tags()->{$tag} = Bio::EnsEMBL::Utils::EprofStack->new($tag);
215  }
216 
217  $self->_tags()->{$tag}->push_stack();
218 }
219 
220 =head2 end
221 
222  Title : end
223  Usage : $eprof->end('this_tag');
224  Function:
225  Example :
226  Returns :
227  Args :
228 
229 
230 =cut
231 
232 sub end {
233  my ( $self, $tag ) = @_;
234 
235  if ( !defined($tag) ) {
236  $self->throw("No tag, can't end.");
237  }
238 
239  if ( !defined( $self->_tags()->{$tag} ) ) {
240  $self->throw(
241  sprintf( "Ending with a nonexistant tag '%s'", $tag ) );
242  }
243 
244  $self->_tags->{$tag}->pop_stack();
245 }
246 
247 =head2 _tags
248 
249  Title : _tags
250  Usage : $obj->_tags($newval)
251  Function:
252  Returns : value of _tags
253  Args : newvalue (optional)
254 
255 
256 =cut
257 
258 sub _tags {
259  my ($obj) = @_;
260  return $obj->{'_tags'};
261 }
262 
263 1;
264 
Bio::EnsEMBL::Utils::EprofStack
Definition: EprofStack.pm:16
Bio::EnsEMBL::Utils::Eprof::new
public new()
Bio::EnsEMBL::Utils::Eprof
Definition: Eprof.pm:27
Bio::EnsEMBL::Utils::EprofStack::new
public new()
Bio::EnsEMBL::Utils::Exception
Definition: Exception.pm:68