blob: 0264da6ca980849dfca4218eb4eafd4e2192773d [file] [log] [blame]
bartf33ce892008-06-07 11:40:14 +00001#!/bin/bash
2
3########################
4# Function definitions #
5########################
6
7## Print the logarithm base 2 of $1 on stdout.
8function log2 {
9 local i
10
11 for ((i=0;i<64;i++))
12 do
13 if [ $((2**i)) = $1 ]; then
14 echo $i
15 return 0
16 fi
17 done
18 echo error
19 return 1
20}
21
22## Print the size of the level 2 cache in bytes on stdout.
23function get_cache_size {
24 local s
25 s=$(</sys/devices/system/cpu/cpu0/cache/index2/size)
26 if [ "${s%M}" != "$s" ]; then
27 echo $((${s%M}*1024*1024))
28 elif [ "${s%K}" != "$s" ]; then
29 echo $((${s%K}*1024))
30 else
31 echo $s
32 fi
33}
34
35## Read a stream of numbers from stdin (one per line), and print the average
36# and standard deviation.
37function avgstddev {
38 awk '{n++;sum+=$1;sumsq+=$1*$1}END{d=sumsq/n-sum*sum/n/n;print sum/n,(d>0?sqrt(d):0)}'
39}
40
41## Echo all arguments on stderr, run the command passed in $1 .. ${$#} three
42# times, pass the file specified in ${test_input} to the command, write the
43# command output to the file specified in ${test_output}, and print the
44# runtime of the command on stdout.
45function measure_runtime {
46 echo "$@" >&2
47 for ((i=0;i<3;i++))
48 do
49 cat "${test_input:-/dev/null}" | \
50 /usr/bin/time --format="%e" "$@" 2>&1 | \
51 tee "${test_output:-/dev/null}" | \
52 tail -n 1
53 done
54}
55
56## Print the average runtime of the command passed in $1 .. ${$#} and the ratio
57# of the runtime to ${avg1} +/- ${stddev1}.
58function print_runtime_ratio {
59 local tmp
60
61 tmp="/tmp/test-timing.$$"
62 rm -f "${tmp}"
63
64 measure_runtime "$@" | avgstddev > "$tmp"
65 read avg2 stddev2 < "$tmp"
66 echo "Average time: ${avg2} +/- ${stddev2} seconds"
67 awk "END{print "'"'"Ratio ="'"'", ${avg2}/${avg1}, "'"'"+/-"'"'", ${avg2}/${avg1}*(${stddev1}/${avg1}+${stddev2}/${avg2})}" </dev/null
68}
69