seed kmig-init from mig-init
[migration-tools.git] / measurespeed.pl
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 measurespeed - program for calculating units per second between the current invocation
8 and the previous invocation
9
10 =head1 SYNOPSIS
11
12 B<measurespeed> [argument] [...]
13
14 =head1 DESCRIPTION
15
16 B<measurespeed> is used to measure the change in number of units over a span of time.
17
18 =head1 OVERVIEW
19
20 Using B<measurespeed> should go something like this:
21
22 =over 15
23
24 =item wc -l growing_file.txt | measurespeed --reset
25
26 =item wc -l growing_file.txt | measurespeed
27
28 =item wc -l growing_file.txt | measurespeed --max=10000 # we expect for the numbers being given to stop at 10000
29
30 =back
31
32 --reset (or --first) makes measurespeed forget about previous invocations.
33
34 measurespeed will track the elapsed time between invocations and difference the current number being fed to it with the previous number.
35
36 This data is stored in .measurespeed in the current working directory
37
38 --max (or --expect) will cause measurespeed to show a progressmeter in addition to its normal calculations
39
40 --debug shows extra information for sanity checking
41
42 =head1 EXAMPLE
43
44 =over 15
45
46 =item echo 1 | measurespeed --first
47
48 =item # 10 seconds elapse
49
50 =item echo 21 | measurespeed
51
52 =back
53
54 measurespeed will calculate that 20 units have occurred in 10 seconds, and report a speed of 2 units per second.
55
56 =cut
57
58 ###############################################################################
59
60 use strict;
61 use Pod::Usage;
62 use Getopt::Long;
63 use Date::Calc qw(Date_to_Time Today_and_Now Delta_DHMS Time_to_Date);
64 use Storable qw(store retrieve);
65
66 my $help;
67 my $reset;
68 my $debug;
69 my $max;
70
71 GetOptions(
72         'max|expect=s' => \$max,
73         'reset|first' => \$reset,
74     'debug' => \$debug,
75         'help|?' => \$help
76 );
77 pod2usage(-verbose => 2) if $help; 
78
79 my $persist = {};
80 if (!$reset) {
81     eval {
82         $persist = retrieve('.measurespeed');
83         print "Previous unit = $persist->{unit}\n" if $debug;
84         print "Previous time = $persist->{time}\n" if $debug;
85     };
86     warn $@ if $@;
87 }
88
89 my $current_unit;
90 my $line = <>;
91 if ($line =~ /(\d+)/) {
92     $current_unit = $1;
93     print "Current unit = $current_unit\n" if $debug;
94 }
95 pod2usage(-verbose => 2) unless defined $current_unit;
96
97 my $current_time = Date_to_Time(Today_and_Now());
98 print "Current time = $current_time\n" if $debug;
99
100 my $unit_delta;
101 my $time_delta;
102
103 if (!$reset && defined $persist->{time} && defined $persist->{unit}) {
104     if ($persist->{unit} <= $current_unit) {
105         $unit_delta = $current_unit - $persist->{unit};
106         $time_delta = $current_time - $persist->{time};
107         print "unit_delta = $unit_delta, time_delta = $time_delta\n" if $debug;
108         my $speed = $unit_delta / $time_delta;
109         print "$speed units per second\n";
110         if ($max) {
111             use Term::ProgressBar;
112              
113             my $progress = Term::ProgressBar->new ({count => $max});
114             $progress->update($current_unit);
115
116             my $eta = ($max - $current_unit) / $speed;
117             print "Estimated Time Remaining: $eta seconds\n";
118
119             use DateTime;
120             my $eta_date = DateTime->from_epoch(epoch => $current_time + $eta);
121             print "                          " . $eta_date->iso8601() . "\n";
122         }
123     } else {
124         print "Current unit is less than previous unit.  Implied --reset\n";
125     }
126 }
127
128 $persist->{unit} = $current_unit;
129 $persist->{time} = $current_time;
130 eval { store($persist, '.measurespeed'); print "Saved .measurespeed\n" if $debug; };
131 warn $@ if $@;
132