Skip to content

Commit

Permalink
strict typing
Browse files Browse the repository at this point in the history
  • Loading branch information
ndelangen committed Jun 22, 2023
1 parent 2ec97e5 commit ac6984f
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 90 deletions.
3 changes: 0 additions & 3 deletions scripts/bench/index.ts

This file was deleted.

76 changes: 60 additions & 16 deletions scripts/bench/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,61 @@
export interface Config {
install?: string;
dev?: string;
devUrl?: string;
build?: string;
serve?: string;
serveUrl?: number;
managerLoaded?: string;
previewFrameLocator?: string;
previewLoadedText?: string;
/**
* If set, wait this amount of time (ms) after the dev server
* starts before starting the browser. Used for Histoire, which
* is incompatible with `wait-on` for some reason.
*/
waitOnTimeout?: number;
// NOTE: this must be kept in sync with ./bench.schema, which defines
// the table schema in BigQuery
export interface BenchResults {
branch: string;
commit: string;
timestamp: string;
label: string;

/** The time it takes to create the base sandbox without storybook */
createTime: number;
/** The time it takes to install the base sandbox after it has been initialized */
generateTime: number;
/** The time it takes to run `sb init` on the base sandbox */
initTime: number;
/** Size of base sandbox node_modules without storybook pre-install */
createSize: number;
/** Size of base sandbox node_modules without storybook post-install */
generateSize: number;
/** Size of the sandbox node_modules post `sb init` */
initSize: number;
/** Difference bewtween `initSize` and `generateSize` */
diffSize: number;
/** Full `sb build` time */
buildTime: number;
/** Size of the storybook-static directory in total */
buildSize: number;
/** Size of the storybook-static/sb-addons in total */
buildSbAddonsSize: number;
/** Size of the storybook-static/sb-common-assets */
buildSbCommonSize: number;
/** Size of the storybook-static/sb-manager */
buildSbManagerSize: number;
/** Size of storybook-static/sb-preview */
buildSbPreviewSize: number;
/** Size of the `static` directory if it exists */
buildStaticSize: number;
/** Total size of `sb-x` above */
buildPrebuildSize: number;
/** Total size of everything else (user's stories & components & CSS & assets etc.) */
buildPreviewSize: number;
/** Time to wait-on iframe.html */
devPreviewResponsive: number;
/** Time to wait-on index.html */
devManagerResponsive: number;
/** 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;
}
5 changes: 3 additions & 2 deletions scripts/bench/utils.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import { join } from 'path';
import { ensureDir, writeJSON, readJSON } from 'fs-extra';
import type { BenchResults } from './types';

export const now = () => new Date().getTime();

export interface SaveBenchOptions {
rootDir?: string;
}

export const saveBench = async (data: any, options: SaveBenchOptions) => {
export const saveBench = async (data: Partial<BenchResults>, options: SaveBenchOptions) => {
const dirname = options.rootDir || process.cwd();
const existing = await ensureDir(dirname).then(() => {
return loadBench(options).catch(() => ({}));
});
await writeJSON(join(dirname, `bench.json`), { ...existing, ...data }, { spaces: 2 });
};

export const loadBench = async (options: SaveBenchOptions) => {
export const loadBench = async (options: SaveBenchOptions): Promise<Partial<BenchResults>> => {
const dirname = options.rootDir || process.cwd();
return readJSON(join(dirname, `bench.json`));
};
9 changes: 7 additions & 2 deletions scripts/tasks/bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ export const bench: Task = {
async run(details, options) {
const controllers: AbortController[] = [];
try {
const { browse, saveBench, loadBench } = await import('../bench');
const { browse } = await import('../bench/browse');
const { saveBench, loadBench } = await import('../bench/utils');
const { default: prettyBytes } = await dynamicImport('pretty-bytes');
const { default: prettyTime } = await dynamicImport('pretty-ms');

Expand Down Expand Up @@ -49,7 +50,7 @@ export const bench: Task = {

buildManagerHeaderVisible: buildBrowseResult.managerHeaderVisible,
buildManagerIndexVisible: buildBrowseResult.managerIndexVisible,
buildPreviewVisible: buildBrowseResult.storyVisible,
buildStoryVisible: buildBrowseResult.storyVisible,
buildDocsVisible: buildBrowseResult.docsVisible,
},
{
Expand All @@ -59,6 +60,10 @@ export const bench: Task = {

const data = await loadBench({ rootDir: details.sandboxDir });
Object.entries(data).forEach(([key, value]) => {
if (typeof value !== 'number') {
return;
}

if (key.includes('Size')) {
console.log(`${key}: ${prettyBytes(value)}`);
} else {
Expand Down
77 changes: 10 additions & 67 deletions scripts/upload-bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { copy } from 'fs-extra';
import { join } from 'path';
import { BigQuery } from '@google-cloud/bigquery';

import { loadBench } from './bench';
import type { BenchResults } from './bench/types';
import { loadBench } from './bench/utils';
import { SANDBOX_DIRECTORY, CODE_DIRECTORY } from './utils/constants';
import { execaCommand } from './utils/exec';

Expand All @@ -12,68 +13,6 @@ const GCP_CREDENTIALS = JSON.parse(process.env.GCP_CREDENTIALS || '{}');
const sandboxDir = process.env.SANDBOX_ROOT || SANDBOX_DIRECTORY;
const templateSandboxDir = templateKey && join(sandboxDir, templateKey.replace('/', '-'));

// NOTE: this must be kept in sync with ./bench/bench.schema, which defines
// the table schema in BigQuery
export interface BenchResults {
branch: string;
commit: string;
timestamp: string;
label: string;

/** The time it takes to create the base sandbox without storybook */
createTime: number;
/** The time it takes to install the base sandbox after it has been initialized */
generateTime: number;
/** The time it takes to run `sb init` on the base sandbox */
initTime: number;
/** Size of base sandbox node_modules without storybook pre-install */
createSize: number;
/** Size of base sandbox node_modules without storybook post-install */
generateSize: number;
/** Size of the sandbox node_modules post `sb init` */
initSize: number;
/** Difference bewtween `initSize` and `generateSize` */
diffSize: number;
/** Full `sb build` time */
buildTime: number;
/** Size of the storybook-static directory in total */
buildSize: number;
/** Size of the storybook-static/sb-addons in total */
buildSbAddonsSize: number;
/** Size of the storybook-static/sb-common-assets */
buildSbCommonSize: number;
/** Size of the storybook-static/sb-manager */
buildSbManagerSize: number;
/** Size of storybook-static/sb-preview */
buildSbPreviewSize: number;
/** Size of the `static` directory if it exists */
buildStaticSize: number;
/** Total size of `sb-x` above */
buildPrebuildSize: number;
/** Total size of everything else (user's stories & components & CSS & assets etc.) */
buildPreviewSize: number;
/** Time to wait-on iframe.html */
devPreviewResponsive: number;
/** Time to wait-on index.html */
devManagerResponsive: number;
/** 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;
}

const defaults: Record<keyof BenchResults, null> = {
branch: null,
commit: null,
Expand All @@ -98,10 +37,14 @@ const defaults: Record<keyof BenchResults, null> = {
buildPreviewSize: null,
devPreviewResponsive: null,
devManagerResponsive: null,
devManagerLoaded: null,
devPreviewLoaded: null,
buildManagerLoaded: null,
buildPreviewLoaded: null,
devManagerHeaderVisible: null,
devManagerIndexVisible: null,
devStoryVisible: null,
devDocsVisible: null,
buildManagerHeaderVisible: null,
buildManagerIndexVisible: null,
buildDocsVisible: null,
buildStoryVisible: null,
};

const uploadBench = async () => {
Expand Down

0 comments on commit ac6984f

Please sign in to comment.