storybook/scripts/upload-bench.ts

82 lines
2.2 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-06-22 09:38:52 +02:00
import type { BenchResults } from './bench/types';
import { loadBench } from './bench/utils';
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-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,
2023-06-22 09:38:52 +02:00
devManagerHeaderVisible: null,
devManagerIndexVisible: null,
devStoryVisible: null,
devDocsVisible: null,
buildManagerHeaderVisible: null,
buildManagerIndexVisible: null,
buildDocsVisible: null,
buildStoryVisible: null,
2023-06-21 22:57:37 +02:00
};
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');
});