Skip to content

Commit

Permalink
refactor: Compact HTTP cache stats (#28011)
Browse files Browse the repository at this point in the history
  • Loading branch information
zharinov committed Mar 19, 2024
1 parent 6ed22b9 commit 5e02f6e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 49 deletions.
37 changes: 10 additions & 27 deletions lib/util/stats.spec.ts
Expand Up @@ -480,20 +480,15 @@ describe('util/stats', () => {

expect(res).toEqual({
'https://example.com/bar': {
localHits: 0,
localMisses: 1,
localTotal: 1,
remoteHits: 1,
remoteMisses: 1,
remoteTotal: 2,
hit: 1,
miss: 1,
localMiss: 1,
},
'https://example.com/foo': {
localHits: 2,
localMisses: 1,
localTotal: 3,
remoteHits: 0,
remoteMisses: 0,
remoteTotal: 0,
hit: 0,
miss: 0,
localHit: 2,
localMiss: 1,
},
});
});
Expand All @@ -512,21 +507,9 @@ describe('util/stats', () => {
const [data, msg] = logger.logger.debug.mock.calls[0];
expect(msg).toBe('HTTP cache statistics');
expect(data).toEqual({
'https://example.com/bar': {
localHits: 0,
localMisses: 1,
localTotal: 1,
remoteHits: 1,
remoteMisses: 1,
remoteTotal: 2,
},
'https://example.com/foo': {
localHits: 2,
localMisses: 1,
localTotal: 3,
remoteHits: 0,
remoteMisses: 0,
remoteTotal: 0,
'https://example.com': {
'/foo': { hit: 0, localHit: 2, localMiss: 1, miss: 0 },
'/bar': { hit: 1, localMiss: 1, miss: 1 },
},
});
});
Expand Down
59 changes: 37 additions & 22 deletions lib/util/stats.ts
Expand Up @@ -237,16 +237,22 @@ export class HttpStats {
}

interface HttpCacheHostStatsData {
localHits: number;
localMisses: number;
localTotal: number;
remoteHits: number;
remoteMisses: number;
remoteTotal: number;
hit: number;
miss: number;
localHit?: number;
localMiss?: number;
}

type HttpCacheStatsData = Record<string, HttpCacheHostStatsData>;

function sortObject<T>(obj: Record<string, T>): Record<string, T> {
const result: Record<string, T> = {};
for (const key of Object.keys(obj).sort()) {
result[key] = obj[key];
}
return result;
}

export class HttpCacheStats {
static getData(): HttpCacheStatsData {
return memCache.get<HttpCacheStatsData>('http-cache-stats') ?? {};
Expand All @@ -255,12 +261,8 @@ export class HttpCacheStats {
static read(key: string): HttpCacheHostStatsData {
return (
this.getData()?.[key] ?? {
localHits: 0,
localMisses: 0,
localTotal: 0,
remoteHits: 0,
remoteMisses: 0,
remoteTotal: 0,
hit: 0,
miss: 0,
}
);
}
Expand All @@ -287,8 +289,8 @@ export class HttpCacheStats {
if (baseUrl) {
const host = baseUrl;
const stats = HttpCacheStats.read(host);
stats.localHits += 1;
stats.localTotal += 1;
stats.localHit ??= 0;
stats.localHit += 1;
HttpCacheStats.write(host, stats);
}
}
Expand All @@ -298,8 +300,8 @@ export class HttpCacheStats {
if (baseUrl) {
const host = baseUrl;
const stats = HttpCacheStats.read(host);
stats.localMisses += 1;
stats.localTotal += 1;
stats.localMiss ??= 0;
stats.localMiss += 1;
HttpCacheStats.write(host, stats);
}
}
Expand All @@ -309,8 +311,7 @@ export class HttpCacheStats {
if (baseUrl) {
const host = baseUrl;
const stats = HttpCacheStats.read(host);
stats.remoteHits += 1;
stats.remoteTotal += 1;
stats.hit += 1;
HttpCacheStats.write(host, stats);
}
}
Expand All @@ -320,14 +321,28 @@ export class HttpCacheStats {
if (baseUrl) {
const host = baseUrl;
const stats = HttpCacheStats.read(host);
stats.remoteMisses += 1;
stats.remoteTotal += 1;
stats.miss += 1;
HttpCacheStats.write(host, stats);
}
}

static report(): void {
const stats = HttpCacheStats.getData();
logger.debug(stats, 'HTTP cache statistics');
const data = HttpCacheStats.getData();
let report: Record<string, Record<string, HttpCacheHostStatsData>> = {};
for (const [url, stats] of Object.entries(data)) {
const parsedUrl = parseUrl(url);
if (parsedUrl) {
const { origin, pathname } = parsedUrl;
report[origin] ??= {};
report[origin][pathname] = stats;
}
}

for (const [host, hostStats] of Object.entries(report)) {
report[host] = sortObject(hostStats);
}
report = sortObject(report);

logger.debug(report, 'HTTP cache statistics');
}
}

0 comments on commit 5e02f6e

Please sign in to comment.