LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Blogs > sumeet inani
User Name
Password

Notices



Rate this Entry

calculating write speed

Posted 07-05-2013 at 09:22 AM by sumeet inani
Updated 07-05-2013 at 09:23 AM by sumeet inani (clarity)

Hi ,
I wrote this script for any normal distro
This calculates data written in 10 seconds to get average.
Also monitors copying process , if possible , to know when to stop monitoring
Code:
#!/usr/bin/bash
monitor_drive=sdb1
number_of_argument=$#
echo PID is $processid
if [ "$1" = "--help" ]
	then
	echo USAGE
	echo ./general \<device_name\> \<PID\>
	echo mention PID as \'no\' \(without quotes , obviously\) if copying via GUI
	exit
fi
if [ "$number_of_argument" -gt 0 ]
	then
	echo defaults overridden
	monitor_drive=$1
fi
echo we-will-monitor-$monitor_drive
mountpoint=$(df | grep -i $monitor_drive | awk '{ print $NF }')
echo whose mount point is $mountpoint
processid=$(ps aux | grep -i cp | grep -i $mountpoint | awk '{print $2}')
if [ "$number_of_argument" -gt 1 ]
	then
	processid=$2
fi
while true
do
	tzero=$(df | grep -i $monitor_drive | awk '{print $3}')
	#echo  tzero is $tzero
	sleep 10
	tten=$(df | grep -i $monitor_drive | awk '{print $3}')
	#echo  tten is $tten
	diff=$((tten-tzero))
	#echo difference is $diff
	mbps=$((diff/10240))
	kbps=$((diff/10))
	echo $kbps KBps , $mbps MBps
	if [ "$2" != "no" ]
		then
		kill -0 $processid 2> /dev/null
		if [ "$?" -eq 1 ]
			then
			echo cp process complete/killed . Bye .
			exit
		fi
	fi
done
I tried with device sdb1 which is kept as default
Posted in Uncategorized
Views 4427 Comments 4
« Prev     Main     Next »
Total Comments 4

