-
Notifications
You must be signed in to change notification settings - Fork 28
perf(test): disable Lambda bundling in CDK unit-test synths (~15x per synth) (#366) #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+153
−0
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6cf7a98
perf(test): disable Lambda bundling in CDK unit-test synths (#366)
scottschreckengaust 82831aa
docs(test): correct bundling opt-out to postCliContext (#366)
scottschreckengaust c1872c0
docs(test): clarify bundling opt-out is for asset output, not synth (…
scottschreckengaust 95ebf79
docs(test): drop dead CI_BUILD_PERFORMANCE.md links (#366)
scottschreckengaust ce523fa
Merge branch 'main' into feat/366-synth-reuse
scottschreckengaust 94c604e
fix(test): harden bundling-disable against ambient CDK_CONTEXT_JSON (…
scottschreckengaust 09b1f24
test(cdk): add executable guard that Lambda bundling is disabled (#366)
scottschreckengaust 77b0c7a
test(cdk): cover the postCliContext bundling opt-out precedence (#366)
scottschreckengaust 3f6ff1f
docs(test): reframe bundling as overhead-plus-smoke-test, not "pure o…
scottschreckengaust File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * MIT No Attribution | ||
| * | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| * the Software without restriction, including without limitation the rights to | ||
| * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| * the Software, and to permit persons to whom the Software is furnished to do so. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| // Executable guard for the global Lambda-bundling disable (#366). | ||
| // | ||
| // The speedup in this package depends entirely on `test/setup/disable-bundling.ts` | ||
| // running as a Jest `setupFiles` entry. Until now the only regression guards | ||
| // were prose in AGENTS.md and a checklist item in review_pr.md — both | ||
| // honor-system. If someone drops the `setupFiles` wiring, reorders Jest setup, | ||
| // or a CDK rename breaks the context key, the suite silently reverts to a | ||
| // full-bundling (~15× slower) synth with no failing check. These assertions | ||
| // make that floor machine-enforced. | ||
| import { App, Stack } from 'aws-cdk-lib'; | ||
|
|
||
| describe('global Lambda bundling disable', () => { | ||
| it('sets aws:cdk:bundling-stacks to [] in the synth context', () => { | ||
| // A bare `new App()` reads CDK_CONTEXT_JSON, which setupFiles populated. | ||
| expect(new App().node.tryGetContext('aws:cdk:bundling-stacks')).toEqual([]); | ||
| }); | ||
|
|
||
| it('makes a bare-App stack report bundlingRequired === false', () => { | ||
| const stack = new Stack(new App(), 'BundlingProbe', { | ||
| env: { account: '123456789012', region: 'us-east-1' }, | ||
| }); | ||
| expect(stack.bundlingRequired).toBe(false); | ||
| }); | ||
| }); | ||
|
|
||
| describe('per-test bundling opt-out (#366)', () => { | ||
| // The documented escape hatch for a test that needs real bundled-asset output | ||
| // is `postCliContext`, NOT constructor `context`. CDK's | ||
| // `App.loadContext(props.context, props.postCliContext)` applies | ||
| // `props.context` first, overwrites it with CDK_CONTEXT_JSON (which the global | ||
| // disable sets), then applies `postCliContext` last — so only the latter wins. | ||
| // These lock that precedence contract against a future CDK reordering; without | ||
| // them the one supported opt-out path is unguarded. | ||
| it('re-enables bundling via postCliContext (the documented opt-out)', () => { | ||
| const app = new App({ postCliContext: { 'aws:cdk:bundling-stacks': ['**'] } }); | ||
| const stack = new Stack(app, 'OptOutProbe', { | ||
| env: { account: '123456789012', region: 'us-east-1' }, | ||
| }); | ||
| expect(stack.bundlingRequired).toBe(true); | ||
| }); | ||
|
|
||
| it('does NOT re-enable bundling via constructor context (env var clobbers it)', () => { | ||
| const app = new App({ context: { 'aws:cdk:bundling-stacks': ['**'] } }); | ||
| const stack = new Stack(app, 'ContextOptOutProbe', { | ||
| env: { account: '123456789012', region: 'us-east-1' }, | ||
| }); | ||
| expect(stack.bundlingRequired).toBe(false); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /** | ||
| * MIT No Attribution | ||
| * | ||
| * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| * | ||
| * Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
| * the Software without restriction, including without limitation the rights to | ||
| * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | ||
| * the Software, and to permit persons to whom the Software is furnished to do so. | ||
| * | ||
| * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
| * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
| * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
| * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
| * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
| * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
| * SOFTWARE. | ||
| */ | ||
|
|
||
| // Disable Lambda asset bundling during unit-test synthesis. | ||
| // | ||
| // `Template.fromStack()` triggers a full CDK synth, which bundles every | ||
| // NodejsFunction via esbuild — ~28s for the AgentStack (413 resources). Unit | ||
| // tests assert on CloudFormation structure/properties, not bundled Lambda code, | ||
| // so for those assertions bundling is overhead — plus a smoke-test of Lambda | ||
| // `entry` resolution and handler compilation that unit tests intentionally cede | ||
| // to the `agent/` build (real synth/deploy still bundles). Skipping it cuts a | ||
| // single synth ~15× (~28.7s -> ~1.9s). See #366. | ||
| // | ||
| // `aws:cdk:bundling-stacks: []` tells the CLI/synth to bundle no stacks. CDK | ||
| // reads CDK_CONTEXT_JSON when an `App` is constructed, so a bare `new App()` | ||
| // (the overwhelming-majority pattern in our tests) picks this up with no | ||
| // call-site changes. | ||
| // | ||
| // Precedence matters for the opt-out. CDK's `App.loadContext(props.context, | ||
| // props.postCliContext)` applies `props.context` FIRST, then overwrites it with | ||
| // CDK_CONTEXT_JSON, then applies `postCliContext` LAST. So for this key the env | ||
| // var beats constructor `context` — `new App({ context: { 'aws:cdk:bundling- | ||
| // stacks': ['**'] } })` does NOT re-enable bundling (the env var clobbers it). | ||
| // | ||
| // This does NOT stop tests from synthesizing — `Template.fromStack()` still | ||
| // runs a full synth; it only skips the esbuild asset-bundling step within that | ||
| // synth. The opt-out below exists solely for the rare test that needs the | ||
| // *bundled-asset output itself* (e.g. asserting on a real asset hash / S3 key), | ||
| // where an unbundled synth would silently yield a placeholder value. Such a | ||
| // test must opt out via `postCliContext` (which wins over the env var), | ||
| // constructing its `App` with | ||
| // `new App({ postCliContext: { 'aws:cdk:bundling-stacks': ['**'] } })` | ||
| // (or the specific stack id). | ||
| const BUNDLING_DISABLED_CONTEXT = { 'aws:cdk:bundling-stacks': [] as string[] }; | ||
|
|
||
| const existing = process.env.CDK_CONTEXT_JSON; | ||
| if (existing) { | ||
| // Preserve any ambient context, but let the bundling disable win | ||
| // unconditionally: a pre-set CDK_CONTEXT_JSON that carries | ||
| // `aws:cdk:bundling-stacks` must not silently re-enable bundling and defeat | ||
| // the speedup. The documented per-test opt-out is `postCliContext` (applied | ||
| // AFTER the env var regardless — see above), so spreading our key LAST keeps | ||
| // that escape hatch working while making the global disable robust to | ||
| // whatever the environment already set. | ||
| let ambient: Record<string, unknown>; | ||
| try { | ||
| ambient = JSON.parse(existing); | ||
| } catch { | ||
| // Without this guard a malformed pre-set var throws here, inside | ||
| // `setupFiles`, and fails EVERY test in EVERY file with an opaque parse | ||
| // error. Re-throw with the offending value so the blame is localized. | ||
| throw new Error(`malformed CDK_CONTEXT_JSON in environment (must be valid JSON): ${existing}`); | ||
| } | ||
| const merged = { ...ambient, ...BUNDLING_DISABLED_CONTEXT }; | ||
| process.env.CDK_CONTEXT_JSON = JSON.stringify(merged); | ||
| } else { | ||
| process.env.CDK_CONTEXT_JSON = JSON.stringify(BUNDLING_DISABLED_CONTEXT); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.