Skip to content

Commit 64f61c8

Browse files
committed
docs(cn): update guide/parallelism.md
1 parent 1fdc544 commit 64f61c8

1 file changed

Lines changed: 22 additions & 23 deletions

File tree

guide/parallelism.md

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,30 @@ title: 并行性 | 指南
33
outline: deep
44
---
55

6-
<!-- TODO: translation -->
7-
86
# 并行性 {#parallelism}
97

10-
Vitest has two levels of parallelism: it can run multiple *test files* at the same time, and within each file it can run multiple *tests* at the same time. Understanding the difference between the two is important because they work differently and have different trade-offs.
8+
Vitest 有两个层级的并行机制:它可以同时运行多个 _测试文件_,也可以在单个文件内同时运行多个 _测试_。理解这两者之间的区别至关重要,因为它们的工作方式不同,权衡内容也不同。
119

1210
## 文件级并行 {#file-parallelism}
1311

14-
By default, Vitest runs test files in parallel across multiple workers. Each file gets its own isolated environment, so tests in different files can't interfere with each other.
12+
默认情况下,Vitest 会在多个 worker 中并行运行测试文件。每个文件都拥有独立的隔离环境,因此不同文件中的测试不会相互干扰。
1513

16-
The mechanism Vitest uses to create workers depends on the configured [`pool`](/config/pool):
14+
Vitest 创建 worker 的机制取决于配置的 [`pool`](/config/pool)
1715

