Skip to content

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 8 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .github/workflows/health-metrics-test.yml
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.
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
"docgen:node": "node scripts/docgen/generate-docs.js --api node",
"docgen": "yarn docgen:js; yarn docgen:node",
"prettier": "prettier --config .prettierrc --write '**/*.{ts,js}'",
"lint": "lerna run --scope @firebase/* --scope rxfire lint"
"lint": "lerna run --scope @firebase/* --scope rxfire lint",
"size-report": "node scripts/report_binary_size.js"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -120,7 +121,8 @@
"watch": "1.0.2",
"webpack": "4.42.0",
"yargs": "15.3.1",
"lodash": "4.17.15"
"lodash": "4.17.15",
"terser": "4.6.7"
},
"husky": {
"hooks": {
Expand Down
161 changes: 161 additions & 0 deletions scripts/report_binary_size.js
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);
17 changes: 4 additions & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down