1 | #!/usr/bin/env bash
|
2 | #
|
3 | # Usage:
|
4 | # benchmarks/time-test.sh <function name>
|
5 |
|
6 | set -o nounset
|
7 | set -o pipefail
|
8 | set -o errexit
|
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 |
|
60 | time-tool -o $out --rusage -- echo hi
|
61 | cat $out | count-lines-and-cols 1 5 ,
|
62 |
|
63 | time-tool -o $out --rusage --field a --field b -- echo hi
|
64 | cat $out | count-lines-and-cols 1 7 ,
|
65 | }
|
66 |
|
67 | test-tsv() {
|
68 | local out=_tmp/time.tsv
|
69 | rm -f $out
|
70 |
|
71 | for i in 1 2 3; do
|
72 | time-tool --tsv -o $out --append --time-fmt '%.2f' -- sleep 0.0${i}
|
73 | done
|
74 | cat $out | count-lines-and-cols 3 2
|
75 |
|
76 | time-tool --tsv -o $out --field a --field b -- echo hi
|
77 | cat $out | count-lines-and-cols 1 4
|
78 |
|
79 | time-tool --tsv -o $out --rusage --field a --field b -- echo hi
|
80 | cat $out | count-lines-and-cols 1 7
|
81 | }
|
82 |
|
83 | test-append() {
|
84 | local out=_tmp/overwrite.tsv
|
85 | for i in 4 5; do
|
86 | time-tool --tsv -o $out -- sleep 0.0${i}
|
87 | done
|
88 | cat $out | count-lines-and-cols 1 2
|
89 |
|
90 | echo ---
|
91 |
|
92 | local out=_tmp/append.tsv
|
93 | rm -f $out
|
94 |
|
95 | for i in 4 5; do
|
96 | time-tool --tsv -o $out --append -- sleep 0.0${i}
|
97 | done
|
98 | cat $out | count-lines-and-cols 2 2
|
99 | }
|
100 |
|
101 | test-usage() {
|
102 | # no args
|
103 | set +o errexit
|
104 |
|
105 | time-tool; status=$?
|
106 | assert $status -eq 2
|
107 |
|
108 | time-tool --output; status=$?
|
109 | assert $status -eq 2
|
110 |
|
111 | time-tool sleep 0.1
|
112 | time-tool --append sleep 0.1; status=$?
|
113 | assert $status -eq 0
|
114 |
|
115 | set -o errexit
|
116 | }
|
117 |
|
118 | test-bad-tsv-chars() {
|
119 | local out=_tmp/time2.tsv
|
120 | rm -f $out
|
121 |
|
122 | set +o errexit
|
123 |
|
124 | # Newline should fail
|
125 | time-tool --tsv -o $out --field $'\n' -- sleep 0.001; status=$?
|
126 | assert $status -eq 1
|
127 |
|
128 | # Tab should fail
|
129 | time-tool --tsv -o $out --field $'\t' -- sleep 0.001; status=$?
|
130 | assert $status -eq 1
|
131 |
|
132 | # Quote should fail
|
133 | time-tool --tsv -o $out --field '"' -- sleep 0.001; status=$?
|
134 | assert $status -eq 1
|
135 |
|
136 | # Backslash is OK
|
137 | time-tool --tsv -o $out --field '\' -- sleep 0.001; status=$?
|
138 | assert $status -eq 0
|
139 |
|
140 | # Space is OK, although canonical form would be " "
|
141 | time-tool --tsv -o $out --field ' ' -- sleep 0.001; status=$?
|
142 | assert $status -eq 0
|
143 |
|
144 | set -o errexit
|
145 |
|
146 | cat $out
|
147 |
|
148 | echo $'OK\ttest-bad-tsv-chars'
|
149 | }
|
150 |
|
151 | test-stdout() {
|
152 | local out=_tmp/time-stdout.csv
|
153 | time-tool -o $out --stdout _tmp/stdout.txt -- seq 3
|
154 |
|
155 | diff _tmp/stdout.txt - <<EOF
|
156 | 1
|
157 | 2
|
158 | 3
|
159 | EOF
|
160 |
|
161 | # No assertions here yet
|
162 | md5sum _tmp/stdout.txt
|
163 | cat $out | count-lines-and-cols 1 3 ,
|
164 |
|
165 | time-tool -o $out --rusage --stdout _tmp/stdout.txt -- seq 3
|
166 | cat $out | count-lines-and-cols 1 6 ,
|
167 | }
|
168 |
|
169 | test-rusage() {
|
170 | local out=_tmp/time-rusage.csv
|
171 | time-tool --tsv -o $out --rusage -- bash -c 'echo bash'
|
172 | cat $out | count-lines-and-cols 1 5
|
173 |
|
174 | #time-tool --tsv -o $out --rusage -- dash -c 'echo dash'
|
175 | #cat $out
|
176 |
|
177 | # Blow up memory size for testing
|
178 | local py='a=[42]*500000; print "python"'
|
179 |
|
180 | time-tool --tsv -o $out --rusage -- python2 -c "$py"
|
181 | cat $out | count-lines-and-cols 1 5
|
182 |
|
183 | #time-tool --tsv -o $out --rusage -- bin/osh -c 'echo osh'
|
184 | #cat $out
|
185 | }
|
186 |
|
187 | test-time-span() {
|
188 | local out=_tmp/time-span.csv
|
189 |
|
190 | time-tool --tsv -o $out --time-span --print-header
|
191 | cat $out | count-lines-and-cols 1 4
|
192 |
|
193 | time-tool --tsv -o $out --time-span -- bash -c 'echo bash'
|
194 | cat $out | count-lines-and-cols 1 4
|
195 | }
|
196 |
|
197 | # Compare vs. /usr/bin/time.
|
198 | test-maxrss() {
|
199 | if which time; then # Ignore this on continuous build
|
200 | command time --format '%x %U %M' -- seq 1
|
201 | fi
|
202 |
|
203 | # Showing a discrepancy. FIXED!
|
204 | time-tool -o _tmp/maxrss --tsv --rusage -- seq 1
|
205 | cat _tmp/maxrss
|
206 | }
|
207 |
|
208 | test-print-header() {
|
209 | set +o errexit
|
210 |
|
211 | # no arguments allowed
|
212 | time-tool --tsv --print-header foo bar
|
213 | assert $? -eq 2
|
214 |
|
215 | time-tool --tsv --print-header --field name
|
216 | assert $? -eq 0
|
217 |
|
218 | time-tool --tsv --print-header --rusage --field name
|
219 | assert $? -eq 0
|
220 |
|
221 | time-tool --print-header --rusage --field foo --field bar
|
222 | assert $? -eq 0
|
223 |
|
224 | time-tool -o _tmp/time-test-1 \
|
225 | --print-header --rusage --stdout DUMMY --tsv --field a --field b
|
226 | assert $? -eq 0
|
227 |
|
228 | #set -x
|
229 | head _tmp/time-test-1
|
230 | }
|
231 |
|
232 | test-time-helper() {
|
233 | set +o errexit
|
234 |
|
235 | local tmp=_tmp/time-helper.txt
|
236 |
|
237 | local th=_devbuild/bin/time-helper
|
238 |
|
239 | # Make some work show up
|
240 | local cmd='{ md5sum */*.md; sleep 0.15; exit 42; } > /dev/null'
|
241 |
|
242 | echo 'will be overwritten' > $tmp
|
243 | cat $tmp
|
244 |
|
245 | $th
|
246 | assert $? -ne 0 # it's 1, but could be 2
|
247 |
|
248 | $th /bad
|
249 | assert $? -eq 1
|
250 |
|
251 | $th -o $tmp -d $'\t' -x -e -- sh -c "$cmd"
|
252 | assert $? -eq 42
|
253 | cat $tmp
|
254 | echo
|
255 |
|
256 | # Now append
|
257 | $th -o $tmp -a -d , -x -e -U -S -M -- sh -c "$cmd"
|
258 | assert $? -eq 42
|
259 | cat $tmp
|
260 | echo
|
261 |
|
262 | # Error case
|
263 | $th -q
|
264 | assert $? -eq 2
|
265 | }
|
266 |
|
267 | test-time-tsv() {
|
268 | local status
|
269 |
|
270 | local out=_tmp/time-test-zz
|
271 | rm -f -v $out
|
272 |
|
273 | # Similar to what soil/worker.sh does
|
274 | set +o errexit
|
275 | time-tsv -o $out --append -- zz
|
276 | status=$?
|
277 | set -o errexit
|
278 |
|
279 | echo status=$status
|
280 | assert $status -eq 1
|
281 |
|
282 | cat $out
|
283 | echo
|
284 | }
|
285 |
|
286 | test-grandchild-memory() {
|
287 | local -a use_mem=( python2 -c 'import sys; ["X" * int(sys.argv[1])]' 10000000 )
|
288 |
|
289 | time-tsv -o /dev/stdout --rusage -- "${use_mem[@]}"
|
290 |
|
291 | # RUSAGE_CHILDREN includes grandchildren!
|
292 | time-tsv -o /dev/stdout --rusage -- sh -c 'echo; "$@"' dummy "${use_mem[@]}"
|
293 |
|
294 | # 'exec' doesn't make a consistent difference, because /bin/sh doesn't use
|
295 | # much memory
|
296 | time-tsv -o /dev/stdout --rusage -- sh -c 'echo; exec "$@"' dummy "${use_mem[@]}"
|
297 | }
|
298 |
|
299 | soil-run() {
|
300 | run-test-funcs
|
301 | }
|
302 |
|
303 | "$@"
|