This repository has been archived by the owner on Dec 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
perform_tests
executable file
·231 lines (208 loc) · 5.32 KB
/
perform_tests
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
#! /bin/bash
# TODO: Commandline options for skipping build, specific day etc.
current_target_prefix() {
local lang=$(basename $(dirname $(pwd -P)))
if [[ $lang == rust ]] && find Cargo.toml &> /dev/null; then
echo "./target/release/part"
elif [[ $lang == haskell ]]; then
echo "./Part"
else
echo "./part"
fi
}
usage() {
cat <<EOF
This nifty little scripts automatically builds, tests and times the existing codebase,
and outputs the results in markdown table format into the README if specified.
usage: $(basename $0) [-bhg] [-l LANG] [-t TEST_THRESH]
-h: Show this help message.
-b: Attempt to build the tested codes.
-g: Regenerate README with results.
-s: Skip tests, useful for regenerating the README quickly.
-l LANG: Only test for language LANG. (rust, haskell, c)
-t TEST_THRESH: Stop testing a program after exceeding TEST_THRESH milliseconds.
A program is tested multiple times until the threshold is exceeded
for slightly more accurate timing estimation. 1 by default to quickly
run each program just once.
-d DAY: Only test for a single specific day.
EOF
}
# No links please (;
cd $(dirname "$0")
DAYS=$(ls -1 io)
LANGS="rust haskell c"
BUILD=false
GENERATE=false
TIMEOUT=15s
TIME_THRESH=1
DAY=
SKIP_TEST=false
while getopts hbgsl:t:d: opt; do
case $opt in
b)
BUILD=true
;;
s)
SKIP_TEST=true
;;
l)
LANGS=$OPTARG
;;
g)
GENERATE=true
;;
t)
TIME_THRESH=$OPTARG
;;
d)
DAY=$OPTARG
;;
h)
usage
exit
;;
esac
done
if $BUILD; then
for lang in $LANGS; do
echo "Compiling $lang code..."
cd $lang
for day in day??; do
echo "$day"
cd $day
if [[ $lang == rust ]]; then
if find Cargo.toml &> /dev/null; then
cargo build
cargo build --release
else
rustc part1.rs
rustc part2.rs
fi
elif [[ $lang == haskell ]]; then
ghc -Wall -O2 Part1.hs
ghc -Wall -O2 Part2.hs
else
if [[ -f Makefile ]]; then
make
else
gcc -Wall -O3 part1.c -o part1
gcc -Wall -O3 part2.c -o part2
fi
fi
cd ..
echo
done
cd ..
done
fi
declare -A results
declare -A mstimes
if ! $SKIP_TEST; then
for lang in $LANGS; do
cd $lang
for day in day??; do
if [ -n "$DAY" -a "day$DAY" != "$day" ]; then
continue
fi
cd $day
iod="../../io/$day"
for c in 1 2; do
part="part$c"
exe="$(current_target_prefix)$c"
out=$(mktemp)
start_time=$(date +%s%N)
timeout -k "$TIMEOUT" "$TIMEOUT" $exe < "$iod"/input.txt > "$out" 2> /dev/null
end_time=$(date +%s%N)
if diff -w "$out" "$iod"/"$part"_output.txt &> /dev/null; then
rm $out
verdict='\033[32m+\033[0m' # green colored +
results[$lang,$day,$c]=1
# start measuring timing
total_time=$(((end_time - start_time) / 1000000))
times_run=1
while [ $(echo "$total_time < $TIME_THRESH" | bc) -eq 1 ]; do
start_time=$(date +%s%N)
$exe < "$iod"/input.txt &> /dev/null
end_time=$(date +%s%N)
ms_time=$(((end_time - start_time) / 1000000))
total_time=$((total_time + ms_time))
times_run=$((times_run + 1))
done
avg_time=$((total_time / times_run))
mstimes[$lang,$day,$c]=$avg_time
printf "%8s $day $part $verdict [%02d]:(%dms)\n" "$lang" "$times_run" "$avg_time"
else
verdict='\033[31m-\033[0m' # red colored -
printf "%8s $day $part $verdict\n" "$lang"
fi
done
cd ..
done
cd ..
done
fi
if $GENERATE; then
# Could do all in one loop by inverting previous, but meh!
exec 5>&1
if ! $SKIP_TEST; then
exec 1>correctness.md
cols="problem $LANGS"
for header in $cols; do
echo -n "| ${header^}"
done
echo "|"
for _ in $cols; do
echo -n "| :---: "
done
echo "|"
for day in $DAYS; do
for c in 1 2; do
echo -n "| ${day^} - Part $c |"
for lang in $LANGS; do
if [[ ${results[$lang,$day,$c]} == 1 ]]; then
echo -n " ✅ |"
else
echo -n " |"
fi
done
echo
done
done
exec 1>mstimes.md
cols="problem $LANGS"
for header in $cols; do
echo -n "| ${header^}"
done
echo "|"
for _ in $cols; do
echo -n "| :---: "
done
echo "|"
for day in $DAYS; do
for c in 1 2; do
echo -n "| ${day^} - Part $c |"
for lang in $LANGS; do
if [[ ${results[$lang,$day,$c]} == 1 ]]; then
ms_time=${mstimes[$lang,$day,$c]}
echo -n " $ms_time""ms |"
else
echo -n " N/A |"
fi
done
echo
done
done
fi
exec 1>README.md
INCLUDE_REGEX='^@include[ \t]*([a-zA-Z0-9._-]+)$'
while IFS= read ln; do
if [[ $ln =~ $INCLUDE_REGEX ]]; then
include_file=${BASH_REMATCH[1]}
cat "$include_file"
else
printf '%s\n' "$ln"
fi
done < readme_template.md
# rm -f mstimes.md correctness.md
exec 1>&5
fi