Comments

  1. Old Comment
    I am going to add time elapsed & average writing speed using concept from
    Code:
    #!/bin/bash
    count=1
    sum=0
    for i in 1 2 3 4 5
    do
       echo "Welcome $i times"
    sum=$(echo "$sum+$i"|bc -l)
    avg=$(echo "scale=2;$sum/$i"|bc -l)
    echo sum so far is $sum
    echo avg  so far is $avg
    done
    if [ -z "$SECONDS" ]; then echo "SECONDS variable not present so cannot report time elapsed"; else echo "time elapsed is '$SECONDS'"; fi
    also I will try formatting using http://stackoverflow.com/questions/6...n-neat-columns
    Posted 07-17-2013 at 08:11 AM by sumeet inani sumeet inani is offline
    Updated 07-17-2013 at 08:15 AM by sumeet inani
  2. Old Comment
    here is updated one which gets rid of finding pid of process as copying process is started inside script
    Code:
    #!/bin/bash
    	counter=0
    	sum=0
    	which bc 2> /dev/null
    	if [ "$?" -eq 1 ]
    	then
    		mode="simple"
    	else
    		mode="accurate"
    	fi	
    echo we began at $(date)
    monitor_drive=sdb1
    number_of_argument=$#
    if [ "$1" = "--help" ]
    	then
    	echo USAGE
    	echo "./general source-dir target-dir <sd##>"
    	we assume sdb1
    	exit
    fi
    cp -r $1 $2 &
    if [ "$number_of_argument" -gt 2 ]
    	then
    	echo defaults overridden
    	monitor_drive=$3
    fi
    echo we-will-monitor-$monitor_drive
    mountpoint=$(df | grep -i $monitor_drive | awk '{ print $NF }')
    echo whose mount point is $mountpoint
    processid=$!
    echo PID is $processid
    echo format is data-rate of last 10 seconds in KBps , MBps , average so far in KBps
    while true
    do
    	tzero=$(df | grep -i $monitor_drive | awk '{print $3}')
    	#echo  tzero is $tzero
    	sleep 10
    	tten=$(df | grep -i $monitor_drive | awk '{print $3}')
    	#echo  tten is $tten
    	diff=$((tten-tzero))
    	counter=$(($counter + 1))
    	#echo difference is $diff
    	if [ "$mode" = "simple" ]
    		then
    		mbps=$((diff/10240))
    		kbps=$((diff/10))
    		sum=$(($sum+$kbps))
    		avg=$(($sum/$counter))
    		echo -e "$kbps KBps , $mbps MBps\t$avg KBps"
    	else
    		mbps=$(echo "scale=2;$diff/10240"|bc -l)
    		kbps=$(echo "scale=0;$diff/10"|bc -l)
    		sum=$(echo "$sum+$kbps" | bc -l)
    		avg=$(echo "scale=0;$sum/$counter"|bc -l)
    #		echo -e "$kbps KBps , $mbps MBps\t$avg KBps"
    #		awk -v kbps=$kbps -v mbps=$mbps avg=$avg '{ printf "%-10s %-10s %-20s\n", kbps, mbps, avg}'
    		echo "$kbps $mbps $avg" | awk '{ printf "%-10s %-10s %-10s\n", $1, $2, $3}'
    	fi
    	if [ "$2" != "no" ]
    		then
    		kill -0 $processid 2> /dev/null
    		if [ "$?" -eq 1 ]
    			then
    			echo cp process complete/killed . Bye .
    			exit
    		fi
    	fi
    done
    echo we end at $(date)
    if [ -z "$SECONDS" ]; then echo "SECONDS variable not present so cannot report time elapsed"; else echo "time elapsed is '$SECONDS'"; fi
    if anybody interested then run
    ./script-name.sh source-folder destination-folder sd##
    sd##=the device where file is being copied(sdb1 default)


    THE DRAWBACK
    it expects folder-names without backslashes i.e alphanumeric only because $1 & $2 are space separated arguments.

    Can you help me solve this ?
    Posted 07-19-2013 at 08:51 AM by sumeet inani sumeet inani is offline
    Updated 07-19-2013 at 08:54 AM by sumeet inani
  3. Old Comment
    Here is my latest try that works
    EXCEPT
    (1)wild card * asterisk not allowed in source folder
    (2)also you have to mention device name in target - nothing else
    (3)check the locaion of bash before running script
    Code:
    #!/usr/bin/bash
    	counter=0
    	sum=0
    	which bc 2> /dev/null
    	if [ "$?" -eq 1 ]
    	then
    		mode="simple"
    	else
    		mode="accurate"
    	fi	
    echo we began at $(date)
    monitor_drive=sdb1
    number_of_argument=$#
    if [ "$1" = "--help" ]
    	then
    	echo USAGE
    	echo "./smartcopy.sh source-dir <sd##>"
    	we assume sdb1
    	exit
    fi
    full_command=$@
    #echo $full_command
    if [ "$number_of_argument" -gt 2 ]
    	then
    	echo defaults overridden
    #	monitor_drive=$3
    	monitor_drive="${@: -1}"
    fi
    echo we-will-monitor-$monitor_drive
    mountpoint=$(df | grep -i $monitor_drive | awk '{ print $NF }')
    echo whose mount point is $mountpoint
    source_dest=$(echo $full_command | sed 's/ [[:alnum:]]*$//')
    echo we will run cp -r \"$source_dest\" $mountpoint
    cp -r "$source_dest" $mountpoint &
    processid=$!
    echo PID is $processid
    echo format is data-rate of last 10 seconds in KBps , MBps , average so far in KBps
    while true
    do
    	tzero=$(df | grep -i $monitor_drive | awk '{print $3}')
    	#echo  tzero is $tzero
    	sleep 10
    	tten=$(df | grep -i $monitor_drive | awk '{print $3}')
    	#echo  tten is $tten
    	diff=$((tten-tzero))
    	counter=$(($counter + 1))
    	#echo difference is $diff
    	if [ "$mode" = "simple" ]
    		then
    		mbps=$((diff/10240))
    		kbps=$((diff/10))
    		sum=$(($sum+$kbps))
    		avg=$(($sum/$counter))
    		echo -e "$kbps KBps , $mbps MBps\t$avg KBps"
    	else
    		mbps=$(echo "scale=2;$diff/10240"|bc -l)
    		kbps=$(echo "scale=0;$diff/10"|bc -l)
    		sum=$(echo "$sum+$kbps" | bc -l)
    		avg=$(echo "scale=0;$sum/$counter"|bc -l)
    #		echo -e "$kbps KBps , $mbps MBps\t$avg KBps"
    #		awk -v kbps=$kbps -v mbps=$mbps avg=$avg '{ printf "%-10s %-10s %-20s\n", kbps, mbps, avg}'
    		echo "$kbps $mbps $avg" | awk '{ printf "%-10s %-10s %-10s\n", $1, $2, $3}'
    	fi
    	if [ "$2" != "no" ]
    		then
    		kill -0 $processid 2> /dev/null
    		if [ "$?" -eq 1 ]
    			then
    			echo cp process complete/killed . Bye .
    			exit
    		fi
    	fi
    done
    echo we end at $(date)
    if [ -z "$SECONDS" ]; then echo "SECONDS variable not present so cannot report time elapsed"; else echo "time elapsed is '$SECONDS'"; fi
    Posted 07-25-2013 at 08:57 AM by sumeet inani sumeet inani is offline
  4. Old Comment
    Try this & report its shortcomings , if you can ?
    Posted 07-25-2013 at 08:58 AM by sumeet inani sumeet inani is offline
 

  



All times are GMT -5. The time now is 06:54 AM.

Main Menu
Advertisement

Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration