Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit b369eea

Browse files
committedMar 21, 2025·
perf(@angular/ssr): optimized request handling performance
This commit refactors request handling logic, leading to significant performance improvements. **Benchmark** | Metric | 19.2.x Branch | Main Branch | After Optimization | Improvement vs 19.2.x | Improvement vs Main | |--------------------|-----------------------|----------------------|----------------------|-----------------------|-----------------------| | Latency (Avg) | 2473.94 ms | 2655.35 ms | 2385.85 ms | ~3.6% | ~10.1% | | Latency (50%) | 2445.67 ms | 2615 ms | 2416.33 ms | ~1.2% | ~7.6% | | Req/Sec (Avg) | 398.32 | 364.54 | 400.12 | ~0.4% | ~9.8% | | Bytes/Sec (Avg) | 8.41 MB | 7.7 MB | 8.45 MB | ~0.5% | ~9.7% | | Total Requests | 13,000 | 12,000 | 13,000 | 0% | ~8.3% | **Test Details:** * **Command:** `npx autocannon -c 100 -d 30 -p 10 http://localhost:<port>` * **Parameters:** * `-c 100`: 100 concurrent connections * `-d 30`: 30 seconds duration * `-p 10`: 10 pipelining factor * **Iterations:** 3 tests were run on each of the Main Branch and the 19.2.x Branch, and 3 tests were run after optimization. Average of each set of tests was used for the comparison. * **Samples:** 30 samples were collected per test run for Req/Bytes counts. The optimized request handling logic (After Optimization) shows significant improvements across all key performance metrics compared to the original logic on the Main Branch. The regression observed in the Main Branch is attributed to the newly added header flushing logic. When compared to the 19.2.x branch, the optimized code delivers similar performance. Latency is slightly reduced, while request throughput (Req/Sec) and data transfer rate (Bytes/Sec) have seen a slight increase. The total number of requests handled remains consistent with the 19.2.x branch.
1 parent 63428f3 commit b369eea

File tree

1 file changed

+10
-7
lines changed
  • packages/angular/ssr/src/utils

1 file changed

+10
-7
lines changed
 

Diff for: ‎packages/angular/ssr/src/utils/ng.ts

+10-7
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,16 @@ export async function renderAngular(
9595
await applicationRef.whenStable();
9696

9797
return {
98-
content: async () => {
99-
try {
100-
return renderInternal(platformRef, applicationRef);
101-
} finally {
102-
await asyncDestroyPlatform(platformRef);
103-
}
104-
},
98+
content: () =>
99+
new Promise<string>((resolve, reject) => {
100+
// Defer rendering to the next event loop iteration to avoid blocking, as most operations in `renderInternal` are synchronous.
101+
setTimeout(() => {
102+
renderInternal(platformRef, applicationRef)
103+
.then(resolve)
104+
.catch(reject)
105+
.finally(() => void asyncDestroyPlatform(platformRef));
106+
}, 0);
107+
}),
105108
};
106109
} catch (error) {
107110
await asyncDestroyPlatform(platformRef);

0 commit comments

Comments
 (0)
Please sign in to comment.