diff --git a/packages/@best/builder/tsconfig.json b/packages/@best/builder/tsconfig.json index 5d885a3a..e41d794e 100644 --- a/packages/@best/builder/tsconfig.json +++ b/packages/@best/builder/tsconfig.json @@ -7,6 +7,6 @@ "references": [ { "path": "../runtime" }, { "path": "../console-stream"}, - { "path": "../config" }, + { "path": "../types"}, ] } diff --git a/packages/@best/runner-abstract/src/index.ts b/packages/@best/runner-abstract/src/index.ts index 5565577d..fc3f27cd 100644 --- a/packages/@best/runner-abstract/src/index.ts +++ b/packages/@best/runner-abstract/src/index.ts @@ -1,20 +1,6 @@ import { getSystemInfo } from '@best/utils'; import { RunnerOutputStream } from "@best/console-stream"; -import { FrozenGlobalConfig, FrozenProjectConfig } from '@best/types'; - -export interface RunnerBundle { - benchmarkName: string, - benchmarkEntry: string, - benchmarkFolder: string, - benchmarkSignature: string -} - -export interface RuntimeOptions { - maxDuration: number; - minSampleCount: number, - iterations: number, - iterateOnClient: boolean -} +import { FrozenGlobalConfig, FrozenProjectConfig, BenchmarkInfo, BenchmarkRuntimeConfig } from '@best/types'; export interface BenchmarkResultsState { executedTime: number, @@ -23,14 +9,12 @@ export interface BenchmarkResultsState { iterateOnClient: boolean, } -export interface BenchmarkResults { - -} +export interface BenchmarkResults {} export default abstract class AbstractRunner { - abstract async run({ benchmarkEntry }: RunnerBundle, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream): Promise; + abstract async run({ benchmarkEntry }: BenchmarkInfo, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream): Promise; - getRuntimeOptions(projectConfig: FrozenProjectConfig): RuntimeOptions { + getRuntimeOptions(projectConfig: FrozenProjectConfig): BenchmarkRuntimeConfig { const { benchmarkIterations, benchmarkOnClient, benchmarkMaxDuration, benchmarkMinIterations } = projectConfig; const definedIterations = Number.isInteger(benchmarkIterations); diff --git a/packages/@best/runner-abstract/tsconfig.json b/packages/@best/runner-abstract/tsconfig.json index c43b6339..f4685536 100644 --- a/packages/@best/runner-abstract/tsconfig.json +++ b/packages/@best/runner-abstract/tsconfig.json @@ -8,5 +8,6 @@ { "path": "../utils" }, { "path": "../console-stream" }, { "path": "../builder" }, + { "path": "../types" } ] } diff --git a/packages/@best/runner-headless/src/index.ts b/packages/@best/runner-headless/src/index.ts index 12d0e9c3..ec303863 100644 --- a/packages/@best/runner-headless/src/index.ts +++ b/packages/@best/runner-headless/src/index.ts @@ -3,8 +3,8 @@ import express from 'express'; import puppeteer from 'puppeteer'; import { Socket } from 'net'; import { RunnerOutputStream } from "@best/console-stream"; -import { FrozenGlobalConfig, FrozenProjectConfig } from '@best/types'; -import AbstractRunner, { RunnerBundle, BenchmarkResultsState, RuntimeOptions } from '@best/runner-abstract'; +import { FrozenGlobalConfig, FrozenProjectConfig, BenchmarkInfo, BenchmarkRuntimeConfig } from '@best/types'; +import AbstractRunner, { BenchmarkResultsState } from '@best/runner-abstract'; declare var BEST: any; @@ -30,9 +30,9 @@ export default class Runner extends AbstractRunner { page: any; browser: any; - async run({ benchmarkEntry }: RunnerBundle, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream) { + async run({ benchmarkEntry }: BenchmarkInfo, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream) { const { useHttp } = projectConfig; - const runtimeOptions = this.normalizeRuntimeOptions(projectConfig); + const runtimeOptions = this.getRuntimeOptions(projectConfig); const state = this.initializeBenchmarkState(runtimeOptions); const url = await this.runSetupAndGetUrl(benchmarkEntry, useHttp); @@ -56,23 +56,7 @@ export default class Runner extends AbstractRunner { } } - normalizeRuntimeOptions(projectConfig: FrozenProjectConfig): RuntimeOptions { - const { benchmarkIterations, benchmarkOnClient, benchmarkMaxDuration, benchmarkMinIterations } = projectConfig; - const definedIterations = Number.isInteger(benchmarkIterations); - - // For benchmarking on the client or a defined number of iterations duration is irrelevant - const maxDuration = definedIterations ? 1 : benchmarkMaxDuration; - const minSampleCount = definedIterations ? benchmarkIterations : benchmarkMinIterations; - - return { - maxDuration, - minSampleCount, - iterations: benchmarkIterations, - iterateOnClient: benchmarkOnClient, - }; - } - - initializeBenchmarkState({ iterateOnClient }: RuntimeOptions): BenchmarkResultsState { + initializeBenchmarkState({ iterateOnClient }: BenchmarkRuntimeConfig): BenchmarkResultsState { return { executedTime: 0, executedIterations: 0, @@ -197,13 +181,13 @@ export default class Runner extends AbstractRunner { }; } - async runIterations(page: any, state: BenchmarkResultsState, runtimeOptions: RuntimeOptions, runnnerLogStream: RunnerOutputStream) { + async runIterations(page: any, state: BenchmarkResultsState, runtimeOptions: BenchmarkRuntimeConfig, runnnerLogStream: RunnerOutputStream) { return state.iterateOnClient ? this.runClientIterations(page, state, runtimeOptions, runnnerLogStream) : this.runServerIterations(page, state, runtimeOptions, runnnerLogStream); } - async runClientIterations(page: any, state: BenchmarkResultsState, runtimeOptions: RuntimeOptions, runnerLogStream: RunnerOutputStream): Promise { + async runClientIterations(page: any, state: BenchmarkResultsState, runtimeOptions: BenchmarkRuntimeConfig, runnerLogStream: RunnerOutputStream): Promise { // Run an iteration to estimate the time it will take const testResult = await this.runIteration(page, { iterations: 1 }); const estimatedIterationTime = testResult.executedTime; diff --git a/packages/@best/runtime/src/primitives.ts b/packages/@best/runtime/src/primitives.ts index 3ae25535..b54741de 100644 --- a/packages/@best/runtime/src/primitives.ts +++ b/packages/@best/runtime/src/primitives.ts @@ -1,15 +1,15 @@ import { dispatch } from './state'; import { HOOKS, RUN_BENCHMARK, MODES } from './constants'; -const _dispatchDescribe = (blockName: any, blockFn: Function, mode?: string) => { +const _dispatchDescribe = (blockName: string, blockFn: Function, mode?: string) => { dispatch({ blockName, mode, name: 'start_describe_definition' }); blockFn(); dispatch({ name: 'finish_describe_definition' }); }; -const describe = (blockName: any, blockFn: Function) => _dispatchDescribe(blockName, blockFn); -describe.only = (blockName: any, blockFn: Function) => _dispatchDescribe(blockName, blockFn, MODES.ONLY); -describe.skip = (blockName: any, blockFn: Function) => _dispatchDescribe(blockName, blockFn, MODES.SKIP); +const describe = (blockName: string, blockFn: Function) => _dispatchDescribe(blockName, blockFn); +describe.only = (blockName: string, blockFn: Function) => _dispatchDescribe(blockName, blockFn, MODES.ONLY); +describe.skip = (blockName: string, blockFn: Function) => _dispatchDescribe(blockName, blockFn, MODES.SKIP); const _dispatchBenchmark = (blockName: any, blockFn: Function, mode?: string) => { dispatch({ blockName, mode, name: 'start_benchmark_definition' }); diff --git a/packages/@best/runtime/src/run_iteration.ts b/packages/@best/runtime/src/run_iteration.ts index 704b4b46..baf2a96f 100644 --- a/packages/@best/runtime/src/run_iteration.ts +++ b/packages/@best/runtime/src/run_iteration.ts @@ -40,7 +40,7 @@ const executeBenchmark = async (benchmarkNode: any, markName: string, { useMacro try { await benchmarkNode.fn(); - benchmarkNode.runDuration = formatTime(time() - benchmarkNode.startedAt); + benchmarkNode.metrics.runDuration = formatTime(time() - benchmarkNode.startedAt); if (useMacroTaskAfterBenchmark) { withMacroTask(async () => { diff --git a/packages/@best/runtime/src/utils/primitives-nodes.ts b/packages/@best/runtime/src/utils/primitives-nodes.ts index ca81d48e..fa5b9185 100644 --- a/packages/@best/runtime/src/utils/primitives-nodes.ts +++ b/packages/@best/runtime/src/utils/primitives-nodes.ts @@ -35,14 +35,9 @@ export const makeBenchmark = (name: string, parent: any, mode: string) => { }; }; -export const makeBenchmarkRun = (fn: Function, parent: any) => { - return { - duration: null, - errors: [], - fn, - name: RUN_BENCHMARK, - parent, - startedAt: null, - status: null, - }; -}; +export const makeBenchmarkRun = (fn: Function, parent: any): BenchmarkPrimitiveRunNode => ({ + fn, + name: RUN_BENCHMARK, + parent, + startedAt: 0, +}); diff --git a/packages/@best/runtime/tsconfig.json b/packages/@best/runtime/tsconfig.json index db6e7ced..e7232b6d 100644 --- a/packages/@best/runtime/tsconfig.json +++ b/packages/@best/runtime/tsconfig.json @@ -5,5 +5,8 @@ "outDir": "dist", "module": "es6", "resolveJsonModule": false - } + }, + "references": [ + { "path": "../types" }, + ] } diff --git a/packages/@best/runtime/typings/runtime.d.ts b/packages/@best/runtime/typings/runtime.d.ts new file mode 100644 index 00000000..3d3e4e5a --- /dev/null +++ b/packages/@best/runtime/typings/runtime.d.ts @@ -0,0 +1,8 @@ +type BenchmarkPrimitiveNode = BenchmarkPrimitiveRunNode; + +interface BenchmarkPrimitiveRunNode { + startedAt: number; + fn: Function; + name: string; + parent: BenchmarkPrimitiveNode; +} diff --git a/packages/@best/types/src/benchmark.ts b/packages/@best/types/src/benchmark.ts new file mode 100644 index 00000000..a99dae6a --- /dev/null +++ b/packages/@best/types/src/benchmark.ts @@ -0,0 +1,43 @@ +export interface BenchmarkInfo { + benchmarkName: string, + benchmarkEntry: string, + benchmarkFolder: string, + benchmarkSignature: string +} + +export interface BenchmarkRuntimeConfig { + maxDuration: number; + minSampleCount: number, + iterations: number, + iterateOnClient: boolean +} + +export interface BenchmarkMetrics { + [key: string]: number; + aggregate: number; + script: number; +} + +export enum BenchmarkNodeType { Benchmark, Group } +export type BenchmarkNodes = BenchmarkGroupNode[] | BenchmarkResultNode[]; + +export interface BenchmarkResultNode { + type: BenchmarkNodeType.Benchmark; + benchmark: string; + startedAt: number; + metrics: BenchmarkMetrics +} + +export interface BenchmarkGroupNode { + type: BenchmarkNodeType.Group; + group: string; + nodes: BenchmarkNodes; + aggregate: number; +} + +export interface BenchmarkResults { + benchmarkName: string; + executedIterations: number; + aggregate: number; + results: BenchmarkNodes; +} diff --git a/packages/@best/types/src/index.ts b/packages/@best/types/src/index.ts index 5c62e04f..a40f18d0 100644 --- a/packages/@best/types/src/index.ts +++ b/packages/@best/types/src/index.ts @@ -1 +1,2 @@ export * from "./config"; +export * from "./benchmark"; diff --git a/packages/best-benchmarks/__benchmarks__/js_execution.benchmark.js b/packages/best-benchmarks/__benchmarks__/js_execution.benchmark.js index c6c480c9..64681a4d 100644 --- a/packages/best-benchmarks/__benchmarks__/js_execution.benchmark.js +++ b/packages/best-benchmarks/__benchmarks__/js_execution.benchmark.js @@ -3,9 +3,15 @@ function fib(n) { } describe('js-execution', () => { - benchmark('fibonacci', () => { + benchmark('fibonacci 15', () => { run(() => { - fib(15); + return fib(15); + }) + }); + + benchmark('fibonacci 16', () => { + run(() => { + return fib(16); }) }); }); diff --git a/packages/best-benchmarks/__benchmarks__/js_execution2.benchmark.js b/packages/best-benchmarks/__benchmarks__/js_execution2.benchmark.js index ea813c6f..de942cb4 100644 --- a/packages/best-benchmarks/__benchmarks__/js_execution2.benchmark.js +++ b/packages/best-benchmarks/__benchmarks__/js_execution2.benchmark.js @@ -5,7 +5,7 @@ function fib(n) { describe('js-execution2', () => { benchmark('fibonacci', () => { run(() => { - fib(15); + return fib(15); }) }); }); diff --git a/packages/best-benchmarks/__benchmarks__/very_long_folder_for_testing_output/yes_indeed_very_long_name_folder/at_least_this_should_be_more_than_hundred_chars/js_execution3.benchmark.js b/packages/best-benchmarks/__benchmarks__/very_long_folder_for_testing_output/yes_indeed_very_long_name_folder/at_least_this_should_be_more_than_hundred_chars/js_execution3.benchmark.js index ea813c6f..de942cb4 100644 --- a/packages/best-benchmarks/__benchmarks__/very_long_folder_for_testing_output/yes_indeed_very_long_name_folder/at_least_this_should_be_more_than_hundred_chars/js_execution3.benchmark.js +++ b/packages/best-benchmarks/__benchmarks__/very_long_folder_for_testing_output/yes_indeed_very_long_name_folder/at_least_this_should_be_more_than_hundred_chars/js_execution3.benchmark.js @@ -5,7 +5,7 @@ function fib(n) { describe('js-execution2', () => { benchmark('fibonacci', () => { run(() => { - fib(15); + return fib(15); }) }); });