Paste #271787

   
pasted on 25.12.2023 23:48
  • Edit to this paste
  • Print
  • Raw
  • Compare with paste
    #  
  • Toggle line numbers
  • Syntax highlighting  
Text paste
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/sh
# Silly speed test just by downloading a file directly to /dev/null
# Useful on routers which only have busybox installed.
# ↄ⃝🄯 B9 2016, 2018, 2023. Creative Commons Zero.

# NOTA BENE: cachefly has worked for over a decade, however they may
# be getting tired of scripts like this. Starting February 2023, their
# 100mb.test file is empty and the 50mb.test file holds 100MB.

dotest() {
    # $1 is which file to grab 200, 100, 50, 10, or 1 (megabytes).
    # $2 (optional) is how many megabytes that file actually is.
    filename=${1}
    megabytes=${2:-$1}

    wget=$(whichwget)

    bloburl=http://cachefly.cachefly.net/${filename}mb.test
    blobsize=$(($megabytes*1024*1024))

    if which time >/dev/null 2>/dev/null; then
	totaltime=$(dotime $wget "$bloburl" -O /dev/null)
    else
	start=$(date +%s)
	$wget "$bloburl" -O /dev/null 
	stop=$(date +%s)
	totaltime=$(( (stop-start) * 1000))
    fi 

    echo Total time was $totaltime milliseconds for $blobsize bytes

    if [ $totaltime -ne 0 ]; then
	bps=$(((1000*blobsize*8)/totaltime))
	echo $((bps/1024/1024)) Mbps
	echo $((bps/1024)) kbps
    else
	echo "Incalculable"
    fi
}

dotime() {
    # If we have the "time" program, use it for more precision
    # This is a wrapper since we want to return just
    # the number of milliseconds. Also, we handle different
    # formats for output from `time`. 
    output=$("time" "$@" 2>&1 | tee /dev/tty)

    if [ -z "$output" ]; then
	echo "Error: Is the time (`which time`) program broken?" >&2
	echo "Why was there no stderr?" >&2
	echo 0
	return
    fi

    # time's output usually looks like this:
    # Connecting to cachefly.cachefly.net (205.234.175.175:80)
    # null       100% |*******************************|  1024k  0:00:00 ETA
    # real    0m 0.22s
    # user    0m 0.00s
    # sys     0m 0.08s

    real=$(echo "$output" | grep ^real)

    # We expect real="real	0m 0.22s", but double check
    if [ "$real" ]; then
	# Yup, we got what we expected.
	set - $real
	min=$2			# 0m
	min=${min%m}		# 0
	sec=$3			# 0.22s
	sec=${sec%s}		# 0.22
    else
	# Oops. This must be one of those machines where `time` returns
	# 0.00user 0.00system 0:00.22elapsed 1%CPU (0avgtext+0avgdata 1284maxresident)k
	real=$(echo "$output" | grep elapsed)
	if [ -z "$real" ]; then
	    echo "Error: Unknown format from the 'time' program:" >&2
	    echo "$output" >&2
	    echo 0
	    return
	fi

	# ...tem 0:00.22elapsed 1%cpu...
	real=${real%%elapsed*}		# ...tem 0:00.22
	real=${real##* }		# 0:00.22
	min=${real%%:*}			# 0
	sec=${real##*:}			#   00.22
    fi

    # Now we've got min=0 and sec=0.22, get rid of the decimal point
    ms=${sec#*.}		# 22
    while [ ${#ms} -lt 3 ]; do	# 220
	ms=${ms}0
    done
    sec=${sec%.*}

    # Bourne arithmetic in $(...) treats leading 0 as octal!
    min=$(nooctal $min)
    sec=$(nooctal $sec)
    ms=$(nooctal $ms)

    echo "$min minutes, $sec seconds, $ms milliseconds" >&2

    # Now return the result as total number of milliseconds 
    echo $((min*60*1000 + sec*1000 + ms))
}

nooctal() {
    # Given a string in $1, print without leading zeros.
    # "008" --> "8"
    x=$1
    y=${x#0}
    while [ "$x" != "$y" ]; do
	x=$y
	y=${x#0}
    done

    if [ "$x" ]; then
	echo $x
    else
	echo 0
    fi
}

whichwget() {
    # On fancy machines, wget detects it is not on a tty and is too verbose.
    # On routers, wget doesn't understand the --progress flag.
    # Let's make everybody happy! :-)

    # Run a test and see how it dies. (255.255.255.255 always fails).

    wget --progress=bar:force  -t 1 -T 0.1  255.255.255.255 >/dev/null  2>&1
    case $? in
	127) echo "Sorry this test requires wget (busybox or regular)." >&2
	     ;;
	4) # Yay! Fancy wget.
	    options="--quiet --show-progress --progress=bar:force --report-speed=bits"
	    ;;
	1) # Still Yay! Busybox wget or a veritable simulacrum.
	    ;;
	*) # Huh... um... yay?
	    echo Error: The shell or wget gave a mysterious error code. >&2
	    echo Error: This ought never happen. >&2
	    ;;
    esac
    echo wget $options
}



echo "======================================================================"
echo "ROUGH APPROXIMATION"
dotest 1
echo "======================================================================"
echo "DOING QUICK TEST"
dotest 10
echo "======================================================================"
echo "DOING NORMAL TEST"
#dotest 100			# cachefly's "100" file is empty as of Feb 2023
dotest 50 100			# cachefly's "50" file is 100MB as of June 2023
#echo "======================================================================"
#echo "EXHAUSTIVE TEST"
#dotest 200
Add Comment
Author