Time::ETA - calculate estimated time of accomplishment
version 0.01
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"; }
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 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.
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: 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.
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().
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
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.
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(); }
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.
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().
The source code for this module and scripts is hosted on GitHub https://github.com/bessarabov/Time-ETA
Please report any bugs or feature requests in GitHub Issues https://github.com/bessarabov/Time-ETA/issues
Ivan Bessarabov <ivan@bessarabov.ru>
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.
To install Time::ETA, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Time::ETA
CPAN shell
perl -MCPAN -e shell install Time::ETA
For more information on module installation, please visit the detailed CPAN module installation guide.