The following script shows:
- Current item (iteration) number
- Sliding window average rate (eg. average over 10 iterations)
- Overall average rate
- The percentage progressed so far (if the number of irrigation is known)
The output:
317: window: 3.76/s overall: 3.28/s progress: 31.7%
The test file:
set test_file
# create a test file
for i in {1..1000} ;do echo $i; done >"$1"
The script:
if="$1" # the input file
lct=$(wc -l <"$if") # number of lines in input file
tot=${lct:-0} # total number of itterations; If unknown, default is 0
#+ The total is know in this case. '$tot' is just a rough
#+ example of how to suppress the progress %age output
beg=$(date +%s.%N) # starting unix time.%N is nanoseconds (a GNU extension)
swx=10 # keep a sliding window of max 'n' itteratons (to average)
unset sw # an array of the last '$swx' rates
for i in $(seq 1 $lct) ;do
sw[$i]=$(date +%s.%N) # sliding window start time
# ================
sleep .$RANDOM # ... process something here
# ================
now=$(date +%s.%N) # current unix time
if ((i<=swx)) ;then
sw1=1 # first element of sliding window
sww=$i # sliding window width (starts from 1)
else
sw1=$((i-swx+1))
sww=$swx
fi
bc=($(bc <<<"scale=2; $i/($now-$beg); $sww/($now-${sw[$sw1]})"))
oavg=${bc[0]} # overall average rate
swhz=${bc[1]} # sliding window rate
((i>swx)) && unset sw[$sw1-1] # remove old entry from sliding window list
((tot==0)) && pc= || pc="progress: $(bc <<<"scale=1; x=($i*100)/$tot; if (x<1) print 0; x")%"
msg="$i: window: $swhz/s overall: $oavg/s $pc"
printf "\r%"$((${#i}+1))"s=\r%s" "" "$msg"
done
timecommand to get the execution time of the script, liketime ./script.sh. – ByteNudger Feb 25 '12 at 15:27