Sub topic 12
Setup overhead, caching and cost per shard
This chapter assumes you can read a GitHub Actions workflow, run a test suite from the command line, and have seen a shard matrix like --shard=${{ matrix.shard }}/4. Here you will see why adding shards has diminishing returns and how to reduce the fixed cost each shard pays.
Every shard pays the setup tax
A shard is a full CI job. Before it runs a single test, it checks out the code, installs the runtime, installs dependencies, and may download browsers or build the app. This setup time is the same on every shard, no matter how few tests that shard runs.
Total wall-clock time for a sharded run is roughly:
wall time = setup + (test time / shards) + aggregationOnly the test portion shrinks as you add shards. Setup stays constant, and billed compute grows because setup now runs once per shard.
def estimate(setup_min: float, test_min: float, shards: int, rate_per_min: float = 0.008):
wall = setup_min + test_min / shards
billed = shards * (setup_min + test_min / shards)
return wall, billed, billed * rate_per_min
for n in (1, 2, 4, 8, 16):
wall, billed, cost = estimate(setup_min=3, test_min=40, shards=n)
print(f"{n:>2} shards: wall {wall:5.1f} min, billed {billed:5.1f} min, cost ${cost:.2f}")With 3 minutes of setup and 40 minutes of tests, going from 8 to 16 shards cuts wall time from 8.0 to 5.5 minutes. Billed time rises from 64 to 88 minutes. The rate of $0.008 per minute is an example. Use your provider's price.
Cut setup with caching
Caching stores files from one run so later runs can restore them instead of rebuilding them. Three things are worth caching in a typical Playwright project.
- Package downloads.
actions/setup-nodecan cache the npm download directory withcache: npm. Runnpm ciafter it sonode_modulesstill matchespackage-lock.json. - Browser binaries. Playwright downloads them to
~/.cache/ms-playwright. Cache that directory, keyed on the lockfile. - Build output. If tests need a built app, build once in a separate job and share it as an artifact instead of building on every shard.
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: 20
cache: npm
- run: npm ci
- uses: actions/cache@v4
id: pw-cache
with:
path: ~/.cache/ms-playwright
key: pw-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- run: npx playwright install --with-deps
if: steps.pw-cache.outputs.cache-hit != 'true'
- run: npx playwright install-deps
if: steps.pw-cache.outputs.cache-hit == 'true'
- run: npx playwright test --shard=${{ matrix.shard }}/4The system libraries that browsers need are not stored in the browser cache. That is why the cached path still runs install-deps.
Reduce what shards must do
- Split the build into its own job and upload the result with
actions/upload-artifact. Shards download it withactions/download-artifact. - Use a prebuilt container image that already holds the browsers and system libraries.
- Skip steps a shard does not need, such as linting or type checking, which belong in a single separate job.
Deciding when another shard is worth it
Compare the time saved against the cost added. Adding shard number n+1 saves about test / n - test / (n+1) minutes of wall time and adds one more setup run.
Also consider queue time. Many matrix jobs compete for limited runners, so a large matrix can wait for free machines and lose the benefit you expected.
Exercise
A suite takes 36 minutes of test time and each shard has 4 minutes of setup. Compare 6 shards with 12 shards in wall time and billed minutes. Is 12 worth it if you only care about feedback time under 10 minutes?
Show answer
With 6 shards, wall time is 4 + 36/6 = 10 minutes and billed time is 6 × 10 = 60 minutes. With 12 shards, wall time is 4 + 36/12 = 7 minutes and billed time is 12 × 7 = 84 minutes. If the target is feedback under 10 minutes, 6 shards reaches exactly 10, which does not meet "under". Using 12 shards gives 7 minutes at a cost of 24 extra billed minutes. An intermediate count such as 8 shards (4 + 4.5 = 8.5 minutes, 68 billed minutes) meets the goal at a lower cost.