NAME
Time::ETA - calculate estimated time of accomplishment
VERSION
version 0.02
SYNOPSIS
use Time::ETA;
my $eta = Time::ETA->new(
milestones => 12,
);
foreach (1..12) {
do_work();
$eta->pass_milestone();
print "Will work " . $eta->get_remaining_seconds() . " seconds more\n";
}
DESCRIPTION
You have a long lasting progress that consist of the number of more or less equal tasks. You need to calculate when the progress will finish. This module is designed to solve this task.
Time::ETA is designed to work with the programms that don't output anything to user console. This module is created to calculate ETA in cron scripts and background running programms. If you need an easy way to output process progress in terminal, please look at the exelent Term::ProgressBar.
To work with Time::ETA you need to create object with constructor new().
Then you run your tasks (just execute subs that containg the code of that tasks) and after each task you run pass_milestone() method to tell Time::ETA object that you have completed part of your process.
Any time in you programme you can use methods to understand what is going on and how soon the process will finish. That are methods is_completed(), get_completed_percent(), get_elapsed_seconds(), get_remaining_seconds().
This module has build-in feature for serialisation. You can run method serialize() to get the text string with the object state. And you can restore your object from that string with spawn() method.
METHODS
new
Get: 1) $class 2) %params
Return: 1) $self with Time::ETA object
This is the constructor. It needs one mandatory parameter - the number of milestones that should be completed.
Here is the example. Let's imagine that we are generating timetable for the next year. We have method generate_month() that is executed for every month. To create Time::ETA object that can calculate estimated time of timetable generation you need to write:
my $eta = Time::ETA->new(
milestones => 12,
);
get_elapsed_seconds
Get: 1) $self
Return: 1) $elapsed_seconds - float number
Method return number of seconds that have passed from object creation time.
print $eta->get_elapsed_seconds();
It can output something like 1.35024 and it means that a bit more than one second have passed from the moment the new() constructor has executed.
If the process is finished this method will return process run time in seconds.
get_remaining_seconds
Get: 1) $self
Return: 1) $elapsed_seconds - float number
Method return estimated seconds how long the process will work.
print $eta->get_remaining_seconds();
It can return number like 14.872352 that means that the process will end in nearly 15 seconds. The accuaccuracy of this time depends on the time lengths of every milestone. The more equal milestones time to each other, the more precise is the prediction.
This method will die in case it haven't got enough information to calculate estimated time of accomplishment. The method will die untill pass_milestone() is run for the first time. AFter pass_milestone() run at least once, get_remaining_seconds() has enouth data to caluate ETA. To find out if ETA can be calculated you can use method can_calculate_eta().
If the process is finished this method will return 0.
get_completed_percent
Get: 1) $self
Return: 1) $completed_percent - float number in the range from zero to 100 (including zero and 100)
Method returns the percentage of the process completion. It will return 0 if no milestones have been passed and it will return 100 if all the milestones have been passed.
$eta->get_completed_percent();
For example, if one milestone from 12 have been completed it will print 8.33333333333333
is_completed
Get: 1) $self
Return: 1) $boolean - true value if the process is completed or false value if the process is running.
You can also use get_completed_percent() to find our how much of the process is finished.
pass_milestone
Get: 1) $self
Return: it returns nothing that can be used
This method tells the object that one part of the task (called milestone) have been completed. You need to run this method as many times as many milestones you have specified in the object new() constructor.
$eta->pass_milestone();
You need to run this method at least once to make method get_remaining_seconds() work.
can_calculate_eta
Get: 1) $self
Return: $boolean
This method returns bool value that that gives information if there is enough data in the object to calculate process estimated time of accomplishment.
It will return true value if method pass_milestone() have been run at least once, if the method pass_milestone() haven't been run it will return false.
This method is used to check if it is safe to run method get_remaining_seconds(). Method get_remaining_seconds() dies in case there is no data to calculate ETA.
if ( $eta->can_calculate_eta() ) {
print $eta->get_remaining_seconds();
}
When the process is complete can_calculate_eta() returns true value, but get_remaining_seconds() return 0.
serialize
Get: 1) $self
Return: 1) $string with serialized object
Object Time::ETA has build-in serialaztion feature. For example you need to store the state of this object in the database. You can run:
my $string = $eta->serialize();
As a result you will get $string with text data that represents the whole object with its state. Then you can store that $string in the database and later with the method spawn() to recreate the object in the same state it was before the serialization.
spawn
Get: 1) $class 2) $string with serialized object
Return: 1) $self
This is actually an object constructor. It recieves $string that contaings serialized object data and creates an object.
my $eta = Time::ETA->spawn($string);
The $string is created by the method serialized().
SEE ALSO
- PBS::ProgressBar
- Progress::Any
- Progress::PV
- Term::ProgressBar::Quiet
- Term::ProgressBar::Simple
- Term::ProgressBar
- Text::ProgressBar::ETA
- Time::Progress
SOURCE CODE
The source code for this module and scripts is hosted on GitHub https://github.com/bessarabov/Time-ETA
BUGS
Please report any bugs or feature requests in GitHub Issues https://github.com/bessarabov/Time-ETA/issues
AUTHOR
Ivan Bessarabov <ivan@bessarabov.ru>
COPYRIGHT AND LICENSE
This software is copyright (c) 2013 by Ivan Bessarabov.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.