Skip to content

Commit 35a9923

Browse files
authored
misc: tweak stats github action (#56694)
This PR adds a few tweaks to our Github actions PR: - expand by default the main section - add some heuristics to not show hash/id changes in the diff view - add some heuristics to not show increase/decrease size when it's too small (100 bytes is noise most often) - add rendering runtimes to the list of tracked files after vs before ![CleanShot 2023-10-11 at 14 56 36@2x](https://github.com/vercel/next.js/assets/11064311/548781bc-e893-45d1-aca4-de73b2b30299) ![CleanShot 2023-10-11 at 14 57 11@2x](https://github.com/vercel/next.js/assets/11064311/8e1c77e3-bed5-46ec-a756-62496df6729e)
1 parent c1a1583 commit 35a9923

File tree

3 files changed

+75
-15
lines changed

3 files changed

+75
-15
lines changed

.github/actions/next-stats-action/src/add-comment.js

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ const shortenLabel = (itemKey) =>
2424
: itemKey
2525

2626
const twoMB = 2 * 1024 * 1024
27+
const ONE_HUNDRED_BYTES = 100
28+
const ONE_HUNDRED_MS = 100
2729

2830
module.exports = async function addComment(
2931
results = [],
@@ -82,24 +84,21 @@ module.exports = async function addComment(
8284
// otherwise only show gzip values
8385
else if (!isGzipItem && !groupKey.match(gzipIgnoreRegex)) return
8486

85-
if (
86-
!itemKey.startsWith('buildDuration') ||
87-
(isBenchmark && itemKey.match(/req\/sec/))
88-
) {
89-
if (typeof mainItemVal === 'number') mainRepoTotal += mainItemVal
90-
if (typeof diffItemVal === 'number') diffRepoTotal += diffItemVal
91-
}
92-
9387
// calculate the change
9488
if (mainItemVal !== diffItemVal) {
9589
if (
9690
typeof mainItemVal === 'number' &&
9791
typeof diffItemVal === 'number'
9892
) {
99-
change = round(diffItemVal - mainItemVal, 2)
93+
const roundedValue = round(diffItemVal - mainItemVal, 2)
10094

10195
// check if there is still a change after rounding
102-
if (change !== 0) {
96+
if (
97+
roundedValue !== 0 &&
98+
((prettyType === 'ms' && roundedValue > ONE_HUNDRED_MS) ||
99+
(prettyType === 'bytes' && roundedValue > ONE_HUNDRED_BYTES))
100+
) {
101+
change = roundedValue
103102
const absChange = Math.abs(change)
104103
const warnIfNegative = isBenchmark && itemKey.match(/req\/sec/)
105104
const warn = warnIfNegative
@@ -112,12 +111,22 @@ module.exports = async function addComment(
112111
change = `${warn}${change < 0 ? '-' : '+'}${
113112
useRawValue ? absChange : prettify(absChange, prettyType)
114113
}`
114+
} else {
115+
change = 'N/A'
115116
}
116117
} else {
117118
change = 'N/A'
118119
}
119120
}
120121

122+
if (
123+
(change !== 'N/A' && !itemKey.startsWith('buildDuration')) ||
124+
(isBenchmark && itemKey.match(/req\/sec/))
125+
) {
126+
if (typeof mainItemVal === 'number') mainRepoTotal += mainItemVal
127+
if (typeof diffItemVal === 'number') diffRepoTotal += diffItemVal
128+
}
129+
121130
groupTable += `| ${
122131
isBenchmark ? itemKey : shortenLabel(itemKey)
123132
} | ${mainItemStr} | ${diffItemStr} | ${change} |\n`
@@ -169,8 +178,7 @@ module.exports = async function addComment(
169178

170179
// add diffs
171180
if (result.diffs) {
172-
const diffHeading = '#### Diffs\n'
173-
let diffContent = diffHeading
181+
let diffContent = ''
174182

175183
Object.keys(result.diffs).forEach((itemKey) => {
176184
const curDiff = result.diffs[itemKey]
@@ -187,8 +195,11 @@ module.exports = async function addComment(
187195
diffContent += `\n</details>\n`
188196
})
189197

190-
if (diffContent !== diffHeading) {
198+
if (diffContent.length > 0) {
199+
resultContent += `<details>\n`
200+
resultContent += `<summary><strong>Diff details</strong></summary>\n\n`
191201
resultContent += diffContent
202+
resultContent += `\n</details>\n\n`
192203
}
193204
}
194205
let increaseDecreaseNote = ''
@@ -199,7 +210,7 @@ module.exports = async function addComment(
199210
increaseDecreaseNote = ' (Decrease detected ✓)'
200211
}
201212

202-
comment += `<details>\n`
213+
comment += `<details open>\n`
203214
comment += `<summary><strong>${result.title}</strong>${increaseDecreaseNote}</summary>\n\n<br/>\n\n`
204215
comment += resultContent
205216
comment += '</details>\n'

.github/actions/next-stats-action/src/run/collect-diffs.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ module.exports = async function collectDiffs(
106106
`cd ${diffingDir} && git diff --minimal HEAD ${file}`
107107
)
108108
stdout = (stdout.split(file).pop() || '').trim()
109-
if (stdout.length > 0) {
109+
if (stdout.length > 0 && !isLikelyHashOrIDChange(stdout)) {
110110
diffs[fileKey] = stdout
111111
}
112112
} catch (err) {
@@ -117,3 +117,48 @@ module.exports = async function collectDiffs(
117117
}
118118
return diffs
119119
}
120+
121+
function isLikelyHashOrIDChange(diff) {
122+
const lines = diff.split('\n')
123+
let additions = []
124+
let deletions = []
125+
126+
// Separate additions and deletions
127+
for (const line of lines) {
128+
if (line.startsWith('+')) {
129+
additions.push(line.substring(1).split(/\b/))
130+
} else if (line.startsWith('-')) {
131+
deletions.push(line.substring(1).split(/\b/))
132+
}
133+
}
134+
135+
// If the number of additions and deletions is different, it's not a hash or ID change
136+
if (additions.length !== deletions.length) {
137+
return false
138+
}
139+
140+
// Compare each addition with each deletion
141+
for (let i = 0; i < additions.length; i++) {
142+
const additionTokens = additions[i]
143+
const deletionTokens = deletions[i]
144+
145+
// Identify differing tokens
146+
const differingTokens = additionTokens.filter(
147+
(token, index) => token !== deletionTokens[index]
148+
)
149+
150+
// Analyze differing tokens
151+
for (const token of differingTokens) {
152+
const isLikelyHash = /^[a-f0-9]+$/.test(token)
153+
const isLikelyID = /^[0-9]+$/.test(token)
154+
// this is most likely noise because some path include the repo name, which can be main or diff
155+
const isLikelyNoise = ['main', 'diff'].includes(token)
156+
157+
if (!isLikelyHash && !isLikelyID && !isLikelyNoise) {
158+
return false
159+
}
160+
}
161+
}
162+
163+
return true
164+
}

test/.stats-app/stats-config.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ const clientGlobs = [
3636
'.next/server/edge-runtime-webpack.js',
3737
],
3838
},
39+
{
40+
name: 'Next Runtimes',
41+
globs: ['../../node_modules/next/dist/compiled/next-server/**/*.js'],
42+
},
3943
]
4044

4145
const renames = [

0 commit comments

Comments
 (0)