Skip to content
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

Draft - measure html size #20004

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion core-libs/setup/ssr/optimized-engine/rendering-cache.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,31 @@ const options: SsrOptimizationOptions = {
defaultSsrOptimizationOptions.shouldCacheRenderingResult,
};

describe('RenderingCache', () => {
fdescribe('RenderingCache', () => {
let renderingCache: RenderingCache;

beforeEach(() => {
renderingCache = new RenderingCache(options);
});

it('should return stored values and measure size', () => {
const testHtml = '<html><head><title>Test</title></head><body><h1>Hello</h1></body></html>';

renderingCache.store('test', null, testHtml);

const storedEntry = renderingCache.get('test');
const htmlSize = storedEntry?.html ? Buffer.byteLength(storedEntry.html, 'utf8') : 0;

console.log(`Test HTML Size: ${htmlSize} bytes`);

expect(storedEntry).toEqual({
err: null,
html: testHtml,
});

expect(htmlSize).toBeGreaterThan(0);
});

it('should create rendering cache instance', () => {
expect(renderingCache).toBeTruthy();
});
Expand Down
16 changes: 15 additions & 1 deletion core-libs/setup/ssr/optimized-engine/rendering-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SsrOptimizationOptions } from './ssr-optimization-options';

export class RenderingCache {
protected renders = new Map<string, RenderingEntry>();
protected usedCacheSize = 0;

constructor(private options?: SsrOptimizationOptions) {}

Expand All @@ -22,26 +23,39 @@ export class RenderingCache {

store(key: string, err?: Error | null, html?: string) {
const entry: RenderingEntry = { err, html };
let htmlSize = 0;

if (this.options?.ttl) {
entry.time = Date.now();
}
if (this.options?.cacheSize) {
if (html) {
htmlSize = Buffer.byteLength(html, 'utf8');
}

this.renders.delete(key);
// if (this.options.cacheSizeKb > this.usedCacheSize) {
if (this.renders.size >= this.options.cacheSize) {
const oldestKey = this.renders.keys().next().value;
if (oldestKey !== undefined) {
const oldestHtmlSize = Buffer.byteLength(
this.renders.get(oldestKey)?.html || '',
'utf8'
);
this.renders.delete(oldestKey);
this.usedCacheSize -= oldestHtmlSize;
}
}
}
// cache only if shouldCacheRenderingResult return true

if (
this.options?.shouldCacheRenderingResult?.({
options: this.options,
entry,
})
) {
this.renders.set(key, entry);
this.usedCacheSize += htmlSize;
}
}

Expand Down
Loading