[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: bash scripting question



Here's something I modified as part of a benchmark script called "fdtree".

-- 
Karl Vogel                      I don't speak for the USAF or my company
Dijkstra probably hates me.             --Linus Torvalds, in kernel/sched.c

#!/bin/bash
# How to use xdate/xtime/persec:
#
#  START=$(date "+%s")
#  count=10
#  xdate
#  tin=$(xtime)
#
#  # do something time-consuming $count times...
#  tout=$(xtime)
#  set $(persec $tin $tout $count); ttot=$1; results=$2
#  xdate
#  echo "TIME IN, OUT, TOTAL = "$tin, $tout, $ttot
#  echo -e "\tWork per second = " $results

PATH=/usr/local/bin:/bin:/usr/bin
export PATH

function xdate
# Display the date in this form: Thu, 10 Sep 2009 21:22:06.494
{
    set $(date "+%a, %d %b %Y %T %N")
    ms=$(echo $6 | cut -c1-3)
    echo "$1 $2 $3 $4 $5.$ms"
}

function xtime
# Display elapsed runtime to the millisecond.
{
    set $(date "+%s %N")
    sec=$(($1 - $START))
    ms=$(echo $2 | cut -c1-3)
    echo "$sec.$ms"
}

function persec
# args: start-second, finish-second, count-things
# returns elapsed time and things that happened per second
# to the millisecond.
{
    start=$1
    finish=$2
    count=$3

    if test "$finish" = "$start"; then
        echo "0 0"
    else
        echo $(echo "scale=3; $finish-$start;
            $count/($finish-$start)" | bc)
    fi
}

function dbg
# debugging prints
{
    test $DEBUG -gt 0 && echo -e "$@"
}

tmp=/tmp/t1$$
tmp2=/tmp/t2$$

xdate
START=$(date "+%s")
tin=$(xtime)
echo

for k in 1 2 3 4 5 6 7 8 9 10
do
    # Read a short amount of random data.
    dd if=/dev/random of=$tmp2 bs=1k count=1 2> /dev/null

    # Duplicate it a bunch of times.
    cat $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 > $tmp
    cat $tmp  $tmp  $tmp  $tmp  $tmp  $tmp  $tmp  $tmp  > $tmp2
    cat $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 > $tmp
    cat $tmp  $tmp  $tmp  $tmp  $tmp  $tmp  $tmp  $tmp  > $tmp2
    cat $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 $tmp2 > $tmp
    rm $tmp2

    # Generate a hash.
    md5sum $tmp
    ls -l $tmp
    rm $tmp
done

echo
count=10
tout=$(xtime)
set $(persec $tin $tout $count); ttot=$1; results=$2
xdate

echo "TIME IN, OUT, TOTAL = "$tin, $tout, $ttot
echo -e "\tWork per second = " $results
exit 0


Reply to: