Stopwatch.pm 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

=pod 

=head1 NAME

    Bio::EnsEMBL::Hive::Utils::Stopwatch

=head1 SYNOPSIS


    my $total_stopwatch    = Bio::EnsEMBL::Hive::Utils::Stopwatch->new()->restart;
    my $fetching_stopwatch = Bio::EnsEMBL::Hive::Utils::Stopwatch->new();
    my $running_stopwatch  = Bio::EnsEMBL::Hive::Utils::Stopwatch->new();

    $fetching_stopwatch->continue();
    $runnable->fetch_input();
    $fetching_stopwatch->pause();
    
    $running_stopwatch->continue();
    $runnable->run();
    $running_stopwatch->pause();

    # ...

    my $only_fetches       = $fetching_stopwatch->get_elapsed;    # probably stopped
    my $total_time_elapsed = $total_stopwatch->get_elapsed;       # running through

=head1 DESCRIPTION

    This is a standalone object used to time various events in the Hive.

32 33
=head1 LICENSE

34 35
    Copyright [1999-2015] Wellcome Trust Sanger Institute and the EMBL-European Bioinformatics Institute
    Copyright [2016] EMBL-European Bioinformatics Institute
36 37 38 39 40 41 42 43 44 45

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

         http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License
    is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and limitations under the License.

46 47
=head1 CONTACT

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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

=cut


package Bio::EnsEMBL::Hive::Utils::Stopwatch;

use strict;
use Time::HiRes qw(time);

my $default_unit = 1000;    # milliseconds

sub new {
    my $class = shift @_;

    my $self = bless {}, $class;
    return $self;
}

67
sub _unit {                 # only set it once for each timer to avoid messing everything up
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
    my $self = shift;

    $self->{'_unit'} = shift if(@_);
    return $self->{'_unit'} || $default_unit;
}

sub is_counting {
    my $self = shift;

    $self->{'_is_counting'} = shift if(@_);
    return $self->{'_is_counting'} || 0;
}

sub accumulated {
    my $self = shift;

    $self->{'_accumulated'} = shift if(@_);
    return $self->{'_accumulated'} || 0;
}

88
sub continue {              # the opposite of pause()
89 90 91 92 93 94
    my $self = shift @_;

    unless($self->is_counting) {    # ignore if it was already running
        $self->is_counting(1);
        $self->{'_start'} = time() * $self->_unit
    }
95 96

    return $self;
97 98 99 100 101 102
}

sub restart {
    my $self = shift @_;

    $self->accumulated(0);
103
    return $self->continue;
104 105
}

106
sub get_elapsed {           # peek without stopping (in case it was running)
107 108 109 110 111 112 113 114 115 116
    my $self = shift @_;

    return ($self->accumulated + $self->is_counting * (time() * $self->_unit - $self->{'_start'}));
}

sub pause {
    my $self = shift @_;

    $self->accumulated( $self->get_elapsed );
    $self->is_counting(0);
117 118

    return $self;
119 120 121
}

1;