Skip to content

Commit

Permalink
fix: more types
Browse files Browse the repository at this point in the history
  • Loading branch information
Diego Ferreiro Val committed Jun 18, 2019
1 parent 23a4cbf commit edb9d25
Show file tree
Hide file tree
Showing 14 changed files with 90 additions and 65 deletions.
2 changes: 1 addition & 1 deletion packages/@best/builder/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"references": [
{ "path": "../runtime" },
{ "path": "../console-stream"},
{ "path": "../config" },
{ "path": "../types"},
]
}
24 changes: 4 additions & 20 deletions packages/@best/runner-abstract/src/index.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<BenchmarkResults>;
abstract async run({ benchmarkEntry }: BenchmarkInfo, projectConfig: FrozenProjectConfig, globalConfig: FrozenGlobalConfig, runnerLogStream: RunnerOutputStream): Promise<BenchmarkResults>;

getRuntimeOptions(projectConfig: FrozenProjectConfig): RuntimeOptions {
getRuntimeOptions(projectConfig: FrozenProjectConfig): BenchmarkRuntimeConfig {
const { benchmarkIterations, benchmarkOnClient, benchmarkMaxDuration, benchmarkMinIterations } = projectConfig;
const definedIterations = Number.isInteger(benchmarkIterations);

Expand Down
1 change: 1 addition & 0 deletions packages/@best/runner-abstract/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
{ "path": "../utils" },
{ "path": "../console-stream" },
{ "path": "../builder" },
{ "path": "../types" }
]
}
30 changes: 7 additions & 23 deletions packages/@best/runner-headless/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);

Expand All @@ -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,
Expand Down Expand Up @@ -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<BenchmarkResultsState> {
async runClientIterations(page: any, state: BenchmarkResultsState, runtimeOptions: BenchmarkRuntimeConfig, runnerLogStream: RunnerOutputStream): Promise<BenchmarkResultsState> {
// Run an iteration to estimate the time it will take
const testResult = await this.runIteration(page, { iterations: 1 });
const estimatedIterationTime = testResult.executedTime;
Expand Down
8 changes: 4 additions & 4 deletions packages/@best/runtime/src/primitives.ts
Original file line number Diff line number Diff line change
@@ -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' });
Expand Down
2 changes: 1 addition & 1 deletion packages/@best/runtime/src/run_iteration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down
17 changes: 6 additions & 11 deletions packages/@best/runtime/src/utils/primitives-nodes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
5 changes: 4 additions & 1 deletion packages/@best/runtime/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"outDir": "dist",
"module": "es6",
"resolveJsonModule": false
}
},
"references": [
{ "path": "../types" },
]
}
8 changes: 8 additions & 0 deletions packages/@best/runtime/typings/runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type BenchmarkPrimitiveNode = BenchmarkPrimitiveRunNode;

interface BenchmarkPrimitiveRunNode {
startedAt: number;
fn: Function;
name: string;
parent: BenchmarkPrimitiveNode;
}
43 changes: 43 additions & 0 deletions packages/@best/types/src/benchmark.ts
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions packages/@best/types/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./config";
export * from "./benchmark";
Original file line number Diff line number Diff line change
Expand Up @@ -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);
})
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function fib(n) {
describe('js-execution2', () => {
benchmark('fibonacci', () => {
run(() => {
fib(15);
return fib(15);
})
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function fib(n) {
describe('js-execution2', () => {
benchmark('fibonacci', () => {
run(() => {
fib(15);
return fib(15);
})
});
});

0 comments on commit edb9d25

Please sign in to comment.