-
-
Notifications
You must be signed in to change notification settings - Fork 145
/
time-test.sh
executable file
·260 lines (192 loc) · 5.11 KB
/
time-test.sh
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#!/usr/bin/env bash
#
# Usage:
# benchmarks/time-test.sh <function name>
set -o nounset
set -o pipefail
set -o errexit
source test/common.sh
# TODO: This would be a nice little program for Oil
count-lines-and-cols() {
python2 -c '
import sys
expected_num_lines = int(sys.argv[1])
expected_num_cols = int(sys.argv[2])
try:
sep = sys.argv[3]
except IndexError:
sep = "\t"
num_lines = 0
tab_counts = []
for line in sys.stdin:
tab_counts.append(line.count(sep))
num_lines += 1
# Show what we get
sys.stdout.write(line)
if any(tab_counts[0] != n for n in tab_counts):
raise AssertionError(tab_counts)
num_tabs = tab_counts[0]
assert expected_num_lines == num_lines, \
"expected %d lines, got %d" % (expected_num_lines, num_lines)
assert expected_num_cols == num_tabs + 1, \
"expected %d cols, got %d" % (expected_num_cols, num_tabs + 1)
' "$@"
}
time-tool() {
$(dirname $0)/time_.py "$@"
}
test-csv() {
local out=_tmp/time.csv
time-tool -o $out -- echo hi
cat $out | count-lines-and-cols 1 2 ,
time-tool -o $out --field a --field b -- echo hi
cat $out | count-lines-and-cols 1 4 ,
time-tool -o $out --rusage -- echo hi
cat $out | count-lines-and-cols 1 5 ,
time-tool -o $out --rusage --field a --field b -- echo hi
cat $out | count-lines-and-cols 1 7 ,
}
test-tsv() {
local out=_tmp/time.tsv
rm -f $out
for i in 1 2 3; do
time-tool --tsv -o $out --append --time-fmt '%.2f' -- sleep 0.0${i}
done
cat $out | count-lines-and-cols 3 2
time-tool --tsv -o $out --field a --field b -- echo hi
cat $out | count-lines-and-cols 1 4
time-tool --tsv -o $out --rusage --field a --field b -- echo hi
cat $out | count-lines-and-cols 1 7
}
test-append() {
local out=_tmp/overwrite.tsv
for i in 4 5; do
time-tool --tsv -o $out -- sleep 0.0${i}
done
cat $out | count-lines-and-cols 1 2
echo ---
local out=_tmp/append.tsv
rm -f $out
for i in 4 5; do
time-tool --tsv -o $out --append -- sleep 0.0${i}
done
cat $out | count-lines-and-cols 2 2
}
test-usage() {
# no args
set +o errexit
time-tool; status=$?
assert $status -eq 2
time-tool --output; status=$?
assert $status -eq 2
time-tool sleep 0.1
time-tool --append sleep 0.1; status=$?
assert $status -eq 0
set -o errexit
}
test-bad-tsv-chars() {
local out=_tmp/time2.tsv
rm -f $out
set +o errexit
# Newline should fail
time-tool --tsv -o $out --field $'\n' -- sleep 0.001; status=$?
assert $status -eq 1
# Tab should fail
time-tool --tsv -o $out --field $'\t' -- sleep 0.001; status=$?
assert $status -eq 1
# Quote should fail
time-tool --tsv -o $out --field '"' -- sleep 0.001; status=$?
assert $status -eq 1
# Backslash is OK
time-tool --tsv -o $out --field '\' -- sleep 0.001; status=$?
assert $status -eq 0
# Space is OK, although canonical form would be " "
time-tool --tsv -o $out --field ' ' -- sleep 0.001; status=$?
assert $status -eq 0
set -o errexit
cat $out
echo $'OK\ttest-bad-tsv-chars'
}
test-stdout() {
local out=_tmp/time-stdout.csv
time-tool -o $out --stdout _tmp/stdout.txt -- seq 3
diff _tmp/stdout.txt - <<EOF
1
2
3
EOF
# No assertions here yet
md5sum _tmp/stdout.txt
cat $out | count-lines-and-cols 1 3 ,
time-tool -o $out --rusage --stdout _tmp/stdout.txt -- seq 3
cat $out | count-lines-and-cols 1 6 ,
}
test-rusage() {
# No assertions here yet
local out=_tmp/time-usage.csv
time-tool --tsv -o $out --rusage -- bash -c 'echo bash'
cat $out | count-lines-and-cols 1 5
#time-tool --tsv -o $out --rusage -- dash -c 'echo dash'
#cat $out
# Blow up memory size for testing
local py='a=[42]*500000; print "python"'
time-tool --tsv -o $out --rusage -- python2 -c "$py"
cat $out | count-lines-and-cols 1 5
#time-tool --tsv -o $out --rusage -- bin/osh -c 'echo osh'
#cat $out
}
# Compare vs. /usr/bin/time.
test-maxrss() {
if which time; then # Ignore this on continuous build
command time --format '%x %U %M' -- seq 1
fi
# Showing a discrepancy. FIXED!
time-tool -o _tmp/maxrss --tsv --rusage -- seq 1
cat _tmp/maxrss
}
test-print-header() {
set +o errexit
# no arguments allowed
time-tool --tsv --print-header foo bar
assert $? -eq 2
time-tool --tsv --print-header --field name
assert $? -eq 0
time-tool --tsv --print-header --rusage --field name
assert $? -eq 0
time-tool --print-header --rusage --field foo --field bar
assert $? -eq 0
time-tool -o _tmp/time-test-1 \
--print-header --rusage --stdout DUMMY --tsv --field a --field b
assert $? -eq 0
#set -x
head _tmp/time-test-1
}
test-time-helper() {
set +o errexit
local tmp=_tmp/time-helper.txt
local th=_devbuild/bin/time-helper
# Make some work show up
local cmd='{ md5sum */*.md; sleep 0.15; exit 42; } > /dev/null'
echo 'will be overwritten' > $tmp
cat $tmp
$th
assert $? -ne 0 # it's 1, but could be 2
$th /bad
assert $? -eq 1
$th -o $tmp -d $'\t' -x -e -- sh -c "$cmd"
assert $? -eq 42
cat $tmp
echo
# Now append
$th -o $tmp -a -d , -x -e -U -S -M -- sh -c "$cmd"
assert $? -eq 42
cat $tmp
echo
# Error case
$th -z
assert $? -eq 2
}
soil-run() {
run-test-funcs
}
"$@"