ensembl-hive  2.6
Stopwatch.pm
Go to the documentation of this file.
1 
2 =pod
3 
4 =head1 NAME
5 
7 
8 =head1 SYNOPSIS
9 
10 
11  my $total_stopwatch = Bio::EnsEMBL::Hive::Utils::Stopwatch->new()->restart;
12  my $fetching_stopwatch = Bio::EnsEMBL::Hive::Utils::Stopwatch->new();
13  my $running_stopwatch = Bio::EnsEMBL::Hive::Utils::Stopwatch->new();
14 
15  $fetching_stopwatch->continue();
16  $runnable->fetch_input();
17  $fetching_stopwatch->pause();
18 
19  $running_stopwatch->continue();
20  $runnable->run();
21  $running_stopwatch->pause();
22 
23  # ...
24 
25  my $only_fetches = $fetching_stopwatch->get_elapsed; # probably stopped
26  my $total_time_elapsed = $total_stopwatch->get_elapsed; # running through
27 
28 =head1 DESCRIPTION
29 
30  This is a standalone object used to time various events in the Hive.
31 
32 =head1 LICENSE
33 
34  Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
35  Copyright [2016-2024] EMBL-European Bioinformatics Institute
36 
37  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
38  You may obtain a copy of the License at
39 
40  http://www.apache.org/licenses/LICENSE-2.0
41 
42  Unless required by applicable law or agreed to in writing, software distributed under the License
43  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
44  See the License for the specific language governing permissions and limitations under the License.
45 
46 =head1 CONTACT
47 
48  Please subscribe to the Hive mailing list: http://listserver.ebi.ac.uk/mailman/listinfo/ehive-users to discuss Hive-related questions or to be notified of our updates
49 
50 =cut
51 
52 
53 package Bio::EnsEMBL::Hive::Utils::Stopwatch;
54 
55 use strict;
56 use warnings;
57 use Time::HiRes qw(time);
58 
59 my $default_unit = 1000; # milliseconds
60 
61 sub new {
62  my $class = shift @_;
63 
64  my $self = bless {}, $class;
65  return $self;
66 }
67 
68 sub _unit { # only set it once for each timer to avoid messing everything up
69  my $self = shift;
70 
71  $self->{'_unit'} = shift if(@_);
72  return $self->{'_unit'} || $default_unit;
73 }
74 
75 sub is_counting {
76  my $self = shift;
77 
78  $self->{'_is_counting'} = shift if(@_);
79  return $self->{'_is_counting'} || 0;
80 }
81 
82 sub accumulated {
83  my $self = shift;
84 
85  $self->{'_accumulated'} = shift if(@_);
86  return $self->{'_accumulated'} || 0;
87 }
88 
89 sub continue { # the opposite of pause()
90  my $self = shift @_;
91 
92  unless($self->is_counting) { # ignore if it was already running
93  $self->is_counting(1);
94  $self->{'_start'} = time() * $self->_unit
95  }
96 
97  return $self;
98 }
99 
100 sub restart {
101  my $self = shift @_;
102 
103  $self->accumulated(0);
104  return $self->continue;
105 }
106 
107 sub get_elapsed { # peek without stopping (in case it was running)
108  my $self = shift @_;
109 
110  return ($self->accumulated + $self->is_counting * (time() * $self->_unit - $self->{'_start'}));
111 }
112 
113 sub pause {
114  my $self = shift @_;
115 
116  $self->accumulated( $self->get_elapsed );
117  $self->is_counting(0);
118 
119  return $self;
120 }
121 
122 1;
Bio::EnsEMBL::Hive::Version
Definition: Version.pm:19
Bio::EnsEMBL::Hive::Utils::Stopwatch
Definition: Stopwatch.pm:33
Bio::EnsEMBL::Hive
Definition: Hive.pm:38
Bio::EnsEMBL::Hive::Utils::Stopwatch::new
public new()