-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
ci: Capture overhead in node app #17420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
bf6c9b3
ci: Capture overhead in node app
mydea 3e3f585
PR feedback
mydea 2e9e5e4
average it even more...
mydea 4b09665
just two runs?
mydea 0c3c410
fix comments
mydea 6172bc7
add mysql
mydea 3925440
yarn
mydea 1c64a2a
more compute
mydea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
ci: Capture overhead in node app
- Loading branch information
commit bf6c9b3a2b814b86e9225e13cf4873ec6b872a5a
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| module.exports = { | ||
| env: { | ||
| node: true, | ||
| }, | ||
| extends: ['../../.eslintrc.js'], | ||
| overrides: [ | ||
| { | ||
| files: ['**/*.mjs'], | ||
| parserOptions: { | ||
| project: ['tsconfig.json'], | ||
| sourceType: 'module', | ||
| }, | ||
| }, | ||
| ], | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # node-overhead-gh-action | ||
|
|
||
| Capture the overhead of Sentry in a node app. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| name: 'node-overhead-gh-action' | ||
| description: 'Run node overhead comparison' | ||
| inputs: | ||
| github_token: | ||
| required: true | ||
| description: 'a github access token' | ||
| comparison_branch: | ||
| required: false | ||
| default: '' | ||
| description: 'If set, compare the current branch with this branch' | ||
| threshold: | ||
| required: false | ||
| default: '3' | ||
| description: 'The percentage threshold for size changes before posting a comment' | ||
| runs: | ||
| using: 'node24' | ||
| main: 'index.mjs' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,237 @@ | ||
| import { promises as fs } from 'node:fs'; | ||
| import path from 'node:path'; | ||
| import { fileURLToPath } from 'node:url'; | ||
| import { DefaultArtifactClient } from '@actions/artifact'; | ||
| import * as core from '@actions/core'; | ||
| import { exec } from '@actions/exec'; | ||
| import { context, getOctokit } from '@actions/github'; | ||
| import * as glob from '@actions/glob'; | ||
| import * as io from '@actions/io'; | ||
| import { markdownTable } from 'markdown-table'; | ||
| import { getArtifactsForBranchAndWorkflow } from './lib/getArtifactsForBranchAndWorkflow.mjs'; | ||
| import { getOverheadMeasurements } from './lib/getOverheadMeasurements.mjs'; | ||
| import { formatResults, hasChanges } from './lib/markdown-table-formatter.mjs'; | ||
|
|
||
| const NODE_OVERHEAD_HEADING = '## node-overhead report 🧳'; | ||
| const ARTIFACT_NAME = 'node-overhead-action'; | ||
| const RESULTS_FILE = 'node-overhead-results.json'; | ||
|
|
||
| function getResultsFilePath() { | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| return path.resolve(__dirname, RESULTS_FILE); | ||
| } | ||
|
|
||
| const { getInput, setFailed } = core; | ||
|
|
||
| async function fetchPreviousComment(octokit, repo, pr) { | ||
| const { data: commentList } = await octokit.rest.issues.listComments({ | ||
| ...repo, | ||
| issue_number: pr.number, | ||
| }); | ||
|
|
||
| const sizeLimitComment = commentList.find(comment => comment.body.startsWith(NODE_OVERHEAD_HEADING)); | ||
| return !sizeLimitComment ? null : sizeLimitComment; | ||
| } | ||
|
|
||
| async function run() { | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| try { | ||
| const { payload, repo } = context; | ||
| const pr = payload.pull_request; | ||
|
|
||
| const comparisonBranch = getInput('comparison_branch'); | ||
| const githubToken = getInput('github_token'); | ||
| const threshold = getInput('threshold') || 1; | ||
|
|
||
| if (comparisonBranch && !pr) { | ||
| throw new Error('No PR found. Only pull_request workflows are supported.'); | ||
| } | ||
|
|
||
| const octokit = getOctokit(githubToken); | ||
| const resultsFilePath = getResultsFilePath(); | ||
|
|
||
| // If we have no comparison branch, we just run size limit & store the result as artifact | ||
| if (!comparisonBranch) { | ||
| return runNodeOverheadOnComparisonBranch(); | ||
| } | ||
|
|
||
| // Else, we run size limit for the current branch, AND fetch it for the comparison branch | ||
mydea marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let base; | ||
| let current; | ||
| let baseIsNotLatest = false; | ||
| let baseWorkflowRun; | ||
|
|
||
| try { | ||
| const workflowName = `${process.env.GITHUB_WORKFLOW || ''}`; | ||
| core.startGroup(`getArtifactsForBranchAndWorkflow - workflow:"${workflowName}", branch:"${comparisonBranch}"`); | ||
| const artifacts = await getArtifactsForBranchAndWorkflow(octokit, { | ||
| ...repo, | ||
| artifactName: ARTIFACT_NAME, | ||
| branch: comparisonBranch, | ||
| workflowName, | ||
| }); | ||
| core.endGroup(); | ||
|
|
||
| if (!artifacts) { | ||
| throw new Error('No artifacts found'); | ||
| } | ||
|
|
||
| baseWorkflowRun = artifacts.workflowRun; | ||
|
|
||
| await downloadOtherWorkflowArtifact(octokit, { | ||
| ...repo, | ||
| artifactName: ARTIFACT_NAME, | ||
| artifactId: artifacts.artifact.id, | ||
| downloadPath: __dirname, | ||
| }); | ||
|
|
||
| base = JSON.parse(await fs.readFile(resultsFilePath, { encoding: 'utf8' })); | ||
|
|
||
| if (!artifacts.isLatest) { | ||
| baseIsNotLatest = true; | ||
| core.info('Base artifact is not the latest one. This may lead to incorrect results.'); | ||
| } | ||
| } catch (error) { | ||
| core.startGroup('Warning, unable to find base results'); | ||
| core.error(error); | ||
| core.endGroup(); | ||
| } | ||
|
|
||
| core.startGroup('Getting current overhead measurements'); | ||
| try { | ||
| current = await getOverheadMeasurements(); | ||
| } catch (error) { | ||
| core.error('Error getting current overhead measurements'); | ||
| core.endGroup(); | ||
| throw error; | ||
| } | ||
| core.debug(`Current overhead measurements: ${JSON.stringify(current, null, 2)}`); | ||
| core.endGroup(); | ||
|
|
||
| const thresholdNumber = Number(threshold); | ||
|
|
||
| const nodeOverheadComment = await fetchPreviousComment(octokit, repo, pr); | ||
|
|
||
| if (nodeOverheadComment) { | ||
| core.debug('Found existing node overhead comment, updating it instead of creating a new one...'); | ||
| } | ||
|
|
||
| const shouldComment = isNaN(thresholdNumber) || hasChanges(base, current, thresholdNumber) || nodeOverheadComment; | ||
|
|
||
| if (shouldComment) { | ||
| const bodyParts = [ | ||
| NODE_OVERHEAD_HEADING, | ||
| 'Note: This is a synthetic benchmark with a minimal express app and does not necessarily reflect the real-world performance impact in an application.', | ||
| ]; | ||
|
|
||
| if (baseIsNotLatest) { | ||
| bodyParts.push( | ||
| '⚠️ **Warning:** Base artifact is not the latest one, because the latest workflow run is not done yet. This may lead to incorrect results. Try to re-run all tests to get up to date results.', | ||
| ); | ||
| } | ||
| try { | ||
| bodyParts.push(markdownTable(formatResults(base, current))); | ||
| } catch (error) { | ||
| core.error('Error generating markdown table'); | ||
| throw error; | ||
| } | ||
|
|
||
| if (baseWorkflowRun) { | ||
| bodyParts.push(''); | ||
| bodyParts.push(`[View base workflow run](${baseWorkflowRun.html_url})`); | ||
| } | ||
|
|
||
| const body = bodyParts.join('\r\n'); | ||
|
|
||
| try { | ||
| if (!nodeOverheadComment) { | ||
| await octokit.rest.issues.createComment({ | ||
| ...repo, | ||
| issue_number: pr.number, | ||
| body, | ||
| }); | ||
| } else { | ||
| await octokit.rest.issues.updateComment({ | ||
| ...repo, | ||
| comment_id: nodeOverheadComment.id, | ||
| body, | ||
| }); | ||
| } | ||
| } catch (error) { | ||
| core.error( | ||
| "Error updating comment. This can happen for PR's originating from a fork without write permissions.", | ||
| ); | ||
| } | ||
| } else { | ||
| core.debug('Skipping comment because there are no changes.'); | ||
| } | ||
| } catch (error) { | ||
| core.error(error); | ||
| setFailed(error.message); | ||
| } | ||
| } | ||
|
|
||
| async function runNodeOverheadOnComparisonBranch() { | ||
| const __dirname = path.dirname(fileURLToPath(import.meta.url)); | ||
| const resultsFilePath = getResultsFilePath(); | ||
|
|
||
| const artifactClient = new DefaultArtifactClient(); | ||
|
|
||
| const result = await getOverheadMeasurements(); | ||
|
|
||
| try { | ||
| await fs.writeFile(resultsFilePath, JSON.stringify(result), 'utf8'); | ||
| } catch (error) { | ||
| core.error('Error parsing node overhead output. The output should be a json.'); | ||
| throw error; | ||
| } | ||
|
|
||
| const globber = await glob.create(resultsFilePath, { | ||
| followSymbolicLinks: false, | ||
| }); | ||
| const files = await globber.glob(); | ||
|
|
||
| await artifactClient.uploadArtifact(ARTIFACT_NAME, files, __dirname); | ||
| } | ||
|
|
||
| run(); | ||
|
|
||
| /** | ||
| * Use GitHub API to fetch artifact download url, then | ||
| * download and extract artifact to `downloadPath` | ||
| */ | ||
| async function downloadOtherWorkflowArtifact(octokit, { owner, repo, artifactId, artifactName, downloadPath }) { | ||
| const artifact = await octokit.rest.actions.downloadArtifact({ | ||
| owner, | ||
| repo, | ||
| artifact_id: artifactId, | ||
| archive_format: 'zip', | ||
| }); | ||
|
|
||
| // Make sure output path exists | ||
| try { | ||
| await io.mkdirP(downloadPath); | ||
| } catch { | ||
| // ignore errors | ||
| } | ||
|
|
||
| const downloadFile = path.resolve(downloadPath, `${artifactName}.zip`); | ||
|
|
||
| await exec('wget', [ | ||
| '-nv', | ||
| '--retry-connrefused', | ||
| '--waitretry=1', | ||
| '--read-timeout=20', | ||
| '--timeout=15', | ||
| '-t', | ||
| '0', | ||
| '-O', | ||
| downloadFile, | ||
| artifact.url, | ||
| ]); | ||
|
|
||
| await exec('unzip', ['-q', '-d', downloadPath, downloadFile], { | ||
| silent: true, | ||
| }); | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.