-
Notifications
You must be signed in to change notification settings - Fork 938
Report binary size metrics on GitHub pull requests. #2744
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
5c13ff3
size presubmit check
Feiyang1 c06eea0
add missing semicolons
Feiyang1 1c34983
report github action url
Feiyang1 6266328
Upload size report to the metrics service.
yifanyang b90abcf
Merge branch 'master' into yifany/size
yifanyang 7b129c9
Cosmetic change.
yifanyang 573e4f2
get size without comments and whitespaces for NPM scripts (#2777)
Feiyang1 768ac9f
Merge branch 'master' into yifany/size
yifanyang 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
name: Health Metrics | ||
|
||
on: [push, pull_request] | ||
|
||
jobs: | ||
binary-size-test: | ||
name: Binary Size | ||
runs-on: ubuntu-latest | ||
env: | ||
METRICS_SERVICE_URL: ${{ secrets.METRICS_SERVICE_URL }} | ||
GITHUB_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }} | ||
GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }} | ||
steps: | ||
- uses: actions/checkout@v2 | ||
- uses: actions/setup-node@v1 | ||
with: | ||
node-version: 10.x | ||
- uses: GoogleCloudPlatform/github-actions/setup-gcloud@master | ||
with: | ||
service_account_key: ${{ secrets.GCP_SA_KEY }} | ||
- run: cp config/ci.config.json config/project.json | ||
- run: yarn install | ||
- run: yarn build | ||
|
||
- name: Run health-metrics/binary-size test | ||
run: yarn size-report | ||
|
||
# TODO(yifany): Enable startup times testing on CI. |
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,161 @@ | ||
const { resolve } = require('path'); | ||
const fs = require('fs'); | ||
const { execSync } = require('child_process'); | ||
const https = require('https'); | ||
const terser = require('terser'); | ||
|
||
const repoRoot = resolve(__dirname, '..'); | ||
|
||
const runId = process.env.GITHUB_RUN_ID || 'local-run-id'; | ||
|
||
const METRICS_SERVICE_URL = process.env.METRICS_SERVICE_URL; | ||
|
||
// CDN scripts | ||
function generateReportForCDNScripts() { | ||
const reports = []; | ||
const firebaseRoot = resolve(__dirname, '../packages/firebase'); | ||
const pkgJson = require(`${firebaseRoot}/package.json`); | ||
|
||
const special_files = [ | ||
'firebase-performance-standalone.es2017.js', | ||
'firebase-performance-standalone.js', | ||
'firebase.js' | ||
]; | ||
|
||
const files = [ | ||
...special_files.map(file => `${firebaseRoot}/${file}`), | ||
...pkgJson.components.map(component => `${firebaseRoot}/firebase-${component}.js`) | ||
]; | ||
|
||
for (const file of files) { | ||
const { size } = fs.statSync(file); | ||
const fileName = file.split('/').slice(-1)[0]; | ||
reports.push(makeReportObject('firebase', fileName, size)); | ||
} | ||
|
||
return reports; | ||
} | ||
|
||
// NPM packages | ||
function generateReportForNPMPackages() { | ||
const reports = []; | ||
const fields = [ | ||
'main', | ||
'module', | ||
'esm2017', | ||
'browser', | ||
'react-native', | ||
'lite', | ||
'lite-esm2017' | ||
]; | ||
|
||
const packageInfo = JSON.parse( | ||
execSync('npx lerna ls --json --scope @firebase/*', { cwd: repoRoot }).toString() | ||
); | ||
|
||
for (const package of packageInfo) { | ||
const packageJson = require(`${package.location}/package.json`); | ||
|
||
for (const field of fields) { | ||
if (packageJson[field]) { | ||
const filePath = `${package.location}/${packageJson[field]}`; | ||
|
||
const rawCode = fs.readFileSync(filePath, 'utf-8'); | ||
|
||
// remove comments and whitespaces, then get size | ||
const { code } = terser.minify(rawCode, { | ||
output: { | ||
comments: false | ||
}, | ||
mangle: false, | ||
compress: false | ||
}); | ||
|
||
const size = Buffer.byteLength(code, 'utf-8') | ||
reports.push(makeReportObject(packageJson.name, field, size)); | ||
} | ||
} | ||
} | ||
|
||
return reports; | ||
} | ||
|
||
function makeReportObject(sdk, type, value) { | ||
return { | ||
sdk, | ||
type, | ||
value | ||
}; | ||
} | ||
|
||
function generateSizeReport() { | ||
const reports = [ | ||
...generateReportForCDNScripts(), | ||
...generateReportForNPMPackages() | ||
]; | ||
|
||
for (const r of reports) { | ||
console.log(r.sdk, r.type, r.value); | ||
} | ||
|
||
console.log(`Github Action URL: https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${runId}`); | ||
|
||
return { | ||
log: `https://github.com/${process.env.GITHUB_REPOSITORY}/actions/runs/${runId}`, | ||
metric: "BinarySize", | ||
results: reports | ||
}; | ||
} | ||
|
||
function constructRequestPath() { | ||
const repo = process.env.GITHUB_REPOSITORY; | ||
const commit = process.env.GITHUB_SHA; | ||
let path = `/repos/${repo}/commits/${commit}/reports`; | ||
if (process.env.GITHUB_EVENT_NAME === 'pull_request') { | ||
const pullRequestNumber = process.env.GITHUB_PULL_REQUEST_NUMBER; | ||
const pullRequestBaseSha = process.env.GITHUB_PULL_REQUEST_BASE_SHA; | ||
path += `?pull_request=${pullRequestNumber}&base_commit=${pullRequestBaseSha}`; | ||
} | ||
return path; | ||
} | ||
|
||
function constructRequestOptions(path) { | ||
const accessToken = execSync('gcloud auth print-identity-token', { encoding: 'utf8' }).trim(); | ||
return { | ||
path: path, | ||
method: 'POST', | ||
headers: { | ||
'Authorization': `Bearer ${accessToken}`, | ||
'Content-Type': 'application/json' | ||
} | ||
}; | ||
} | ||
|
||
function upload(report) { | ||
if (!process.env.GITHUB_ACTIONS) { | ||
console.log('Metrics upload is only enabled on CI.'); | ||
return; | ||
} | ||
|
||
const path = constructRequestPath(); | ||
const options = constructRequestOptions(path); | ||
|
||
console.log(`${report.metric} report:`, report); | ||
console.log(`Posting to metrics service endpoint: ${path} ...`); | ||
|
||
const request = https.request(METRICS_SERVICE_URL, options, response => { | ||
response.setEncoding('utf8'); | ||
console.log(`Response status code: ${response.statusCode}`); | ||
response.on('data', console.log); | ||
response.on('end', () => { | ||
if (response.statusCode !== 202) { | ||
process.exit(1); | ||
} | ||
}) | ||
}); | ||
request.write(JSON.stringify(report)); | ||
request.end(); | ||
} | ||
|
||
const report = generateSizeReport(); | ||
upload(report); |
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 |
---|---|---|
|
@@ -13986,19 +13986,10 @@ terser-webpack-plugin@^1.4.3: | |
webpack-sources "^1.4.0" | ||
worker-farm "^1.7.0" | ||
|
||
terser@^4.1.2: | ||
version "4.3.8" | ||
resolved "https://registry.npmjs.org/terser/-/terser-4.3.8.tgz#707f05f3f4c1c70c840e626addfdb1c158a17136" | ||
integrity sha512-otmIRlRVmLChAWsnSFNO0Bfk6YySuBp6G9qrHiJwlLDd4mxe2ta4sjI7TzIR+W1nBMjilzrMcPOz9pSusgx3hQ== | ||
dependencies: | ||
commander "^2.20.0" | ||
source-map "~0.6.1" | ||
source-map-support "~0.5.12" | ||
|
||
terser@^4.6.2: | ||
version "4.6.6" | ||
resolved "https://registry.npmjs.org/terser/-/terser-4.6.6.tgz#da2382e6cafbdf86205e82fb9a115bd664d54863" | ||
integrity sha512-4lYPyeNmstjIIESr/ysHg2vUPRGf2tzF9z2yYwnowXVuVzLEamPN1Gfrz7f8I9uEPuHcbFlW4PLIAsJoxXyJ1g== | ||
[email protected], terser@^4.1.2, terser@^4.6.2: | ||
version "4.6.7" | ||
resolved "https://registry.npmjs.org/terser/-/terser-4.6.7.tgz#478d7f9394ec1907f0e488c5f6a6a9a2bad55e72" | ||
integrity sha512-fmr7M1f7DBly5cX2+rFDvmGBAaaZyPrHYK4mMdHEDAdNTqXSZgSOfqsfGq2HqPGT/1V0foZZuCZFx8CHKgAk3g== | ||
dependencies: | ||
commander "^2.20.0" | ||
source-map "~0.6.1" | ||
|
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.