Tuesday, February 7, 2017

Bc as CPU benchmark - revisited

Recently I found little script I wrote that may be helpful benchmarking machines with bc:

if [ -n "$1" ]; then
   PRECISION="$1"
else
   PRECISION=10000
fi

CPUINFO="/proc/cpuinfo"
MEMINFO="/proc/meminfo"

echo "Linux shell CPU benchmark"
echo "------------------------------------"
echo "Usage: $0 "
echo

BCCMD=$(which bc)
if [ -z "$BCCMD" ]; then
        echo "bc command not found... exiting now"
        exit
fi

echo -n "Getting CPU data.... "
#get cpu data
if [ -r $CPUINFO ]; then
        VENDOR=$(cat "$CPUINFO" | grep vendor_id | head -n1 | cut -f2 -d':'| cut -c2- )
        MODEL=$(cat "$CPUINFO" | grep "model name" | head -n1 | cut -f2 -d':' | cut -c2- | sed -e 's/  */ /g')
        SPEED=$(cat "$CPUINFO" | grep "cpu MHz" | head -n1 | cut -f2 -d':' | cut -c2- )
        CACHE=$(cat "$CPUINFO" | grep "cache size" | head -n1 | cut -f2 -d':' | cut -c2-)
        LMFLAG=$(cat "$CPUINFO" | grep flags | head -n1 | cut -f2 -d':' | tr ' ' '\n' | grep lm)

        if [ "$LMFLAG" = "lm" ]; then
                CPU64="1"
        else
                CPU64="0"
        fi
        echo "[OK]"
else
        CPU64=""
        echo "[NOT FOUND]"
fi


echo -n "Getting memory data..."
#get memory data
if [ -r "$MEMINFO" ]; then
        MEMTOTAL=$(cat "$MEMINFO" | grep MemTotal | cut -f2 -d':' | cut -c2- | sed -e 's/ *//g')
        echo "[OK]"
else
        MEMTOTAL=""
        echo "[NOT FOUND]"
fi
echo -n "Getting machine, OS and Kernel data..."
#get machine,os,kernel data
UNAME_CMD=$(which uname)
if [ -n "$UNAME_CMD" ]; then
        #MACHINE=$($UNAME_CMD -m)
        OS=$($UNAME_CMD -o)
        KERNEL=$($UNAME_CMD -r)
        echo "[OK]"
else
        OS=""
        KERNEL=""
        echo "[NOT FOUND]"
fi

#get distro name
LSBRELEASE=$(which lsb_release 2>/dev/null)
if [ -n "$LSBRELEASE" ]; then
    DISTRO=$($LSBRELEASE -d | cut -f2 -d':' | tr -d '\t')
elif [ $(ls /etc/*version 2>/dev/null | wc -l) -gt 0 ]; then
    DISTRO=$(cat $(ls /etc/*version | head -n1))
elif [ $(ls /etc/*release 2>/dev/null | wc -l) -gt 0 ]; then
    DISTRO=$(cat $(ls /etc/*release | head -n1))
elif [ -f /proc/version ]; then
    DISTRO=$(cat /proc/version)
else
    echo "[NOT FOUND]"
    DISTRO=""
fi


echo -n "Benchmarking (precision=$PRECISION)..."
#benchmarking
if [ -n "$BCCMD" ]; then
  SCORE=$((time echo "scale=$PRECISION;a(1)*4" | $BCCMD -l) 2>&1 | grep real | sed -e 's/  */ /g' | tr '\t' ' ' | cut -f2 -d' ')
  #echo "SCORE#$SCORE"
  echo "[OK]"
else
  SCORE=""
  echo "[ERROR]"
fi

#print values
echo
echo "--- cut here ---"
echo "DISTRO#$DISTRO"
echo "OS#$OS"
echo "KERNEL#$KERNEL"
echo "VENDOR#$VENDOR"
echo "MODEL#$MODEL"
echo "SPEED#$SPEED"
echo "CACHE#$CACHE"
echo "CPU64#$CPU64"
echo "MEMTOTAL#$MEMTOTAL"
echo "PRECISION#$PRECISION"
echo "SCORE#$SCORE"
echo "--- cut here ---"

2 comments:

  1. Hey, Wow all the posts are very informative for the people who visit this site. Good work! We also have a Website. Please feel free to visit our site. Thank you for sharing.
    Be Your Own Boss! If you're looking for a change in your work prospects, then let's prepare for your career from here!!!
    Self Employment | Women Development | Information Technology | Engineering Courses

    ReplyDelete
  2. E-CAREERPLUZ INFO INDIA PRIVATE LIMITED.To learn all the self employment course here..

    ReplyDelete