Skip to content

Commit 1a12d6e

Browse files
committed
Add bundle definitions and its measurement script.
1 parent 3b23a9e commit 1a12d6e

File tree

18 files changed

+1204
-51
lines changed

18 files changed

+1204
-51
lines changed

.github/workflows/health-metrics-test.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@ name: Health Metrics
33
on: [push, pull_request]
44

55
env:
6-
METRICS_SERVICE_URL: ${{ secrets.METRICS_SERVICE_URL }}
76
GITHUB_PULL_REQUEST_NUMBER: ${{ github.event.pull_request.number }}
7+
# TODO(yifany): parse from git commit history directly
8+
# Reason: actions/checkout@v2 does not always honor ${{ github.event.pull_request.base.sha }},
9+
# therefore "base.sha" sometimes is not the commit that actually gets merged with the
10+
# pull request head commit for CI test.
11+
# See:
12+
# - https://github.com/actions/checkout/issues/27
13+
# - https://github.com/actions/checkout/issues/237
814
GITHUB_PULL_REQUEST_BASE_SHA: ${{ github.event.pull_request.base.sha }}
915
NODE_OPTIONS: "--max-old-space-size=4096"
1016

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/**
2+
* @license
3+
* Copyright 2021 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import * as fs from 'fs';
19+
import * as path from 'path';
20+
import * as tmp from 'tmp';
21+
22+
import { Bundler, Mode, run as runBundleAnalysis } from './bundle-analysis';
23+
import { Report } from '../../scripts/size_report/report_binary_size';
24+
25+
export async function generateReportForBundles(version?: string) {
26+
const definitionDir = `${__dirname}/bundle-definitions`;
27+
const outputDir = tmp.dirSync().name;
28+
console.log(`Bundle definitions are located at "${definitionDir}".`);
29+
console.log(`Analysis output are located at "${outputDir}".`);
30+
31+
const bundles = fs.readdirSync(definitionDir);
32+
const results: Report[] = [];
33+
for (const bundle of bundles) {
34+
const product = path.basename(bundle, '.json');
35+
const output = `${outputDir}/${product}.analysis.json`;
36+
if (version) {
37+
overwriteVersion(definitionDir, bundle, outputDir, version);
38+
}
39+
const option = {
40+
input: version ? `${outputDir}/${bundle}` : `${definitionDir}/${bundle}`,
41+
bundler: Bundler.Rollup,
42+
mode: version ? Mode.Npm : Mode.Local,
43+
output: output,
44+
debug: true
45+
};
46+
console.log(`Running for bundle "${bundle}" with mode "${option.mode}".`);
47+
await runBundleAnalysis(option);
48+
const measurements = parseAnalysisOutput(product, output);
49+
results.push(...measurements);
50+
}
51+
console.log(results);
52+
return results;
53+
}
54+
55+
function overwriteVersion(
56+
definitionDir: string,
57+
bundle: string,
58+
temp: string,
59+
version: string
60+
) {
61+
const definitions = JSON.parse(
62+
fs.readFileSync(`${definitionDir}/${bundle}`, { encoding: 'utf-8' })
63+
);
64+
for (const definition of definitions) {
65+
const dependencies = definition.dependencies;
66+
for (const dependency of dependencies) {
67+
dependency.versionOrTag = version;
68+
}
69+
}
70+
fs.writeFileSync(`${temp}/${bundle}`, JSON.stringify(definitions, null, 2), {
71+
encoding: 'utf-8'
72+
});
73+
}
74+
75+
function parseAnalysisOutput(product: string, output: string) {
76+
const analyses = JSON.parse(fs.readFileSync(output, { encoding: 'utf-8' }));
77+
const results: Report[] = [];
78+
for (const analysis of analyses) {
79+
const sdk = 'bundle';
80+
const value = analysis.results[0].size;
81+
const type = `${product} (${analysis.name})`;
82+
results.push({ sdk, type, value });
83+
}
84+
return results;
85+
}

repo-scripts/size-analysis/bundle-analysis.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ interface SubModuleImport {
6464
imports: string[];
6565
}
6666

67-
enum Bundler {
67+
export enum Bundler {
6868
Rollup = 'rollup',
6969
Webpack = 'webpack',
7070
Both = 'both'
7171
}
7272

73-
enum Mode {
73+
export enum Mode {
7474
Npm = 'npm',
7575
Local = 'local'
7676
}

repo-scripts/size-analysis/bundle-definition-examples/bundle-definition-1.json

Lines changed: 0 additions & 44 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
[
2+
{
3+
"name": "logEvent",
4+
"dependencies": [
5+
{
6+
"packageName": "firebase",
7+
"versionOrTag": "exp",
8+
"imports": [
9+
{
10+
"path": "app",
11+
"imports": [
12+
"initializeApp"
13+
]
14+
}
15+
]
16+
},
17+
{
18+
"packageName": "firebase",
19+
"versionOrTag": "exp",
20+
"imports": [
21+
{
22+
"path": "analytics",
23+
"imports": [
24+
"getAnalytics",
25+
"logEvent"
26+
]
27+
}
28+
]
29+
}
30+
]
31+
}
32+
]
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
[
2+
{
3+
"name": "ReCaptchaV3Provider",
4+
"dependencies": [
5+
{
6+
"packageName": "firebase",
7+
"versionOrTag": "exp",
8+
"imports": [
9+
{
10+
"path": "app",
11+
"imports": [
12+
"initializeApp"
13+
]
14+
}
15+
]
16+
},
17+
{
18+
"packageName": "firebase",
19+
"versionOrTag": "exp",
20+
"imports": [
21+
{
22+
"path": "app-check",
23+
"imports": [
24+
"initializeAppCheck",
25+
"ReCaptchaV3Provider"
26+
]
27+
}
28+
]
29+
}
30+
]
31+
},
32+
{
33+
"name": "ReCaptchaEnterpriseProvider",
34+
"dependencies": [
35+
{
36+
"packageName": "firebase",
37+
"versionOrTag": "exp",
38+
"imports": [
39+
{
40+
"path": "app",
41+
"imports": [
42+
"initializeApp"
43+
]
44+
}
45+
]
46+
},
47+
{
48+
"packageName": "firebase",
49+
"versionOrTag": "exp",
50+
"imports": [
51+
{
52+
"path": "app-check",
53+
"imports": [
54+
"initializeAppCheck",
55+
"ReCaptchaEnterpriseProvider"
56+
]
57+
}
58+
]
59+
}
60+
]
61+
},
62+
{
63+
"name": "CustomProvider",
64+
"dependencies": [
65+
{
66+
"packageName": "firebase",
67+
"versionOrTag": "exp",
68+
"imports": [
69+
{
70+
"path": "app",
71+
"imports": [
72+
"initializeApp"
73+
]
74+
}
75+
]
76+
},
77+
{
78+
"packageName": "firebase",
79+
"versionOrTag": "exp",
80+
"imports": [
81+
{
82+
"path": "app-check",
83+
"imports": [
84+
"initializeAppCheck",
85+
"CustomProvider"
86+
]
87+
}
88+
]
89+
}
90+
]
91+
}
92+
]

0 commit comments

Comments
 (0)