Skip to content

Commit

Permalink
some refactoring of the benchmarking utilities
Browse files Browse the repository at this point in the history
  • Loading branch information
Poincare committed Feb 5, 2017
1 parent c3f3338 commit feaff8e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 19 deletions.
4 changes: 1 addition & 3 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ const simpleQuery = gql`
lastName
}
}`;

const simpleResult = {
data: {
author: {
Expand Down Expand Up @@ -78,9 +79,6 @@ group((end) => {
});

group((end) => {
// TOOD need to figure out a way to run some code before
// every call of this benchmark so that the client instance
// and observable can be set up outside of the timed region.
benchmark('write data and receive update from the cache', (done, setupScope) => {
const client = getClientInstance();
const observable = client.watchQuery({
Expand Down
49 changes: 33 additions & 16 deletions benchmark/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
// easier to use for our benchmarking needs.

// Specifically, it provides `group` and `benchmark`, examples of which
// can be seen below.The functions allow you to manage scope and async
// can be seen within the benchmarks.The functions allow you to manage scope and async
// code more easily than benchmark.js typically allows.
//
// `group` is meant to provide a way to execute code that sets up the scope variables for your
Expand Down Expand Up @@ -55,13 +55,37 @@ export function log(logString: string, ...args: any[]) {
console.log(logString, ...args);
}

interface Scope {
benchmark?: BenchmarkFunction,
setup?: SetupFunction,
afterEach?: AfterEachFunction,
afterAll?: AfterAllFunction,
};

// Internal function that returns the current exposed functions
// benchmark, setup, etc.
function currentScope() {
return {
benchmark,
setup,
afterEach,
afterAll,
};
}

// Internal function that lets us set benchmark, setup, afterEach, etc.
// in a reasonable fashion.
function setScope(scope: Scope) {
benchmark = scope.benchmark;
setup = scope.setup;
afterEach = scope.afterEach;
afterAll = scope.afterAll;
}

export const groupPromises: Promise<void>[] = [];
export const group = (groupFn: GroupFunction) => {
const oldBenchmark = benchmark;
const oldSetup = setup;
const oldAfterEach = afterEach;
const oldAfterAll = afterAll;

export const group = (groupFn: GroupFunction) => {
const oldScope = currentScope();
const scope: {
setup?: SetupFunction,
benchmark?: BenchmarkFunction,
Expand Down Expand Up @@ -164,17 +188,10 @@ export const group = (groupFn: GroupFunction) => {
resolve();
};

benchmark = scope.benchmark;
setup = scope.setup;
afterEach = scope.afterEach;
afterAll = scope.afterAll;

setScope(scope);
groupFn(groupDone);

benchmark = oldBenchmark;
setup = oldSetup;
afterEach = oldAfterEach;
afterAll = oldAfterAll;
setScope(oldScope);

}));
};

Expand Down

0 comments on commit feaff8e

Please sign in to comment.