2 ###############################################################################
7 measurespeed - program for calculating units per second between the current invocation
8 and the previous invocation
12 B<measurespeed> [argument] [...]
16 B<measurespeed> is used to measure the change in number of units over a span of time.
20 Using B<measurespeed> should go something like this:
24 =item wc -l growing_file.txt | measurespeed --reset
26 =item wc -l growing_file.txt | measurespeed
28 =item wc -l growing_file.txt | measurespeed --max=10000 # we expect for the numbers being given to stop at 10000
32 --reset (or --first) makes measurespeed forget about previous invocations.
34 measurespeed will track the elapsed time between invocations and difference the current number being fed to it with the previous number.
36 This data is stored in .measurespeed in the current working directory
38 --max (or --expect) will cause measurespeed to show a progressmeter in addition to its normal calculations
40 --debug shows extra information for sanity checking
46 =item echo 1 | measurespeed --first
48 =item # 10 seconds elapse
50 =item echo 21 | measurespeed
54 measurespeed will calculate that 20 units have occurred in 10 seconds, and report a speed of 2 units per second.
58 ###############################################################################
63 use Date::Calc qw(Date_to_Time Today_and_Now Delta_DHMS Time_to_Date);
64 use Storable qw(store retrieve);
72 'max|expect=s' => \$max,
73 'reset|first' => \$reset,
77 pod2usage(-verbose => 2) if $help;
82 $persist = retrieve('.measurespeed');
83 print "Previous unit = $persist->{unit}\n" if $debug;
84 print "Previous time = $persist->{time}\n" if $debug;
91 if ($line =~ /(\d+)/) {
93 print "Current unit = $current_unit\n" if $debug;
95 pod2usage(-verbose => 2) unless defined $current_unit;
97 my $current_time = Date_to_Time(Today_and_Now());
98 print "Current time = $current_time\n" if $debug;
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";
111 use Term::ProgressBar;
113 my $progress = Term::ProgressBar->new ({count => $max});
114 $progress->update($current_unit);
116 my $eta = ($max - $current_unit) / $speed;
117 print "Estimated Time Remaining: $eta seconds\n";
120 my $eta_date = DateTime->from_epoch(epoch => $current_time + $eta);
121 print " " . $eta_date->iso8601() . "\n";
124 print "Current unit is less than previous unit. Implied --reset\n";
128 $persist->{unit} = $current_unit;
129 $persist->{time} = $current_time;
130 eval { store($persist, '.measurespeed'); print "Saved .measurespeed\n" if $debug; };