Commit 1c83150
[Jest] Lazy core mocks (elastic#235003)
Closes elastic#235079
Lazily instantiate core mocks, to relieve memory pressure. This has the
benefit of tests using less memory usage, and thus us being able to run
them in parallel without upsizing our CI workers, and CPU time spent on
garbage collection goes down. Here's how it affects one of the alerting
plugin test files, significant improvements across the board:
```stdout
Benchmark diff: cwd -> f69f92
cwd: [Jest] Lazily instantiate mocks
f69f92: [Agent Builder] Setup doclinks structure (elastic#235804)
alerting-plugin-test
-
cwd f69f92 Δ CI
Duration 6.3s ±1.7% 7.0s ±1.8% 711ms (+11.3%) 95%, +7.9%–+14.8%
CPU Usage 6.5s ±2.0% 7.4s ±2.1% 852ms (+13.1%) 95%, +9.1%–+17.2%
Max RSS 857.90 MB ±1.3% 977.20 MB ±2.4% 119.30 MB (+13.9%) 95%, +10.3%–+17.6%
GC time 190ms ±4.4% 252ms ±3.4% 62ms (+32.6%) 95%, +23.8%–+42.0%
```
### How it works
Core mocks are instantiated using `lazyObject`. This is a helper utility that will be rewritten using a Babel plugin to create annotated getters that will only instantiate the mock when it is accessed.
### Why it helps
Core mocks are eagerly created, and this means that many objects will be allocated. Specifically, jest will keep mocks in a global registry to support `jest.clearAllMocks()`. This means only when a test file completes these functions will be garbage collected. Increased memory consumption has two consequences that are relevant in this context:
- they increase memory pressure, which means garbage collection has to run more frequently to prevent the process from running out of memory
- our CI workers (that run Jest tests) have 16gb of memory. running jest configs in parallel means that there currently is a risk that either the processes themselves run out of memory, or the agent will. if we reduce memory usage, we can run them in parallel.
---------
Co-authored-by: kibanamachine <[email protected]>1 parent 6a2c0d2 commit 1c83150
File tree
208 files changed
+2068
-1270
lines changed- .github
- packages/kbn-babel-preset
- src
- core
- packages
- analytics
- browser-internal
- src
- browser-mocks
- src
- server-internal
- src
- server-mocks
- src
- application/browser-mocks
- src
- base
- browser-mocks
- src
- server-mocks
- src
- chrome/browser-mocks
- src
- custom-branding
- browser-mocks
- server-mocks
- src
- data-streams/server-mocks
- deprecations
- browser-mocks
- src
- server-mocks
- src
- di/mocks
- src
- doc-links/browser-mocks
- src
- elasticsearch
- client-server-mocks
- src
- server-mocks
- src
- environment/server-mocks
- src
- execution-context/browser-mocks
- src
- fatal-errors/browser-mocks
- src
- feature-flags/browser-mocks
- http
- browser-mocks
- src
- context-server-mocks
- src
- resources-server-mocks
- src
- router-server-mocks
- src
- server-mocks
- src
- i18n
- browser-mocks
- src
- server-mocks
- src
- injected-metadata/browser-mocks
- src
- integrations/browser-mocks
- src
- lifecycle
- browser-mocks
- src
- server-mocks
- src
- logging
- browser-mocks
- src
- server-mocks
- src
- metrics
- collectors-server-mocks
- src
- server-mocks
- src
- node/server-mocks
- src
- notifications/browser-mocks
- src
- overlays/browser-mocks
- src
- plugins
- browser-mocks
- src
- server-mocks
- src
- preboot/server-mocks
- src
- pricing
- browser-mocks
- src
- server-mocks
- src
- rendering/browser-mocks
- src
- saved-objects
- api-server-mocks
- src
- base-server-mocks
- src
- import-export-server-mocks
- src
- migration-server-mocks
- src
- server-mocks
- src
- security
- browser-mocks
- src
- server-mocks
- src
- status/server-mocks
- src
- theme/browser-mocks
- src
- ui-settings
- browser-mocks
- src
- server-mocks
- src
- usage-data/server-mocks
- src
- user-profile/browser-mocks
- src
- user-settings/server-mocks
- src
- public
- platform
- packages
- private/kbn-config-mocks
- src
- x-pack
- solutions/observability/plugins/infra/server/lib/alerting/metric_threshold
Some content is hidden
Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
208 files changed
+2068
-1270
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1796 | 1796 | | |
1797 | 1797 | | |
1798 | 1798 | | |
| 1799 | + | |
1799 | 1800 | | |
1800 | 1801 | | |
1801 | 1802 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1587 | 1587 | | |
1588 | 1588 | | |
1589 | 1589 | | |
| 1590 | + | |
1590 | 1591 | | |
1591 | 1592 | | |
1592 | 1593 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | | - | |
19 | 19 | | |
20 | 20 | | |
21 | 21 | | |
| |||
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | | - | |
| 14 | + | |
| 15 | + | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
Lines changed: 7 additions & 6 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | | - | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | | - | |
| 27 | + | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | | - | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | | - | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | | - | |
| 39 | + | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
42 | | - | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
| 16 | + | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
Lines changed: 3 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
| 12 | + | |
12 | 13 | | |
13 | | - | |
| 14 | + | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
| |||
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
23 | | - | |
| 24 | + | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | | - | |
| 18 | + | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
Lines changed: 9 additions & 8 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
| 18 | + | |
18 | 19 | | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
22 | | - | |
| 23 | + | |
23 | 24 | | |
24 | 25 | | |
25 | 26 | | |
26 | 27 | | |
27 | 28 | | |
28 | 29 | | |
29 | 30 | | |
30 | | - | |
| 31 | + | |
31 | 32 | | |
32 | 33 | | |
33 | 34 | | |
34 | | - | |
| 35 | + | |
35 | 36 | | |
36 | 37 | | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
40 | 41 | | |
41 | 42 | | |
42 | | - | |
| 43 | + | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | | - | |
| 47 | + | |
47 | 48 | | |
48 | 49 | | |
49 | 50 | | |
50 | | - | |
| 51 | + | |
51 | 52 | | |
52 | 53 | | |
53 | 54 | | |
54 | | - | |
| 55 | + | |
55 | 56 | | |
56 | 57 | | |
57 | 58 | | |
58 | 59 | | |
59 | | - | |
| 60 | + | |
60 | 61 | | |
61 | 62 | | |
62 | 63 | | |
| |||
0 commit comments