1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-11-22 18:00:18 +01:00
metamask-extension/test/e2e/mv3-perf-stats/bundle-size.js

143 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-07-21 19:40:24 +02:00
#!/usr/bin/env node
/* eslint-disable node/shebang */
const path = require('path');
const { promises: fs } = require('fs');
const yargs = require('yargs/yargs');
const { hideBin } = require('yargs/helpers');
const {
isWritable,
getFirstParentDirectoryThatExists,
} = require('../../helpers/file');
const { exitWithError } = require('../../../development/lib/exit-with-error');
/**
* The e2e test case is used to capture bundle time statistics for extension.
*/
const backgroundFiles = [
'runtime-lavamoat.js',
'lockdown-more.js',
'globalthis.js',
'sentry-install.js',
'policy-load.js',
];
const uiFiles = [
'globalthis.js',
'sentry-install.js',
'runtime-lavamoat.js',
'lockdown-more.js',
'policy-load.js',
];
const BackgroundFileRegex = /background-[0-9]*.js/u;
const CommonFileRegex = /common-[0-9]*.js/u;
const UIFileRegex = /ui-[0-9]*.js/u;
async function main() {
const { argv } = yargs(hideBin(process.argv)).usage(
'$0 [options]',
'Run a page load benchmark',
(_yargs) =>
_yargs.option('out', {
description:
'Output filename. Output printed to STDOUT of this is omitted.',
type: 'string',
normalize: true,
}),
);
const { out } = argv;
const distFolder = 'dist/chrome';
const backgroundFileList = [];
const uiFileList = [];
2022-09-16 18:56:36 +02:00
const commonFileList = [];
2022-07-21 19:40:24 +02:00
Bundlesize stats over time (#15209) * Adding tasks for MV3 test build * more changes * fix * fix * fix * MV3 CI fixes * fixes * fix * Initial work to capture the logs, write to file, and parse it. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * chrome debug logs * Pull logs from webdriver and save to json file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * comment out stats stuffs Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint Fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove console.log Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fixes * Adding build artifact for initialisation time * change * fix lint * fix * fix * fix * fix * fixes * Capturing load time stats * fix * fix * fix * fix * fix build * fix * fix * fix * fix * fix * Initial work to capture the logs, write to file, and parse it. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * chrome debug logs * Pull logs from webdriver and save to json file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * comment out stats stuffs Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Lint Fixes Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * Remove console.log Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix * fix * fix * fix * Initial work to capture the logs, write to file, and parse it. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> chrome debug logs Pull logs from webdriver and save to json file. Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Remove console.log Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> split output of lavamoat to 2 files. background and ui Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix * fix * fix * Enable logging to chrome_debug only if in MV3 Signed-off-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> * fix * fix * fix * fixes * fix * fix * fix * fix * fixes * test * test * fix * fix * MV3 bundle size stats * fix * Committing bundle size status to extension_bundlesize_stats repo * fix * fix * fix * fix * fix * fix * fix * fix * fix * fix * Initial commit * Initial commit * Initial commit * Initial commit * fixes * fix * fix * fix * fix * fix * fix Co-authored-by: Akintayo A. Olusegun <akintayo.segun@gmail.com> Co-authored-by: PeterYinusa <peter.yinusa@consensys.net>
2022-07-22 00:25:18 +02:00
const files = await fs.readdir(distFolder);
2022-07-21 19:40:24 +02:00
for (let i = 0; i < files.length; i++) {
const file = files[i];
if (CommonFileRegex.test(file)) {
const stats = await fs.stat(`${distFolder}/${file}`);
2022-09-16 18:56:36 +02:00
commonFileList.push({ name: file, size: stats.size });
2022-07-21 19:40:24 +02:00
} else if (
backgroundFiles.includes(file) ||
BackgroundFileRegex.test(file)
) {
const stats = await fs.stat(`${distFolder}/${file}`);
backgroundFileList.push({ name: file, size: stats.size });
} else if (uiFiles.includes(file) || UIFileRegex.test(file)) {
const stats = await fs.stat(`${distFolder}/${file}`);
uiFileList.push({ name: file, size: stats.size });
}
}
const backgroundBundleSize = backgroundFileList.reduce(
(result, file) => result + file.size,
0,
);
const uiBundleSize = uiFileList.reduce(
(result, file) => result + file.size,
0,
);
2022-09-16 18:56:36 +02:00
const commonBundleSize = commonFileList.reduce(
(result, file) => result + file.size,
0,
);
2022-07-21 19:40:24 +02:00
const result = {
background: {
name: 'background',
size: backgroundBundleSize,
fileList: backgroundFileList,
},
ui: {
name: 'ui',
size: uiBundleSize,
fileList: uiFileList,
},
2022-09-16 18:56:36 +02:00
common: {
name: 'common',
size: commonBundleSize,
fileList: commonFileList,
},
2022-07-21 19:40:24 +02:00
};
if (out) {
const outPath = `${out}/bundle_size.json`;
const outputDirectory = path.dirname(outPath);
const existingParentDirectory = await getFirstParentDirectoryThatExists(
outputDirectory,
);
if (!(await isWritable(existingParentDirectory))) {
throw new Error('Specified output file directory is not writable');
}
if (outputDirectory !== existingParentDirectory) {
await fs.mkdir(outputDirectory, { recursive: true });
}
await fs.writeFile(outPath, JSON.stringify(result, null, 2));
await fs.writeFile(
`${out}/bundle_size_stats.json`,
JSON.stringify(
{
background: backgroundBundleSize,
ui: uiBundleSize,
2022-09-16 18:56:36 +02:00
common: commonBundleSize,
timestamp: new Date().getTime(),
},
null,
2,
),
);
2022-07-21 19:40:24 +02:00
} else {
console.log(JSON.stringify(result, null, 2));
}
}
main().catch((error) => {
exitWithError(error);
});