storybook/scripts/upload-bench.ts

139 lines
4.5 KiB
TypeScript
Raw Normal View History

import { copy } from 'fs-extra';
import { join } from 'path';
2023-04-25 13:17:59 +08:00
import { BigQuery } from '@google-cloud/bigquery';
2023-04-25 00:17:36 +08:00
import { loadBench } from './bench';
import { SANDBOX_DIRECTORY, CODE_DIRECTORY } from './utils/constants';
2023-04-25 13:17:59 +08:00
import { execaCommand } from './utils/exec';
const templateKey = process.argv[2];
2023-04-25 13:17:59 +08:00
const GCP_CREDENTIALS = JSON.parse(process.env.GCP_CREDENTIALS || '{}');
const sandboxDir = process.env.SANDBOX_ROOT || SANDBOX_DIRECTORY;
const templateSandboxDir = templateKey && join(sandboxDir, templateKey.replace('/', '-'));
2023-04-25 00:17:36 +08:00
2023-06-22 14:45:12 +08:00
// NOTE: this must be kept in sync with ./bench/bench.schema, which defines
// the table schema in BigQuery
2023-04-25 13:17:59 +08:00
export interface BenchResults {
branch: string;
commit: string;
timestamp: string;
label: string;
2023-06-21 22:57:37 +02:00
2023-06-22 15:24:29 +08:00
/** The time it takes to create the base sandbox without storybook */
2023-06-21 22:57:37 +02:00
createTime: number;
2023-06-22 15:24:29 +08:00
/** The time it takes to install the base sandbox after it has been initialized */
2023-06-21 22:57:37 +02:00
generateTime: number;
2023-06-22 15:24:29 +08:00
/** The time it takes to run `sb init` on the base sandbox */
2023-06-21 22:57:37 +02:00
initTime: number;
2023-06-22 15:24:29 +08:00
/** Size of base sandbox node_modules without storybook pre-install */
2023-06-21 22:57:37 +02:00
createSize: number;
2023-06-22 15:24:29 +08:00
/** Size of base sandbox node_modules without storybook post-install */
2023-06-21 22:57:37 +02:00
generateSize: number;
2023-06-22 15:24:29 +08:00
/** Size of the sandbox node_modules post `sb init` */
2023-06-21 22:57:37 +02:00
initSize: number;
2023-06-22 15:24:29 +08:00
/** Difference bewtween `initSize` and `generateSize` */
2023-06-21 22:57:37 +02:00
diffSize: number;
2023-06-22 15:24:29 +08:00
/** Full `sb build` time */
2023-04-25 13:17:59 +08:00
buildTime: number;
2023-06-22 15:24:29 +08:00
/** Size of the storybook-static directory in total */
2023-06-21 22:57:37 +02:00
buildSize: number;
2023-06-22 15:24:29 +08:00
/** Size of the storybook-static/sb-addons in total */
2023-06-21 22:57:37 +02:00
buildSbAddonsSize: number;
2023-06-22 15:24:29 +08:00
/** Size of the storybook-static/sb-common-assets */
2023-06-21 22:57:37 +02:00
buildSbCommonSize: number;
2023-06-22 15:24:29 +08:00
/** Size of the storybook-static/sb-manager */
2023-06-21 22:57:37 +02:00
buildSbManagerSize: number;
2023-06-22 15:24:29 +08:00
/** Size of storybook-static/sb-preview */
2023-06-21 22:57:37 +02:00
buildSbPreviewSize: number;
2023-06-22 15:24:29 +08:00
/** Size of the `static` directory if it exists */
2023-06-21 22:57:37 +02:00
buildStaticSize: number;
2023-06-22 15:24:29 +08:00
/** Total size of `sb-x` above */
2023-06-21 22:57:37 +02:00
buildPrebuildSize: number;
2023-06-22 15:24:29 +08:00
/** Total size of everything else (user's stories & components & CSS & assets etc.) */
2023-06-21 22:57:37 +02:00
buildPreviewSize: number;
2023-06-22 15:24:29 +08:00
/** Time to wait-on iframe.html */
2023-06-21 22:57:37 +02:00
devPreviewResponsive: number;
2023-06-22 15:24:29 +08:00
/** Time to wait-on index.html */
2023-06-21 22:57:37 +02:00
devManagerResponsive: number;
2023-06-22 15:24:29 +08:00
/** Time to browse to index.html and view the SB logo */
devManagerHeaderVisible: number;
/** Time to browse to index.html and load the story index */
devManagerIndexVisible: number;
/** Time to browse to index.html and load iframe content and the story is rendered */
devStoryVisible: number;
/** Time to browse to index.html and load iframe content and the docs page is rendered */
devDocsVisible: number;
/** Time to browse to index.html and view the SB logo */
buildManagerHeaderVisible: number;
/** Time to browse to index.html and load the story index */
buildManagerIndexVisible: number;
/** Time to browse to index.html and load iframe content and the story is rendered */
buildStoryVisible: number;
/** Time to browse to index.html and load iframe content and the docs page is rendered */
buildDocsVisible: number;
2023-04-25 13:17:59 +08:00
}
2023-06-22 14:45:12 +08:00
2023-06-21 22:57:37 +02:00
const defaults: Record<keyof BenchResults, null> = {
branch: null,
commit: null,
timestamp: null,
label: null,
createTime: null,
generateTime: null,
initTime: null,
createSize: null,
generateSize: null,
initSize: null,
diffSize: null,
buildTime: null,
buildSize: null,
buildSbAddonsSize: null,
buildSbCommonSize: null,
buildSbManagerSize: null,
buildSbPreviewSize: null,
buildStaticSize: null,
buildPrebuildSize: null,
buildPreviewSize: null,
devPreviewResponsive: null,
devManagerResponsive: null,
devManagerLoaded: null,
devPreviewLoaded: null,
buildManagerLoaded: null,
buildPreviewLoaded: null,
};
2023-04-25 13:17:59 +08:00
2023-04-25 00:17:36 +08:00
const uploadBench = async () => {
2023-06-21 22:57:37 +02:00
const results = await loadBench({ rootDir: templateSandboxDir });
2023-04-25 13:17:59 +08:00
const row = {
2023-06-21 22:57:37 +02:00
...defaults,
2023-04-25 13:17:59 +08:00
branch:
process.env.CIRCLE_BRANCH || (await execaCommand('git rev-parse --abbrev-ref HEAD')).stdout,
commit: process.env.CIRCLE_SHA1 || (await execaCommand('git rev-parse HEAD')).stdout,
timestamp: new Date().toISOString(),
label: templateKey,
2023-06-21 22:57:37 +02:00
...results,
2023-04-25 13:17:59 +08:00
} as BenchResults;
2023-06-21 22:57:37 +02:00
const store = new BigQuery({
2023-04-25 13:17:59 +08:00
projectId: GCP_CREDENTIALS.project_id,
credentials: GCP_CREDENTIALS,
});
2023-06-21 22:57:37 +02:00
const dataset = store.dataset('benchmark_results');
2023-06-22 14:45:12 +08:00
const appTable = dataset.table('bench2');
2023-04-25 13:17:59 +08:00
await appTable.insert([row]);
2023-06-21 22:57:37 +02:00
// for CI artifacts
await copy(
`${templateSandboxDir}/bench.json`,
`${CODE_DIRECTORY}/bench-results/${templateSandboxDir}.json`
);
2023-04-25 00:17:36 +08:00
};
uploadBench().then(() => {
console.log('done');
});