Skip to content

Commit 80145e4

Browse files
committed
refactor-stats
1 parent 56fe014 commit 80145e4

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

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: 'Rendering runtimes',
41+
globs: ['../../node_modules/next/dist/compiled/next-server/**/*.js'],
42+
},
3943
]
4044

4145
const renames = [

0 commit comments

Comments
 (0)