Skip to content

Commit

Permalink
refactor-stats
Browse files Browse the repository at this point in the history
  • Loading branch information
feedthejim committed Oct 11, 2023
1 parent 56fe014 commit 14cee98
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
39 changes: 25 additions & 14 deletions .github/actions/next-stats-action/src/add-comment.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const shortenLabel = (itemKey) =>
: itemKey

const twoMB = 2 * 1024 * 1024
const ONE_HUNDRED_BYTES = 100
const ONE_HUNDRED_MS = 100

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

if (
!itemKey.startsWith('buildDuration') ||
(isBenchmark && itemKey.match(/req\/sec/))
) {
if (typeof mainItemVal === 'number') mainRepoTotal += mainItemVal
if (typeof diffItemVal === 'number') diffRepoTotal += diffItemVal
}

// calculate the change
if (mainItemVal !== diffItemVal) {
if (
typeof mainItemVal === 'number' &&
typeof diffItemVal === 'number'
) {
change = round(diffItemVal - mainItemVal, 2)
const roundedValue = round(diffItemVal - mainItemVal, 2)

// check if there is still a change after rounding
if (change !== 0) {
if (
roundedValue !== 0 &&
((prettyType === 'ms' && roundedValue > ONE_HUNDRED_MS) ||
(prettyType === 'bytes' && roundedValue > ONE_HUNDRED_BYTES))
) {
change = roundedValue
const absChange = Math.abs(change)
const warnIfNegative = isBenchmark && itemKey.match(/req\/sec/)
const warn = warnIfNegative
Expand All @@ -112,12 +111,22 @@ module.exports = async function addComment(
change = `${warn}${change < 0 ? '-' : '+'}${
useRawValue ? absChange : prettify(absChange, prettyType)
}`
} else {
change = 'N/A'
}
} else {
change = 'N/A'
}
}

if (
(change !== 'N/A' && !itemKey.startsWith('buildDuration')) ||
(isBenchmark && itemKey.match(/req\/sec/))
) {
if (typeof mainItemVal === 'number') mainRepoTotal += mainItemVal
if (typeof diffItemVal === 'number') diffRepoTotal += diffItemVal
}

groupTable += `| ${
isBenchmark ? itemKey : shortenLabel(itemKey)
} | ${mainItemStr} | ${diffItemStr} | ${change} |\n`
Expand Down Expand Up @@ -169,8 +178,7 @@ module.exports = async function addComment(

// add diffs
if (result.diffs) {
const diffHeading = '#### Diffs\n'
let diffContent = diffHeading
let diffContent = ''

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

if (diffContent !== diffHeading) {
if (diffContent.length > 0) {
resultContent += `<details>\n`
resultContent += `<summary><strong>Diff details</strong></summary>\n\n`
resultContent += diffContent
resultContent += `\n</details>\n\n`
}
}
let increaseDecreaseNote = ''
Expand All @@ -199,7 +210,7 @@ module.exports = async function addComment(
increaseDecreaseNote = ' (Decrease detected ✓)'
}

comment += `<details>\n`
comment += `<details ${results.length === 1 ? 'open' : ''}>\n`
comment += `<summary><strong>${result.title}</strong>${increaseDecreaseNote}</summary>\n\n<br/>\n\n`
comment += resultContent
comment += '</details>\n'
Expand Down
47 changes: 46 additions & 1 deletion .github/actions/next-stats-action/src/run/collect-diffs.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ module.exports = async function collectDiffs(
`cd ${diffingDir} && git diff --minimal HEAD ${file}`
)
stdout = (stdout.split(file).pop() || '').trim()
if (stdout.length > 0) {
if (stdout.length > 0 && !isLikelyHashOrIDChange(stdout)) {
diffs[fileKey] = stdout
}
} catch (err) {
Expand All @@ -114,3 +114,48 @@ module.exports = async function collectDiffs(
}
return diffs
}

function isLikelyHashOrIDChange(diff) {
const lines = diff.split('\n')
let additions = []
let deletions = []

// Separate additions and deletions
for (const line of lines) {
if (line.startsWith('+')) {
additions.push(line.substring(1).split(/\b/))
} else if (line.startsWith('-')) {
deletions.push(line.substring(1).split(/\b/))
}
}

// If the number of additions and deletions is different, it's not a hash or ID change
if (additions.length !== deletions.length) {
return false
}

// Compare each addition with each deletion
for (let i = 0; i < additions.length; i++) {
const additionTokens = additions[i]
const deletionTokens = deletions[i]

// Identify differing tokens
const differingTokens = additionTokens.filter(
(token, index) => token !== deletionTokens[index]
)

// Analyze differing tokens
for (const token of differingTokens) {
const isLikelyHash = /^[a-f0-9]+$/.test(token)
const isLikelyID = /^[0-9]+$/.test(token)
// this is most likely noise because some path include the repo name, which can be main or diff
const isLikelyNoise = ['main', 'diff'].includes(token)

if (!isLikelyHash && !isLikelyID && !isLikelyNoise) {
return false
}
}
}

return true
}
4 changes: 4 additions & 0 deletions test/.stats-app/stats-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ const clientGlobs = [
'.next/server/edge-runtime-webpack.js',
],
},
{
name: 'Rendering runtimes',
globs: ['node_modules/next/dist/compiled/next-server/**/*.js'],
},
]

const renames = [
Expand Down

0 comments on commit 14cee98

Please sign in to comment.