Skip to content

Commit ad8ef82

Browse files
committed
05/04: add solution draft
1 parent 0a48126 commit ad8ef82

File tree

10 files changed

+71
-0
lines changed

10 files changed

+71
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["1","2","3","4",null],"3.0.8",[],["5"],["6"],{"type":"7"},["8","9"],"Unhandled Error","",[]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["1","2","3","4",null],"3.0.8",[],["5"],["6"],{"type":"7"},["8","9"],"Unhandled Error","",[]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["1","2","3","4",null],"3.0.8",[],["5"],["6"],{"type":"7"},["8","9"],"Unhandled Error","",[]]
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[["1","2","3","4",null],"3.0.8",[],["5"],["6"],{"type":"7"},["8","9"],"Unhandled Error","",[]]
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,41 @@
11
# Sharding
2+
3+
Our test files are running in parallel and our test cases are running concurrently, but we can do more with Vitest. Large test suites can still be slow even despite all of that parallelism simple because there are a lot of tests to run.
4+
5+
This is where it comes in handy to split our entire test suite into groups (or shards) using a technique called _sharding_. With sharding, we can split a huge test suite in smaller pieces and run them individually and in parallel. It will mean _multiple Vitest processes_ running different portions of the test suite.
6+
7+
To enable sharding, we need to change the way we run Vitest:
8+
9+
1. Provide the [`--shard`](https://main.vitest.dev/guide/cli#shard) option to the Vitest CLI, describing which shard group this process should run. For example, to run the first quarter of the tests, use `--shard=1/4`; the second quarter will be `--shard=2/4`, and so on.
10+
1. Use [`--reporter=blob`](https://main.vitest.dev/guide/reporters#blob-reporter) because it's required to support sharding.
11+
12+
These changes can be illustrated in a simple Bash script.
13+
14+
```sh filename=run-tests.sh
15+
npx vitest --reporter=blob --shard=1/4 & \
16+
npx vitest --reporter=blob --shard=2/4 & \
17+
npx vitest --reporter=blob --shard=3/4 & \
18+
npx vitest --reporter=blob --shard=4/4
19+
```
20+
21+
```sh nonumber
22+
./run-tests.sh
23+
```
24+
25+
This script will split the test suite in four shard and run them in parallel (`&`), producing isolated test reports.
26+
27+
<callout-info>Sharding is particularly useful in CI. If you have a large test suite that takes 8s to complete, splitting it into four groups and four parallel CI jobs can speed up the tests up to _four times_.</callout-info>
28+
29+
## Merging test reports
30+
31+
But since sharding splits our test suite, we need to glue it back together when previewing the results of the test run.
32+
33+
Use the [`--merge-reports`](https://main.vitest.dev/guide/cli#merge-reports) flag in the Vitest CLI to merge multiple sharded test reports into one:
34+
35+
```sh nonumber
36+
npx vitest run --merge-reports
37+
```
38+
39+
```
40+
TODO: Show an example of the groupped reported test run here.
41+
```
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=1/4 & \
2+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=2/4 & \
3+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=3/4 & \
4+
VITEST_MAX_FORKS=5 npx vitest run --reporter=blob --shard=4/4 & \
5+
wait
6+
npx vitest run --merge-reports
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
3+
for (let i = 0; i < 5000; i++) {
4+
test.concurrent(`${i}`, async ({ expect }) => {
5+
await setTimeout(25)
6+
expect(i).toBe(i)
7+
})
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { setTimeout } from 'node:timers/promises'
2+
3+
for (let i = 0; i < 5000; i++) {
4+
test.concurrent(`${i}`, async ({ expect }) => {
5+
await setTimeout(25)
6+
expect(i).toBe(i)
7+
})
8+
}

exercises/05.performance/04.solution.sharding/vite.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ export default defineConfig({
77
},
88
test: {
99
globals: true,
10+
maxConcurrency: 50,
1011
},
1112
})

exercises/05.performance/README.mdx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@
55
- Vitest isolates each test _file_ (e.g. each test file runs in its own fork/worker/vm).
66
- Vitest runs test _files_ concurrently.
77
- Test cases in test files run _sequentially_ (thus a designated `test.concurrent()`).
8+
9+
## Where to start
10+
11+
1. Understand the difference between _parallelism_ and _concurrency_.

0 commit comments

Comments
 (0)