diff --git a/.dockerignore b/.dockerignore index 4721023..b59afcd 100644 --- a/.dockerignore +++ b/.dockerignore @@ -13,3 +13,5 @@ dev.Dockerfile !/bin/run.sh !/bin/prepare.sh test/ +!/bin/warm-up.sh +!/bin/record-prefetch-list.sh diff --git a/Dockerfile b/Dockerfile index a9781cc..202b534 100644 --- a/Dockerfile +++ b/Dockerfile @@ -52,6 +52,37 @@ ENV COREPACK_ENABLE_NETWORK=0 \ # Mark this as a docker run so we don't try to execute things in /tmp TMP_MAY_BE_NON_EXEC=1; +# Node saves the compiled code of each module it loads in this folder, and +# reuses it in later runs. bin/warm-up.sh runs the test runner once to fill +# it, so each run skips compiling Jest, the transformer and the reporter. +# Node keeps a separate cache for each user ID, so the warm-up runs as +# appuser. Root then owns the cache, so runs only read it. +ENV NODE_COMPILE_CACHE=/opt/test-runner/.node-compile-cache +RUN mkdir .node-compile-cache && chown appuser .node-compile-cache +USER appuser +RUN set -ex; \ + bin/warm-up.sh; \ + # remove Jest's cache from the warm-up, so runs start with an empty one + rm -rf /tmp/jest_* +USER root +# Node writes the cache files readable by their owner only +RUN chown -R root:root .node-compile-cache && chmod -R a+rX .node-compile-cache + # Execute everything as the appuser USER appuser ENTRYPOINT [ "/opt/test-runner/bin/run.sh" ] + +# Record the files read by a run. A separate stage keeps strace out of the +# final image. +FROM runner AS prefetch-list +USER root +RUN set -ex; \ + apt-get update; \ + apt-get install strace -y +# Record as appuser, so the list has the files a real run reads from +# appuser's home folder. +USER appuser +RUN bin/record-prefetch-list.sh /tmp/prefetch-list + +FROM runner +COPY --from=prefetch-list /tmp/prefetch-list /opt/test-runner/.prefetch-list diff --git a/bin/record-prefetch-list.sh b/bin/record-prefetch-list.sh new file mode 100755 index 0000000..a2e4e7a --- /dev/null +++ b/bin/record-prefetch-list.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +# Synopsis: +# Record the files read by one run of the test runner, while building the +# Docker image. run.sh reads these files into the page cache at the start of +# each run (see run.sh). + +# Arguments: +# $1: path of the list to write + +set -euo pipefail + +ROOT="$(realpath "$(dirname "$0")/..")" +TRACE="$(mktemp)" + +# Log every file opened by the warm-up run and by every process it starts +strace -f -qq -e trace=openat,execve -e signal=none -o "${TRACE}" "${ROOT}/bin/warm-up.sh" + +# Keep the files that opened successfully, in the order they were first +# opened. The warm-up exercise reads packages through its node_modules link, +# so those paths are rewritten to the real folder. Files in the exercise +# itself, and in /tmp, /proc, /dev and /sys, are left out. +grep -v ' = -1 ' "${TRACE}" \ + | grep -oE '(openat\(AT_FDCWD, |execve\()"[^"]+"' \ + | sed -E 's/^[^"]*"//; s/"$//' \ + | sed -E "s#^/tmp/[^/]+/warm-up/node_modules/#${ROOT}/node_modules/#" \ + | grep -vE '^/(tmp|proc|dev|sys)/' \ + | awk '!seen[$0]++' \ + | while read -r file; do + if test -f "${file}"; then echo "${file}"; fi + done > "$1" + +rm -f "${TRACE}" + +echo "Recorded $(wc -l < "$1") files in $1" diff --git a/bin/run.sh b/bin/run.sh index e1a8079..27560ba 100755 --- a/bin/run.sh +++ b/bin/run.sh @@ -77,6 +77,14 @@ OUTPUT="${OUTPUT%/}/" set -euo pipefail ROOT="$(realpath $(dirname "$0")/..)" + +# Read the files a run needs into the page cache, 8 at a time, in the +# background. The Docker image records the list at build time. On a machine +# where the image's files are not in the page cache yet, Node read them one +# at a time as it needed them, which took several seconds per run. +if test -f "${ROOT}/.prefetch-list"; then + ( nice -n 19 xargs -P 8 -n 50 -a "${ROOT}/.prefetch-list" cat > /dev/null 2>&1 & ) +fi REPORTER="$ROOT/dist/reporter.js" SETUP="$ROOT/dist/jest/setup.js" CONFIG="$ROOT/jest.runner.config.js" diff --git a/bin/warm-up.sh b/bin/warm-up.sh new file mode 100755 index 0000000..8b53d21 --- /dev/null +++ b/bin/warm-up.sh @@ -0,0 +1,49 @@ +#!/usr/bin/env bash + +# Synopsis: +# Run the test runner once on a small exercise while building the Docker +# image, with NODE_COMPILE_CACHE set (see the Dockerfile). Node saves the +# compiled code of every module it loads (Jest, the transformer and the +# reporter's parser) in that folder. Each run then loads the compiled code +# instead of compiling it again. + +set -euo pipefail + +ROOT="$(realpath "$(dirname "$0")/..")" +WARM_UP_DIR="$(mktemp -d)/warm-up/" + +mkdir -p "${WARM_UP_DIR}.meta" + +cat > "${WARM_UP_DIR}.meta/config.json" <<'JSON' +{ + "files": { + "solution": ["warm-up.js"], + "test": ["warm-up.spec.js"] + } +} +JSON + +cat > "${WARM_UP_DIR}warm-up.js" <<'JS' +export function answer() { + console.log('warming up') + return 42 +} +JS + +cat > "${WARM_UP_DIR}warm-up.spec.js" <<'JS' +import { describe, expect, test } from '@jest/globals' +import { answer } from './warm-up' + +describe('answer', () => { + test('is 42', () => { + expect(answer()).toBe(42) + }) +}) +JS + +"${ROOT}/bin/run.sh" warm-up "${WARM_UP_DIR}" "${WARM_UP_DIR}" > /dev/null + +# Check the warm-up run passed, so a broken runner fails the image build +grep -q '"status": "pass"' "${WARM_UP_DIR}results.json" + +rm -rf "$(dirname "${WARM_UP_DIR}")"