| 1 | #!/usr/bin/env bash
|
| 2 | #
|
| 3 | # Usage:
|
| 4 | # benchmarks/time-test.sh <function name>
|
| 5 |
|
| 6 | : ${LIB_OSH=stdlib/osh}
|
| 7 | source $LIB_OSH/bash-strict.sh
|
| 8 | source $LIB_OSH/no-quotes.sh
|
| 9 |
|
| 10 | REPO_ROOT=$(cd "$(dirname $0)/.."; pwd)
|
| 11 |
|
| 12 | source test/common.sh
|
| 13 | source test/tsv-lib.sh
|
| 14 |
|
| 15 | # TODO: This would be a nice little program for Oil
|
| 16 | count-lines-and-cols() {
|
| 17 | python2 -c '
|
| 18 | import sys
|
| 19 |
|
| 20 | expected_num_lines = int(sys.argv[1])
|
| 21 | expected_num_cols = int(sys.argv[2])
|
| 22 | try:
|
| 23 | sep = sys.argv[3]
|
| 24 | except IndexError:
|
| 25 | sep = "\t"
|
| 26 |
|
| 27 | num_lines = 0
|
| 28 | tab_counts = []
|
| 29 | for line in sys.stdin:
|
| 30 | tab_counts.append(line.count(sep))
|
| 31 | num_lines += 1
|
| 32 | # Show what we get
|
| 33 | sys.stdout.write(line)
|
| 34 |
|
| 35 | if any(tab_counts[0] != n for n in tab_counts):
|
| 36 | raise AssertionError(tab_counts)
|
| 37 |
|
| 38 | num_tabs = tab_counts[0]
|
| 39 |
|
| 40 | assert expected_num_lines == num_lines, \
|
| 41 | "expected %d lines, got %d" % (expected_num_lines, num_lines)
|
| 42 | assert expected_num_cols == num_tabs + 1, \
|
| 43 | "expected %d cols, got %d" % (expected_num_cols, num_tabs + 1)
|
| 44 | ' "$@"
|
| 45 | }
|
| 46 |
|
| 47 | time-tool() {
|
| 48 | $(dirname $0)/time_.py "$@"
|
| 49 | }
|
| 50 |
|
| 51 | test-csv() {
|
| 52 | local out=_tmp/time.csv
|
| 53 |
|
| 54 | time-tool -o $out -- echo hi
|
| 55 | cat $out | count-lines-and-cols 1 2 ,
|
| 56 |
|
| 57 | time-tool -o $out --field a --field b -- echo hi
|
| 58 | cat $out | count-lines-and-cols 1 4 ,
|
| 59 | echo csv fields=$?
|
| 60 |
|
| 61 | time-tool -o $out --rusage -- echo hi
|
| 62 | cat $out | count-lines-and-cols 1 5 ,
|
| 63 | echo csv rusage=$?
|
| 64 |
|
| 65 | time-tool -o $out --rusage --field a --field b -- echo hi
|
| 66 | cat $out | count-lines-and-cols 1 7 ,
|
| 67 | echo csv rusage fields=$?
|
| 68 | }
|
| 69 |
|
| 70 | test-tsv() {
|
| 71 | local out=_tmp/time.tsv
|
| 72 | rm -f $out
|
| 73 |
|
| 74 | for i in 1 2 3; do
|
| 75 | time-tool --tsv -o $out --append --time-fmt '%.2f' -- sleep 0.0${i}
|
| 76 | done
|
| 77 | cat $out | count-lines-and-cols 3 2
|
| 78 |
|
| 79 | time-tool --tsv -o $out --field a --field b -- echo hi
|
| 80 | cat $out | count-lines-and-cols 1 4
|
| 81 | echo fields=$?
|
| 82 |
|
| 83 | time-tool --tsv -o $out --rusage --field a --field b -- echo hi
|
| 84 | cat $out | count-lines-and-cols 1 7
|
| 85 | echo rusage=$?
|
| 86 |
|
| 87 | time-tool --tsv -o $out --print-header \
|
| 88 | --rusage-2
|
| 89 | time-tool --tsv -o $out --append \
|
| 90 | --rusage-2 -- echo hi
|
| 91 | cat $out | count-lines-and-cols 2 10
|
| 92 | echo rusage-2=$?
|
| 93 | }
|
| 94 |
|
| 95 | test-append() {
|
| 96 | local out=_tmp/overwrite.tsv
|
| 97 | for i in 4 5; do
|
| 98 | time-tool --tsv -o $out -- sleep 0.0${i}
|
| 99 | done
|
| 100 | cat $out | count-lines-and-cols 1 2
|
| 101 |
|
| 102 | echo ---
|
| 103 |
|
| 104 | local out=_tmp/append.tsv
|
| 105 | rm -f $out
|
| 106 |
|
| 107 | for i in 4 5; do
|
| 108 | time-tool --tsv -o $out --append -- sleep 0.0${i}
|
| 109 | done
|
| 110 | cat $out | count-lines-and-cols 2 2
|
| 111 | }
|
| 112 |
|
| 113 | test-usage() {
|
| 114 | local status
|
| 115 | nq-run status \
|
| 116 | time-tool
|
| 117 | nq-assert $status -eq 2
|
| 118 |
|
| 119 | nq-run status \
|
| 120 | time-tool --output
|
| 121 | nq-assert $status -eq 2
|
| 122 |
|
| 123 | nq-run status \
|
| 124 | time-tool sleep 0.1
|
| 125 | nq-assert $status -eq 0
|
| 126 |
|
| 127 | nq-run status \
|
| 128 | time-tool --append sleep 0.1
|
| 129 | nq-assert $status -eq 0
|
| 130 | }
|
| 131 |
|
| 132 | test-bad-tsv-chars() {
|
| 133 | local status
|
| 134 | local out=_tmp/time2.tsv
|
| 135 | rm -f $out
|
| 136 |
|
| 137 | # Newline should fail
|
| 138 | nq-run status \
|
| 139 | time-tool --tsv -o $out --field $'\n' -- sleep 0.001
|
| 140 | nq-assert $status = 1
|
| 141 |
|
| 142 | # Tab should fail
|
| 143 | nq-run status \
|
| 144 | time-tool --tsv -o $out --field $'\t' -- sleep 0.001
|
| 145 | nq-assert $status = 1
|
| 146 |
|
| 147 | # Quote should fail
|
| 148 | nq-run status \
|
| 149 | time-tool --tsv -o $out --field '"' -- sleep 0.001
|
| 150 | nq-assert $status = 1
|
| 151 |
|
| 152 | # Backslash is OK
|
| 153 | nq-run status \
|
| 154 | time-tool --tsv -o $out --field '\' -- sleep 0.001
|
| 155 | nq-assert $status = 0
|
| 156 |
|
| 157 | # Space is OK, although canonical form would be " "
|
| 158 | nq-run status \
|
| 159 | time-tool --tsv -o $out --field ' ' -- sleep 0.001
|
| 160 | nq-assert $status = 0
|
| 161 |
|
| 162 | cat $out
|
| 163 | }
|
| 164 |
|
| 165 | test-stdout() {
|
| 166 | local out=_tmp/time-stdout.csv
|
| 167 | time-tool -o $out --stdout _tmp/stdout.txt -- seq 3
|
| 168 |
|
| 169 | diff _tmp/stdout.txt - <<EOF
|
| 170 | 1
|
| 171 | 2
|
| 172 | 3
|
| 173 | EOF
|
| 174 |
|
| 175 | # No assertions here yet
|
| 176 | md5sum _tmp/stdout.txt
|
| 177 | cat $out | count-lines-and-cols 1 3 ,
|
| 178 |
|
| 179 | time-tool -o $out --rusage --stdout _tmp/stdout.txt -- seq 3
|
| 180 | cat $out | count-lines-and-cols 1 6 ,
|
| 181 | }
|
| 182 |
|
| 183 | test-rusage() {
|
| 184 | local out=_tmp/time-rusage.csv
|
| 185 | time-tool --tsv -o $out --rusage -- bash -c 'echo bash'
|
| 186 | cat $out | count-lines-and-cols 1 5
|
| 187 |
|
| 188 | #time-tool --tsv -o $out --rusage -- dash -c 'echo dash'
|
| 189 | #cat $out
|
| 190 |
|
| 191 | # Blow up memory size for testing
|
| 192 | local py='a=[42]*500000; print "python"'
|
| 193 |
|
| 194 | time-tool --tsv -o $out --rusage -- python2 -c "$py"
|
| 195 | cat $out | count-lines-and-cols 1 5
|
| 196 |
|
| 197 | #time-tool --tsv -o $out --rusage -- bin/osh -c 'echo osh'
|
| 198 | #cat $out
|
| 199 | }
|
| 200 |
|
| 201 | test-time-span() {
|
| 202 | local out=_tmp/time-span.csv
|
| 203 |
|
| 204 | time-tool --tsv -o $out --time-span --print-header
|
| 205 | cat $out | count-lines-and-cols 1 4
|
| 206 |
|
| 207 | time-tool --tsv -o $out --time-span -- bash -c 'echo bash'
|
| 208 | cat $out | count-lines-and-cols 1 4
|
| 209 | }
|
| 210 |
|
| 211 | # Compare vs. /usr/bin/time.
|
| 212 | test-maxrss() {
|
| 213 | if which time; then # Ignore this on continuous build
|
| 214 | command time --format '%x %U %M' -- seq 1
|
| 215 | fi
|
| 216 |
|
| 217 | # Showing a discrepancy. FIXED!
|
| 218 | time-tool -o _tmp/maxrss --tsv --rusage -- seq 1
|
| 219 | cat _tmp/maxrss
|
| 220 | }
|
| 221 |
|
| 222 | test-print-header() {
|
| 223 | local status
|
| 224 |
|
| 225 | # no arguments allowed
|
| 226 | nq-run status \
|
| 227 | time-tool --tsv --print-header foo bar
|
| 228 | nq-assert $status = 2
|
| 229 |
|
| 230 | nq-run status \
|
| 231 | time-tool --tsv --print-header --field name
|
| 232 | nq-assert $status = 0
|
| 233 |
|
| 234 | nq-run status \
|
| 235 | time-tool --tsv --print-header --rusage --field name
|
| 236 | nq-assert $status = 0
|
| 237 |
|
| 238 | nq-run status \
|
| 239 | time-tool --print-header --rusage --field foo --field bar
|
| 240 | nq-assert $status = 0
|
| 241 |
|
| 242 | nq-run status \
|
| 243 | time-tool -o _tmp/time-test-1 \
|
| 244 | --print-header --rusage --stdout DUMMY --tsv --field a --field b
|
| 245 | nq-assert $status = 0
|
| 246 |
|
| 247 | head _tmp/time-test-1
|
| 248 |
|
| 249 | echo OK
|
| 250 | }
|
| 251 |
|
| 252 | test-time-helper() {
|
| 253 | local status
|
| 254 | local tmp=_tmp/time-helper.txt
|
| 255 | local th=_devbuild/bin/time-helper
|
| 256 |
|
| 257 | # Make some work show up
|
| 258 | local cmd='{ md5sum */*.md; sleep 0.15; exit 42; } > /dev/null'
|
| 259 |
|
| 260 | echo 'will be overwritten' > $tmp
|
| 261 | cat $tmp
|
| 262 |
|
| 263 | nq-run status \
|
| 264 | $th
|
| 265 | nq-assert $status != 0 # it's 1, but could be 2
|
| 266 |
|
| 267 | nq-run status \
|
| 268 | $th /bad
|
| 269 | nq-assert $status = 1
|
| 270 |
|
| 271 | nq-run status \
|
| 272 | $th -o $tmp -d $'\t' -x -e -- sh -c "$cmd"
|
| 273 | nq-assert $status = 42
|
| 274 | cat $tmp
|
| 275 | echo
|
| 276 |
|
| 277 | # Now append
|
| 278 |
|
| 279 | nq-run status \
|
| 280 | $th -o $tmp -a -d , -x -e -U -S -M -- sh -c "$cmd"
|
| 281 | nq-assert $status = 42
|
| 282 | cat $tmp
|
| 283 | echo
|
| 284 |
|
| 285 | # Error case
|
| 286 | nq-run status \
|
| 287 | $th -q
|
| 288 | nq-assert $status -eq 2
|
| 289 | }
|
| 290 |
|
| 291 | test-time-tsv() {
|
| 292 | local status
|
| 293 |
|
| 294 | local out=_tmp/time-test-zz
|
| 295 | rm -f -v $out
|
| 296 |
|
| 297 | # Similar to what soil/worker.sh does
|
| 298 | nq-run status \
|
| 299 | time-tsv -o $out --append -- zz
|
| 300 | nq-assert $status -eq 1
|
| 301 |
|
| 302 | cat $out
|
| 303 | echo
|
| 304 | }
|
| 305 |
|
| 306 | test-grandchild-memory() {
|
| 307 | local -a use_mem=( python2 -c 'import sys; ["X" * int(sys.argv[1])]' 10000000 )
|
| 308 |
|
| 309 | time-tsv -o /dev/stdout --rusage -- "${use_mem[@]}"
|
| 310 |
|
| 311 | # RUSAGE_CHILDREN includes grandchildren!
|
| 312 | time-tsv -o /dev/stdout --rusage -- sh -c 'echo; "$@"' dummy "${use_mem[@]}"
|
| 313 |
|
| 314 | # 'exec' doesn't make a consistent difference, because /bin/sh doesn't use
|
| 315 | # much memory
|
| 316 | time-tsv -o /dev/stdout --rusage -- sh -c 'echo; exec "$@"' dummy "${use_mem[@]}"
|
| 317 | }
|
| 318 |
|
| 319 | soil-run() {
|
| 320 | run-test-funcs
|
| 321 | }
|
| 322 |
|
| 323 | "$@"
|