angular/modules/ssr-benchmarks
Alan Agius 28926ba92c feat(core): introduce BootstrapContext for improved server bootstrapping (#63562)
This commit introduces a number of changes to the server bootstrapping process to make it more robust and less error-prone, especially for concurrent requests.

Previously, the server rendering process relied on a module-level global platform injector. This could lead to issues in server-side rendering environments where multiple requests are processed concurrently, as they could inadvertently share or overwrite the global injector state.

The new approach introduces a `BootstrapContext` that is passed to the `bootstrapApplication` function. This context provides a platform reference that is scoped to the individual request, ensuring that each server-side render has an isolated platform injector. This prevents state leakage between concurrent requests and makes the overall process more reliable.

BREAKING CHANGE:
The server-side bootstrapping process has been changed to eliminate the reliance on a global platform injector.

Before:
```ts
const bootstrap = () => bootstrapApplication(AppComponent, config);
```

After:
```ts
const bootstrap = (context: BootstrapContext) =>
  bootstrapApplication(AppComponent, config, context);
```

A schematic is provided to automatically update `main.server.ts` files to pass the `BootstrapContext` to the `bootstrapApplication` call.

In addition, `getPlatform()` and `destroyPlatform()` will now return `null` and be a no-op respectively when running in a server environment.

PR Close #63562
2025-09-09 10:57:09 -07:00
..
dist refactor(platform-server): Add an ssr benchmark setup. (#57647) 2024-10-04 10:45:22 -07:00
patches build: remove unused patch (#58821) 2024-11-22 14:56:23 +00:00
src feat(core): introduce BootstrapContext for improved server bootstrapping (#63562) 2025-09-09 10:57:09 -07:00
angular.json refactor(platform-server): replace patch by CLI option. (#58256) 2024-10-21 09:09:07 -07:00
BUILD.bazel build: do not run ssr-benchmarks when using //... (#61566) 2025-05-29 14:39:12 -04:00
package.json build: update cross-repo angular dependencies (#62902) 2025-07-31 09:52:50 +00:00
README.md build: use pnpm as the package manager instead of yarn (#62924) 2025-07-31 22:06:27 +00:00
run-benchmark.ts docs: update license URL from angular.io to angular.dev and year of license to 2025 (#59407) 2025-01-09 10:27:54 -05:00
test-data.ts docs: update license URL from angular.io to angular.dev and year of license to 2025 (#59407) 2025-01-09 10:27:54 -05:00
tsconfig.app.json refactor(platform-server): Add an ssr benchmark setup. (#57647) 2024-10-04 10:45:22 -07:00
tsconfig.json refactor(platform-server): Add an ssr benchmark setup. (#57647) 2024-10-04 10:45:22 -07:00
yarn.lock build: remove patch-package dependency (#63126) 2025-08-14 12:59:46 +02:00

Intro

This small benchmark suite is dedicated to mesure & describe how compute time is spent when rendering an application like in SSR.

Struture

  • ./main.ts is the entry point to run the benchmark

  • ./src contains a sample app that exports a render function.

  • This app render a table of variable size, which depends on data (initData())

  • This app is then rendered X numbers of times

  • Individual function calls are measured with startMeasuring()/stopMeasuring() from the core package.

  • If you add a new measure, make sure to add it also to the levels map for it to be represented correctly in the result

Build & run

pnpm bazel run //modules/ssr-benchmarks:run

Running the benchmark in a browser environment

pnpm bazel run //modules/ssr-benchmarks:run_browser

This bazel target will build the benchmark, start a http-server with a html that will load the benckmark script. The benchmark script with this target will have DOM Emulation disabled. The result will be visible in the devtools console.

Note: Due to the CLI adding some polyfills, @angular/build is patched to disable DOM emulation and running server code inside a browser:

  1. removing an import from node:module in polyfills.server.mjs (with tail ...)
  2. removing the import of platform-server/init.

To run create a usable flame chart, prepare a narrowed run (like benchmarkRun(10000, 20);). Then in the performance tab of the devtools, trigger "Record & Reload" to generate a profile.

Deopt Explorer

A target is dedicated to generate a v8 log that can be fed to the Deopt Explorer extension.

  1. Run pnpm bazel run //modules/ssr-benchmarks:run_deopt,
  2. open the project generated at the path after Successfully ran all commands in test directory:,
  3. open the logfile in the extension

Result example

=== table with 10000 rows, with 1000 renders === ┌─────────┬──────────────────────────────────────┬──────────┬──────────┬────────────┬───────────┐ │ (index) │ name │ min │ average │ percentage │ max │ ├─────────┼──────────────────────────────────────┼──────────┼──────────┼────────────┼───────────┤ │ 0 │ ' renderApplication ' │ '77.0ms' │ '86.4ms' │ '100.0%' │ '259.2ms' │ │ 1 │ ' └ createServerPlatform ' │ '0.0ms' │ '0.1ms' │ '0.1%' │ '3.7ms' │ │ 2 │ ' └ bootstrap ' │ '35.9ms' │ '42.6ms' │ '49.3%' │ '138.4ms' │ │ 3 │ ' └ _render ' │ '39.7ms' │ '43.8ms' │ '50.7%' │ '124.9ms' │ │ 4 │ ' └ whenStable ' │ '0.0ms' │ '0.0ms' │ '0.0%' │ '0.0ms' │ │ 5 │ ' └ prepareForHydration ' │ '13.1ms' │ '14.8ms' │ '17.1%' │ '53.4ms' │ │ 6 │ ' └ insertEventRecordScript ' │ '0.0ms' │ '0.0ms' │ '0.0%' │ '0.0ms' │ │ 7 │ ' └ serializeTransferStateFactory' │ '0.0ms' │ '0.0ms' │ '0.0%' │ '0.1ms' │ │ 8 │ ' └ renderToString ' │ '7.3ms' │ '8.9ms' │ '10.3%' │ '41.8ms' │ └─────────┴──────────────────────────────────────┴──────────┴──────────┴────────────┴───────────┘

Note: The max measure is often an outlier of the first few measures, probably before the JIT optimisation happens