EnsEMBL/Utils/Eprof.pm untested

filter: Statementstmt 0.0% 0 / 66 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 3 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 112 CC 11 SCAR 21.4
CC
11
Cov
0%
dump
20.0
start
12.0
end
12.0
CRAP 8.5
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 $Bio::EnsEMBL::Utils::Eprof::VERSION = '114.0.0_50'; # TRIAL
59 $Bio::EnsEMBL::Utils::Eprof::VERSION = '114.0.050';
60 use strict;
61 use warnings;
62  
63 use Bio::EnsEMBL::Utils::Exception ('throw');
64 use Bio::EnsEMBL::Utils::EprofStack;
65  
66 use base qw( Exporter );
67  
68 our @EXPORT_OK =
69 ( 'eprof_start', 'eprof_end', 'eprof_dump', 'eprof_reset' );
70  
71 my $global;
72  
73 sub new {
74 my ($proto) = @_;
75  
76 my $class = ref($proto) || $proto;
77 my $self = bless( { '_tags' => {} }, $class );
78  
79 return $self;
80 }
81  
82 =head2 eprof_start
83  
84 Title : eprof_start
85 Usage :
86 Function:
87 Example :
88 Returns :
89 Args :
90  
91  
92 =cut
93  
94 sub eprof_start {
95 my ($tag) = @_;
96  
97 if ( !defined($global) ) {
98 $global = Bio::EnsEMBL::Utils::Eprof->new();
99 }
100  
101 $global->start($tag);
102 }
103  
104 =head2 eprof_end
105  
106 Title : eprof_end
107 Usage :
108 Function:
109 Example :
110 Returns :
111 Args :
112  
113  
114 =cut
115  
116 sub eprof_end {
117 my ($tag) = @_;
118  
119 if ( !defined($global) ) {
120 $global = Bio::EnsEMBL::Utils::Eprof->new();
121 }
122  
123 $global->end($tag);
124 }
125  
126 sub eprof_dump {
127 my ($fh) = @_;
128  
129 if ( !defined($global) ) { return }
130  
131 $global->dump($fh);
132 }
133  
134 =head2 eprof_reset
135  
136 Title : eprof_reset
137 Usage :
138 Function:
139 Example :
140 Returns :
141 Args :
142  
143  
144 =cut
145  
146 sub eprof_reset { undef($global) }
147  
148 =head2 dump
149  
150 Title : dump
151 Usage :
152 Function:
153 Example :
154 Returns :
155 Args :
156  
157  
158 =cut
159  
160 sub dump {
161 my ( $self, $fh ) = @_;
162  
163 my @tags = sort {
164 $self->_tags()->{$a}->total_time()
165 <=> $self->_tags()->{$b}->total_time()
166 } keys %{ $self->_tags() };
167  
168 foreach my $tag (@tags) {
169 my $st = $self->_tags->{$tag};
170  
171 if ( $st->number() == 0 ) { next }
172  
173 my $STD = '---';
174  
175 if ( $st->number() > 1 ) {
176 my $SS =
177 $st->total_time_time() -
178 $st->total_time()*$st->total_time()/$st->number();
179  
180 if ( $SS > 0 ) {
181 $STD = sprintf( "%6f",
182 sqrt( $SS/$st->number()/( $st->number() - 1 ) )
183 );
184 }
185 }
186  
187 print( $fh sprintf( "Eprof: %20s %6f %6f %d %s [%6f,%6f]\n",
188 $st->tag(), $st->total_time(),
189 $st->total_time()/$st->number(), $st->number(),
190 $STD, $st->min_time(),
191 $st->max_time() ) );
192 } ## end foreach my $tag (@tags)
193 } ## end sub dump
194  
195 =head2 start
196  
197 Title : start
198 Usage : $eprof->start('this_tag');
199 Function:
200 Example :
201 Returns :
202 Args :
203  
204  
205 =cut
206  
207 sub start {
208 my ( $self, $tag ) = @_;
209  
210 if ( !defined($tag) ) {
211 $self->throw("No tag, can't start.");
212 }
213  
214 if ( !defined( $self->_tags()->{$tag} ) ) {
215 $self->_tags()->{$tag} = Bio::EnsEMBL::Utils::EprofStack->new($tag);
216 }
217  
218 $self->_tags()->{$tag}->push_stack();
219 }
220  
221 =head2 end
222  
223 Title : end
224 Usage : $eprof->end('this_tag');
225 Function:
226 Example :
227 Returns :
228 Args :
229  
230  
231 =cut
232  
233 sub end {
234 my ( $self, $tag ) = @_;
235  
236 if ( !defined($tag) ) {
237 $self->throw("No tag, can't end.");
238 }
239  
240 if ( !defined( $self->_tags()->{$tag} ) ) {
241 $self->throw(
242 sprintf( "Ending with a nonexistant tag '%s'", $tag ) );
243 }
244  
245 $self->_tags->{$tag}->pop_stack();
246 }
247  
248 =head2 _tags
249  
250 Title : _tags
251 Usage : $obj->_tags($newval)
252 Function:
253 Returns : value of _tags
254 Args : newvalue (optional)
255  
256  
257 =cut
258  
259 sub _tags {
260 my ($obj) = @_;
261 return $obj->{'_tags'};
262 }
263  
264 1;
265