storybook/scripts/upload-bench.ts

109 lines
2.8 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
createTime: number;
generateTime: number;
initTime: number;
createSize: number;
generateSize: number;
initSize: number;
diffSize: number;
2023-04-25 13:17:59 +08:00
buildTime: number;
2023-06-21 22:57:37 +02:00
buildSize: number;
buildSbAddonsSize: number;
buildSbCommonSize: number;
buildSbManagerSize: number;
buildSbPreviewSize: number;
buildStaticSize: number;
buildPrebuildSize: number;
buildPreviewSize: number;
devPreviewResponsive: number;
devManagerResponsive: number;
devManagerLoaded: number;
devPreviewLoaded: number;
buildManagerLoaded: number;
buildPreviewLoaded: 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');
});