Repository navigation
Expand file tree
/
Copy pathtest-componentize.sh
More file actions
executable file
·100 lines (92 loc) · 3.77 KB
/
Copy pathtest-componentize.sh
File metadata and controls
executable file
·100 lines (92 loc) · 3.77 KB
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
#!/usr/bin/env bash
# SPDX-License-Identifier: Apache-2.0-WITH-LLVM-exception
#
# Run the componentization suites of `componentize/tests/`, several test binaries at a time.
#
# The script builds every test binary first, then runs `COMPONENTIZE_JOBS` of them at a time. The
# default is one per 8 GiB of memory, at least one and at most one per CPU. `serve` and
# `top_level`, which take longest, start first. Each binary runs `RUST_TEST_THREADS` tests at a
# time, by default twice the number of CPUs divided by the number of jobs, but at most one per GiB
# of memory divided by the number of jobs, and at least one.
#
# Each binary's output goes to a log file. Once the binary exits, the script prints its
# `test result:` line if it passed, and the whole log if it failed. A binary that exits without a
# `test result:` line failed too. The script fails if any binary did.
#
# The suites use the runtime build `STARLING_RUNTIME` names. The arguments go to every test
# binary, so `./scripts/test-componentize.sh echo_stream` runs only the matching tests. Needs `jq`
# on PATH.
#
# Usage:
# ./scripts/test-componentize.sh [<libtest argument>...]
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR/../componentize"
cpus=$(getconf _NPROCESSORS_ONLN)
if [[ -r /proc/meminfo ]]; then
memory_kib=$(awk '/^MemTotal:/ { print $2 }' /proc/meminfo)
else
memory_kib=$(( $(sysctl -n hw.memsize) / 1024 ))
fi
jobs=$(( memory_kib / (8 * 1024 * 1024) ))
(( jobs > cpus )) && jobs=$cpus
(( jobs < 1 )) && jobs=1
jobs=${COMPONENTIZE_JOBS:-$jobs}
threads=$(( 2 * cpus / jobs ))
memory_threads=$(( memory_kib / (1024 * 1024) / jobs ))
(( threads > memory_threads )) && threads=$memory_threads
(( threads < 1 )) && threads=1
export RUST_TEST_THREADS=${RUST_TEST_THREADS:-$threads}
echo "=== Building the componentization suites"
binaries=$(cargo test --tests --no-run --message-format=json-render-diagnostics |
jq -r 'select(.reason == "compiler-artifact" and .profile.test and .executable != null)
| .executable')
# The binaries, `serve` and `top_level` first. A binary's name is its file name without the hash
# cargo appends.
ordered=$(printf '%s\n' "$binaries" | awk '{
name = $0
sub(/.*\//, "", name)
sub(/-[0-9a-f]+$/, "", name)
print (name == "serve" || name == "top_level" ? 0 : 1) "\t" $0
}' | sort -s -k1,1 | cut -f2-)
if [[ -z "$ordered" ]]; then
echo "The build produced no test binaries." >&2
exit 1
fi
count=$(printf '%s\n' "$ordered" | wc -l | tr -d ' ')
LOGS=$(mktemp -d)
trap 'rm -rf "$LOGS"' EXIT
export LOGS
# Run the test binary `$1` with the remaining arguments, logging to `$LOGS/<name>.log`. Prints
# the binary's `test result:` line if it passed, and its log otherwise, in which case it creates
# `$LOGS/<name>.failed`.
run_binary() {
local binary=$1
shift
local name
name=$(basename "$binary" | sed -E 's/-[0-9a-f]+$//')
local log="$LOGS/$name.log"
if "$binary" "$@" > "$log" 2>&1 && grep -q '^test result: ' "$log"; then
echo "$name: $(grep '^test result: ' "$log" | tail -n 1)"
else
touch "$LOGS/$name.failed"
echo "=== $name failed"
cat "$log"
echo "=== end of $name"
fi
}
export -f run_binary
echo "=== Running $count test binaries, $jobs at a time, $RUST_TEST_THREADS tests each"
# xargs appends the binary after the libtest arguments, and the inner shell moves it to the front.
# shellcheck disable=SC2016
printf '%s\n' "$ordered" | tr '\n' '\0' |
xargs -0 -n 1 -P "$jobs" bash -c 'run_binary "${@: -1}" "${@:1:$#-1}"' _ "$@"
failed=""
for marker in "$LOGS"/*.failed; do
[[ -e "$marker" ]] && failed="$failed $(basename "$marker" .failed)"
done
if [[ -n "$failed" ]]; then
echo "Failed test binaries: $failed" >&2
exit 1
fi
echo "All $count test binaries passed."