18-
- `forks` (the default) and `vmForks` run each file in a separate [child process](https://nodejs.org/api/child_process.html)
19-
- `threads` and `vmThreads` run each file in a separate [worker thread](https://nodejs.org/api/worker_threads.html)
16+
- `forks`(默认值)和 `vmForks` 在单独的 [子进程](https://nodejs.org/api/child_process.html) 中运行每个文件
17+
- `threads` `vmThreads` 在单独的 [工作线程](https://nodejs.org/api/worker_threads.html) 中运行每个文件
2018

21-
You can control how many workers run simultaneously with the [`maxWorkers`](/config/maxworkers) option. More workers means more files run in parallel, but also more memory and CPU usage. The right number depends on your machine and how heavy your tests are.
19+
你可以通过 [`maxWorkers`](/config/maxworkers) 选项控制同时运行的 worker 数量。更多的 worker 意味着可以并行运行更多文件,但也会占用更多内存和 CPU。具体的数量取决于你的机器性能和测试的负载情况。
2220

23-
For most projects, file parallelism is the single biggest factor in test suite speed. However, there are cases where you might want to disable it — for example, if your tests share an external resource like a database that can't handle concurrent access. You can set [`fileParallelism`](/config/fileparallelism) to `false` to run files one at a time.
21+
对于大多数项目而言,文件级并行是影响测试套件速度的最主要因素。然而,在某些情况下,你可能需要禁用它:例如,当你的测试共享一个无法处理并发访问的外部资源(如数据库)时。你可以将 [`fileParallelism`](/config/fileparallelism) 设置为 `false` 来逐个顺序运行文件。
2422

25-
To learn more about tuning performance, see the [Performance Guide](/guide/improving-performance).
23+
要了解更多关于性能优化的信息,请参阅 [性能指南](/guide/improving-performance)
2624

2725
## 测试级并行 {#test-parallelism}
2826

29-
Within a single file, Vitest runs tests sequentially by default. Tests execute in the order they are defined, one after another. This is the safest default because tests within a file often share setup and state through lifecycle hooks like `beforeEach`.
27+
在单个文件内部,Vitest 默认按顺序运行测试。测试按照定义的顺序依次执行。这是最安全的默认设置,因为同一文件内的测试通常通过 `beforeEach` 等生命周期钩子共享初始化和状态。
3028

31-
If the tests in a file are independent, you can opt into running them concurrently with the [`concurrent`](/api/test#test-concurrent) modifier:
29+
如果文件中的测试是相互独立的,你可以选择使用 [`concurrent`](/api/test#test-concurrent) 修饰符来并发运行它们:
3230

3331
```ts
3432
import { expect, test } from 'vitest'
@@ -44,14 +42,14 @@ test.concurrent('fetches user posts', async () => {
4442
})
4543
```
4644

47-
When tests are marked as `concurrent`, Vitest groups them together and runs them with [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all). The number of tests running at once is bounded by the [`maxConcurrency`](/config/maxconcurrency) option.
45+
当测试被标记为 `concurrent` 时,Vitest 会将它们分组,并使用 [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) 运行它们。同时运行的测试数量受 [`maxConcurrency`](/config/maxconcurrency) 参数限制。
4846

49-
::: tip When does `concurrent` actually help?
50-
Vitest doesn't create extra workers for concurrent tests — they all run in the same worker as the file they belong to. This means `concurrent` only speeds things up when your tests spend time *waiting* (on network requests, timers, file I/O, etc.). Purely synchronous tests won't benefit because they still block the single JavaScript thread:
47+
::: tip `concurrent` 何时真正有效?
48+
Vitest 不会为并发测试创建额外的 worker,它们都会在所属文件的同一个 worker 中运行。这意味着,只有当测试会花时间 “等待”(例如等待网络请求、定时器、文件 I/O 等)时,`concurrent` 才能带来提速。纯同步测试不会因此受益,因为它们仍然会阻塞单个 JavaScript 线程:
5149

5250
```ts
53-
// These run one after another despite `concurrent`,
54-
// because there is nothing to await
51+
// 尽管使用了 `concurrent`,这些测试仍会依次运行,
52+
// 因为没有任何需要等待的内容
5553
test.concurrent('the first test', () => {
5654
expect(1).toBe(1)
5755
})
@@ -60,9 +58,10 @@ test.concurrent('the second test', () => {
6058
expect(2).toBe(2)
6159
})
6260
```
61+
6362
:::
6463

65-
You can also apply `concurrent` to an entire suite:
64+
你也可以将 `concurrent` 应用于整个测试套件:
6665

6766
```ts
6867
import { describe, expect, test } from 'vitest'
@@ -80,9 +79,9 @@ describe.concurrent('user API', () => {
8079
})
8180
```
8281

83-
If you want *all* tests in your project to run concurrently by default, set [`sequence.concurrent`](/config/sequence#sequence-concurrent) to `true` in your config.
82+
如果你希望项目中的 _所有_ 测试默认并发运行,可以在配置中将 [`sequence.concurrent`](/config/sequence#sequence-concurrent) 设置为 `true`
8483

85-
You can opt individual tests or suites out of inherited concurrency with `concurrent: false`:
84+
你可以通过 `concurrent: false` 让单个测试或测试套件退出继承的并发设置:
8685

8786
```ts
8887
test('uses a shared resource', { concurrent: false }, async () => {
@@ -95,8 +94,8 @@ describe('shared resource suite', { concurrent: false }, () => {
9594
})
9695
```
9796

98-
### Hooks with Concurrent Tests
97+
### 并发测试中的钩子 {#hooks-with-concurrent-tests}
9998

100-
When tests run concurrently, lifecycle hooks behave differently. `beforeAll` and `afterAll` still run once for the group, but `beforeEach` and `afterEach` run for each test — potentially at the same time, since the tests themselves overlap.
99+
当测试并发运行时,生命周期钩子的行为会有所不同。`beforeAll` `afterAll` 仍然会为整个组运行一次,但 `beforeEach` `afterEach` 会为每个测试分别运行,而且由于测试本身会重叠执行,它们可能会在同一时间发生。
101100

102-
The hook execution order is controlled by [`sequence.hooks`](/config/sequence#sequence-hooks). With `sequence.hooks: 'parallel'`, hooks are also bounded by the [`maxConcurrency`](/config/maxconcurrency) limit.
101+
钩子的执行顺序由 [`sequence.hooks`](/config/sequence#sequence-hooks) 控制。当 `sequence.hooks` 设置为 `'parallel'` 时,钩子同样受 [`maxConcurrency`](/config/maxconcurrency) 限制。

0 commit comments

Comments
 (0)