Skip to content

Commit 5d1456f

Browse files
Use a shared cache when testing examples, but better (#481)
### Motivation The examples pipeline currently takes around one hour to run. The CI timeout is 60 minutes so we sometimes don't even finish it before the CI job gets cancelled. We previously experimented with enabling a shared build cache but this wasn't working as desired because the paths to the source files were different for each of the examples we build. However, by using the same package harness path, we should be able to get better build cache performance (and keep the cache smaller during the run). ### Modifications - Use a shared `--cache-path` and `--scratch-path`. - Use a stable `--package-path` for each example (we delete it entirely and recreate to be sure it's clean). I experimented briefly with `ccache` which I thought might help with some of the builds (e.g. the BoringSSL build, which comes in a few times as a dependency) but didn't get very far. ### Result CI pipeline should run much quicker. It's possible that we will not catch failures where we depend on something that we haven't declared properly, because these object files will still be in the cache. I can't say with confidence that this is how the build system works though; potentially it's a non-issue. ### Test Plan CI.
1 parent ce18524 commit 5d1456f

File tree

1 file changed

+34
-16
lines changed

1 file changed

+34
-16
lines changed

scripts/test-examples.sh

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,60 @@ TMP_DIR=$(/usr/bin/mktemp -d -p "${TMPDIR-/tmp}" "$(basename "$0").XXXXXXXXXX")
2727

2828
PACKAGE_PATH=${PACKAGE_PATH:-${REPO_ROOT}}
2929
EXAMPLES_PACKAGE_PATH="${PACKAGE_PATH}/Examples"
30+
SHARED_EXAMPLE_HARNESS_PACKAGE_PATH="${TMP_DIR}/swift-openapi-example-harness"
31+
SHARED_PACKAGE_SCRATCH_PATH="${TMP_DIR}/swift-openapi-example-cache"
32+
SHARED_PACKAGE_CACHE_PATH="${TMP_DIR}/swift-openapi-example-scratch"
3033

3134
for EXAMPLE_PACKAGE_PATH in $(find "${EXAMPLES_PACKAGE_PATH}" -maxdepth 2 -name Package.swift -type f -print0 | xargs -0 dirname); do
3235

3336
EXAMPLE_PACKAGE_NAME="$(basename "${EXAMPLE_PACKAGE_PATH}")"
34-
EXAMPLE_COPY_DIR="${TMP_DIR}/${EXAMPLE_PACKAGE_NAME}"
3537

3638
if [[ "${SINGLE_EXAMPLE_PACKAGE:-${EXAMPLE_PACKAGE_NAME}}" != "${EXAMPLE_PACKAGE_NAME}" ]]; then
3739
log "Skipping example: ${EXAMPLE_PACKAGE_NAME}"
3840
continue
3941
fi
4042

41-
log "Copying example ${EXAMPLE_PACKAGE_NAME} to ${EXAMPLE_COPY_DIR}"
42-
mkdir "${EXAMPLE_COPY_DIR}"
43-
git archive HEAD "${EXAMPLE_PACKAGE_PATH}" --format tar | tar -C "${EXAMPLE_COPY_DIR}" -xvf- --strip-components 2
43+
log "Recreating shared example harness directory: ${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}"
44+
rm -rf "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}"
45+
mkdir -v "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}"
4446

45-
log "Overriding dependency in ${EXAMPLE_PACKAGE_NAME} to use ${PACKAGE_PATH}"
47+
log "Copying example contents from ${EXAMPLE_PACKAGE_NAME} to ${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}"
48+
git archive HEAD "${EXAMPLE_PACKAGE_PATH}" --format tar | tar -C "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}" -xvf- --strip-components 2
49+
50+
# GNU tar has --touch, but BSD tar does not, so we'll use touch directly.
51+
log "Updating mtime of example contents..."
52+
find "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}" -print0 | xargs -0 -n1 touch -m
53+
54+
log "Re-overriding dependency in ${EXAMPLE_PACKAGE_NAME} to use ${PACKAGE_PATH}"
55+
"${SWIFT_BIN}" package \
56+
--package-path "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}" \
57+
--cache-path "${SHARED_PACKAGE_CACHE_PATH}" \
58+
--skip-update \
59+
--scratch-path "${SHARED_PACKAGE_SCRATCH_PATH}" \
60+
unedit swift-openapi-generator || :
4661
"${SWIFT_BIN}" package \
47-
--package-path "${EXAMPLE_COPY_DIR}" \
62+
--package-path "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}" \
63+
--cache-path "${SHARED_PACKAGE_CACHE_PATH}" \
64+
--skip-update \
65+
--scratch-path "${SHARED_PACKAGE_SCRATCH_PATH}" \
4866
edit swift-openapi-generator \
4967
--path "${PACKAGE_PATH}"
5068

5169
log "Building example package: ${EXAMPLE_PACKAGE_NAME}"
52-
"${SWIFT_BIN}" build \
53-
--package-path "${EXAMPLE_COPY_DIR}"
70+
"${SWIFT_BIN}" build --build-tests \
71+
--package-path "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}" \
72+
--cache-path "${SHARED_PACKAGE_CACHE_PATH}" \
73+
--skip-update \
74+
--scratch-path "${SHARED_PACKAGE_SCRATCH_PATH}"
5475
log "✅ Successfully built the example package ${EXAMPLE_PACKAGE_NAME}."
5576

56-
if [ -d "${EXAMPLE_COPY_DIR}/Tests" ]; then
77+
if [ -d "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}/Tests" ]; then
5778
log "Running tests for example package: ${EXAMPLE_PACKAGE_NAME}"
5879
"${SWIFT_BIN}" test \
59-
--package-path "${EXAMPLE_COPY_DIR}"
80+
--package-path "${SHARED_EXAMPLE_HARNESS_PACKAGE_PATH}" \
81+
--cache-path "${SHARED_PACKAGE_CACHE_PATH}" \
82+
--skip-update \
83+
--scratch-path "${SHARED_PACKAGE_SCRATCH_PATH}"
6084
log "✅ Passed the tests for the example package ${EXAMPLE_PACKAGE_NAME}."
6185
fi
62-
63-
log "Deleting example ${EXAMPLE_PACKAGE_NAME} at ${EXAMPLE_COPY_DIR}"
64-
rm -rf "${EXAMPLE_COPY_DIR}"
6586
done
66-
67-
log "Deleting temporary directory"
68-
rm -rf "${TMP_DIR}"

0 commit comments

Comments
 (0)