xfertest.shon commit improve webp-convert.sh (b8ae487)
   1#!/bin/sh
   2#
   3# xfertest -- test transfer speeds of ftp, samba and rcp.
   4#
   5Program='Samba'
   6Verbose=0
   7Filesize=0
   8Debug=0
   9#JUNK='2>&1' # make output appear for debugging.
  10JUNK='>/dev/null 2>&1'
  11
  12
  13main() {
  14        while [ "$1" != "" ]; do
  15                case "$1" in
  16                -s)     Program='Samba';;
  17                -f)     Program='Ftp';;
  18                -r)     Program='Rcp';;
  19                -n)     Program='Nfs';;
  20                -v)     Verbose=1;;
  21                -d)     Debug=1; Verbose=1;;
  22                *)      break;;
  23                esac
  24                shift
  25        done
  26        if [ $# -lt 2 ]; then
  27                say "xfertest: you must provide a host and a test file."
  28                say "Usage: $0 [-sfr -v] host file"
  29                exit 1
  30        fi
  31
  32
  33        host="$1"
  34        file="$2"
  35
  36        if [ ! -f "$file" ]; then
  37                say "$0: $file not found, halting."
  38                exit 1
  39        else
  40                Filesize=`ls -l $file | awk '{print $5}'`
  41        fi
  42        cp $file /tmp
  43        cd /tmp
  44        $Program $host $file
  45}
  46
  47#
  48# Rcp -- time Remote CoPy
  49#
  50Rcp() {
  51        host="$1"
  52        file="$2"
  53
  54        if [ "$Verbose" -eq 1 ]; then
  55                say "benchmarking rcp on $host with file $file"
  56                say "getting $file 5 times"
  57        fi
  58        rcp $host:$file . # Prime the pump
  59        for i in 1 2 3 4 5; do
  60                time rcp $host:$file .
  61        done 2>&1 | timeStats "rcp get $Filesize"
  62
  63        if [ "$Verbose" -eq 1 ]; then
  64                say "putting $file 5 times"
  65        fi
  66        f=`basename $file`
  67        rcp $f $host:$file 
  68 
  69        for i in 1 2 3 4 5; do
  70                time rcp $f $host:$file
  71        done 2>&1 | timeStats "rcp put $Filesize"
  72}
  73
  74
  75
  76#
  77# Nfs -- time nfs via timing cp. "host" will be ignored.
  78#
  79Nfs() {
  80        host="$1"
  81        file="$2"
  82
  83        if [ "$file" != "" ]; then
  84                :
  85        else
  86                say "you must supply a host (which is then ingored)"
  87                exit 1
  88        fi
  89
  90        if [ "$Verbose" -eq 1 ]; then
  91                say "benchmarking nfs (cp) on $host with file $file"
  92                say "getting $file 5 times"
  93        fi
  94        cp $file . # Prime the pump
  95        for i in 1 2 3 4 5; do
  96                time cp $file ./junk
  97        done 2>&1 | timeStats "nfs get $Filesize"
  98
  99        if [ "$Verbose" -eq 1 ]; then
 100                say "putting $file 5 times"
 101        fi
 102        f=`basename $file`
 103        cp $f $file 
 104 
 105        for i in 1 2 3 4 5; do
 106                time cp $f $file
 107        done 2>&1 | timeStats "nfs put $Filesize"
 108}
 109
 110
 111#
 112# timeStats -- summarize stats from /bin/time, used for nfs/cp and rcp
 113#
 114timeStats() {
 115        nawk '
 116        BEGIN {
 117                avg=0.0;
 118        }
 119        /.*/ { 
 120                if (debug) {
 121                        print "#", $0; 
 122                }
 123        }
 124
 125        /real/ {
 126                avg += $2;
 127                if (verbose) {
 128                        print $2;
 129                }
 130                next;
 131        }
 132        /user|sys|^[    ]*$/ {
 133                next;
 134        }
 135        /.*/ {
 136                print $0;
 137        }
 138        END {
 139                if (verbose) {
 140                        print "#pg op  size time kb/sec";
 141                }
 142                if (avg == 0.0 ) {
 143                        printf("%s 0.0 N/A\n",description);
 144                }
 145                else {
 146                        time = avg / 5;
 147                        printf("%s %.2f %.2f\n",description,\
 148                                 time, (size/time) /1000);
 149                }
 150        }
 151' description="$1" verbose=$Verbose size=$Filesize debug=$Debug
 152}
 153
 154
 155#
 156# ftp -- time File Transfer Program
 157#
 158Ftp() {
 159        host="$1"
 160        file="$2"
 161
 162        if [ "$Verbose" -eq 1 ]; then
 163                say "benchmarking ftp on $host with file $file"
 164        fi
 165        say "Warning: this will temporarily overwrite your .netrc password"
 166        say "$0: enter password to use for ftp"
 167        read password
 168
 169        trap '' 1 2 3 4 5 6 7 8 9 # Disable ^C et all.
 170        if [ -f $HOME/.netrc ]; then
 171                mv $HOME/.netrc $HOME/saved.netrc
 172        fi
 173        cat >$HOME/.netrc <<!
 174machine $host
 175login `whoami`
 176password $password
 177!
 178        chmod 700 $HOME/.netrc
 179
 180        if [ "$Verbose" -eq 1 ]; then
 181                say "getting $file 5 times"
 182        fi
 183        # Prime the punp.
 184        ftp $host >/dev/null 2>&1 <<!
 185get $file junk
 186quit
 187!
 188
 189        # Do the test
 190        ftp -v $host >/tmp/$$ 2>&1 <<!
 191get $file junk
 192get $file junk
 193get $file junk
 194get $file junk
 195get $file junk
 196quit
 197!
 198        cat /tmp/$$ | timeFtp "ftp get $Filesize"
 199        rm /tmp/$$
 200
 201        if [ "$Verbose" -eq 1 ]; then
 202                say "putting $file 5 times"
 203        fi
 204        # Prime
 205        ftp $host >/dev/null 2>&1 <<!
 206put $file junk
 207quit
 208!
 209
 210        # Test
 211ftp -v $host >/tmp/$$ 2>&1 <<!
 212put $file junk
 213put $file junk
 214put $file junk
 215put $file junk
 216put $file junk
 217quit
 218!
 219        cat /tmp/$$ | timeFtp "ftp put $Filesize"
 220        rm /tmp/$$
 221
 222        rm $HOME/.netrc
 223        if [ -f $HOME/saved.netrc ]; then
 224                mv $HOME/saved.netrc $HOME/.netrc
 225        fi
 226
 227}
 228
 229#
 230# timeFtp 
 231#
 232timeFtp() {
 233
 234        nawk '
 235        BEGIN {
 236                avg = 0.0;
 237        }
 238        /.*/ { 
 239                if (debug) {
 240                        print "#", $0; 
 241                }
 242        }
 243        /.* bytes .* in/ {
 244                avg += $5
 245                if (verbose) {
 246                        print $0;
 247                }
 248                next;
 249        }
 250        /[0-9]*/ {
 251                next;
 252        }
 253        /.*/ {
 254                print $0;
 255        }
 256        END {
 257                if (verbose) {
 258                        print "#pg op  size time kb/sec";
 259                }
 260                if (avg == 0.0 ) {
 261                        printf("%s 0.0 N/A\n",description);
 262                }
 263                else {
 264                        time = avg / 5;
 265                        printf("%s %.2f %.2f\n",description, time, (size/time) /1000);
 266                }
 267        }
 268' description="$1" verbose=$Verbose size=$Filesize  debug=$Debug
 269}
 270
 271
 272#
 273# Samba -- time Smbclient get/put, "host" parameter must be a service,
 274#       "file" a file relative to it
 275#
 276Samba() {
 277        host="$1"
 278        file="$2"
 279
 280        if [ "$Verbose" -eq 1 ]; then
 281                say "benchmarking Samba's smbclient on $host with file $file"
 282        fi
 283        say "$0: enter password to use for samba"
 284        read password
 285
 286        trap '' 1 2 3 4 5 6 7 8 9 # Disable ^C et all.
 287        if [ "$Verbose" -eq 1 ]; then
 288                say "getting $file 5 times"
 289        fi
 290        smbclient $host $password  >/dev/null 2>&1 <<!
 291get $file junk
 292quit
 293!
 294        smbclient $host $password >/tmp/$$ 2>&1 <<!
 295get $file junk
 296get $file junk
 297get $file junk
 298get $file junk
 299get $file junk
 300quit
 301!
 302        cat /tmp/$$ | timeSamba "smb get $Filesize"
 303        rm /tmp/$$
 304
 305        if [ "$Verbose" -eq 1 ]; then
 306                say "putting $file 5 times"
 307        fi
 308        smbclient $host $password  >/dev/null 2>&1 <<!
 309put $file junk
 310quit
 311!
 312
 313        smbclient $host $password >/tmp/$$ 2>&1 <<!
 314put $file junk
 315put $file junk
 316put $file junk
 317put $file junk
 318put $file junk
 319quit
 320!
 321
 322        cat /tmp/$$ | timeSamba "smb put $Filesize"
 323        rm /tmp/$$
 324
 325}
 326
 327#
 328# timeSamba -- summarize stats from Samba
 329#
 330timeSamba() {
 331        nawk '
 332        BEGIN {
 333                avg=0.0;
 334        }
 335        /.*/ { 
 336                if (debug) {
 337                        print "#", $0; 
 338                }
 339        }
 340        /^\(/ {
 341                sub("[(]", "", $1);
 342                avg += size/($1*1000)
 343                if (verbose) {
 344                        print size/($1*1000), "sec, ", $1, "kb/sec";
 345                }
 346                next;
 347        }
 348        /^putting/ {
 349                sub("[(]", "", $10);
 350                avg += size/($10*1000)
 351                if (verbose) {
 352                        print size/($10*1000), "sec, ", $10, "kb/sec";
 353                }
 354                next;
 355        }               
 356        END {
 357                if (verbose) {
 358                        print "#pg op  size time kb/sec";
 359                }
 360                if (avg == 0.0 ) {
 361                        printf("%s 0.0 N/A\n",description);
 362                }
 363                else {
 364                        time = avg / 5;
 365                        printf("%s %.2f %.2f\n",description, time, (size/time) /1000);
 366                }
 367        }
 368' description="$1" verbose=$Verbose size=$Filesize  debug=$Debug
 369}
 370
 371
 372        
 373say() {
 374        echo "$*" 1>&2
 375}
 376
 377main "$@"
 378