storybook/scripts/upload-bench.ts

101 lines
2.6 KiB
TypeScript
Raw Normal View History

/* eslint-disable no-console */
import { join } from 'path';
2023-04-25 13:17:59 +08:00
import { BigQuery } from '@google-cloud/bigquery';
2023-11-08 09:59:43 +01:00
import { execaCommand } from 'execa';
2023-06-22 09:38:52 +02:00
import type { BenchResults } from './bench/types';
import { loadBench } from './bench/utils';
2023-06-23 14:31:58 +02:00
import { SANDBOX_DIRECTORY } from './utils/constants';
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,
testBuildTime: null,
testBuildSize: null,
testBuildSbAddonsSize: null,
testBuildSbCommonSize: null,
testBuildSbManagerSize: null,
testBuildSbPreviewSize: null,
testBuildStaticSize: null,
testBuildPrebuildSize: null,
testBuildPreviewSize: null,
2023-06-21 22:57:37 +02:00
devPreviewResponsive: null,
devManagerResponsive: null,
2023-06-22 09:38:52 +02:00
devManagerHeaderVisible: null,
devManagerIndexVisible: null,
devStoryVisible: null,
2023-06-23 10:22:14 +08:00
devStoryVisibleUncached: null,
devAutodocsVisible: null,
devMDXVisible: null,
2023-06-22 09:38:52 +02:00
buildManagerHeaderVisible: null,
buildManagerIndexVisible: null,
2023-06-23 10:22:14 +08:00
buildAutodocsVisible: null,
2023-06-22 09:38:52 +02:00
buildStoryVisible: null,
2023-06-23 10:22:14 +08:00
buildMDXVisible: 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:
2023-11-08 09:59:43 +01:00
process.env.CIRCLE_BRANCH ||
(await execaCommand('git rev-parse --abbrev-ref HEAD', { cleanup: true })).stdout,
commit:
process.env.CIRCLE_SHA1 ||
(await execaCommand('git rev-parse HEAD', { cleanup: true })).stdout,
2023-04-25 13:17:59 +08:00
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-04-25 00:17:36 +08:00
};
2023-06-23 10:22:14 +08:00
uploadBench()
.catch((err) => {
console.error(err);
if (err.errors) {
err.errors.forEach((elt: any) => {
console.log(elt);
});
}
process.exit(1);
})
.then(() => {
console.log('done');
});