From 0904855001b2f2d8fa8cfd764c1c03e7c175feaa Mon Sep 17 00:00:00 2001 From: syuilo <4439005+syuilo@users.noreply.github.com> Date: Thu, 25 Jun 2026 10:14:08 +0900 Subject: [PATCH] chore(dev): tweak backend-memory-report --- .github/scripts/backend-memory-report.mjs | 50 +++++++++++++++++------ 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/.github/scripts/backend-memory-report.mjs b/.github/scripts/backend-memory-report.mjs index 3a9b2fdddb..b6d48a5bef 100644 --- a/.github/scripts/backend-memory-report.mjs +++ b/.github/scripts/backend-memory-report.mjs @@ -296,16 +296,24 @@ function getHeapSnapshotBreakdownEntries(report, phase, category) { .toSorted((a, b) => b[1] - a[1]); } +const heapSnapshotSankeyChildMinRatio = 0.3; +const heapSnapshotSankeyParentMinPercent = 10; + function escapeCsvValue(value) { return `"${String(value).replaceAll('"', '""')}"`; } -function formatSankeyValue(value) { +function formatSankeyPercentValue(value) { const rounded = Math.round(value * 100) / 100; + if (rounded === 0 && value > 0) return '0.01'; if (Number.isInteger(rounded)) return String(rounded); return rounded.toFixed(2).replace(/0+$/, '').replace(/\.$/, ''); } +function formatHeapSnapshotSankeyChildLabel(label) { + return String(label).replace(/^[^:]+:\s*/, ''); +} + function renderHeapSnapshotSankey(report, phase, title) { const total = getHeapSnapshotCategoryValue(report, phase, 'Total'); if (total == null || total <= 0) return null; @@ -317,11 +325,30 @@ function renderHeapSnapshotSankey(report, phase, title) { if (value == null || value <= 0) return null; const breakdownEntries = getHeapSnapshotBreakdownEntries(report, phase, category); const breakdownTotal = breakdownEntries.reduce((sum, [, childValue]) => sum + childValue, 0); + const percent = (value * 100) / total; + const childEntries = []; + let otherPercent = 0; + + if (breakdownTotal > 0 && percent > heapSnapshotSankeyParentMinPercent) { + for (const [childName, childValue] of breakdownEntries) { + const childRatio = childValue / breakdownTotal; + const childPercent = percent * childRatio; + if (childRatio >= heapSnapshotSankeyChildMinRatio) { + childEntries.push([formatHeapSnapshotSankeyChildLabel(childName), childPercent]); + } else { + otherPercent += childPercent; + } + } + + if (childEntries.length > 0 && otherPercent > 0) { + childEntries.push(['Other', otherPercent]); + } + } + return { category, - value, - breakdownTotal, - breakdownEntries, + percent, + childEntries, }; }) .filter(value => value != null); @@ -331,12 +358,12 @@ function renderHeapSnapshotSankey(report, phase, title) { const nodeColors = { [title]: heapSnapshotCategoriesColorsHex.Total, }; - for (const { category, breakdownEntries } of categories) { + for (const { category, childEntries } of categories) { const categoryColor = heapSnapshotCategoriesColorsHex[category] ?? heapSnapshotCategoriesColorsHex.Total; nodeColors[category] = categoryColor; - for (const [childName] of breakdownEntries) { - nodeColors[`${category}: ${childName}`] = categoryColor; + for (const [childName] of childEntries) { + nodeColors[childName] = categoryColor; } } @@ -354,12 +381,11 @@ function renderHeapSnapshotSankey(report, phase, title) { 'sankey-beta', ]; - for (const { category, value, breakdownTotal, breakdownEntries } of categories) { - lines.push(`${escapeCsvValue(title)},${escapeCsvValue(category)},${formatSankeyValue(value)}`); + for (const { category, percent, childEntries } of categories) { + lines.push(`${escapeCsvValue(title)},${escapeCsvValue(category)},${formatSankeyPercentValue(percent)}`); - for (const [childName, childValue] of breakdownEntries) { - const normalizedValue = breakdownTotal > 0 ? childValue * value / breakdownTotal : childValue; - lines.push(`${escapeCsvValue(category)},${escapeCsvValue(`${category}: ${childName}`)},${formatSankeyValue(normalizedValue)}`); + for (const [childName, childPercent] of childEntries) { + lines.push(`${escapeCsvValue(category)},${escapeCsvValue(childName)},${formatSankeyPercentValue(childPercent)}`); } }