From feaff8e2f1c157912344854f1693efeeebca2358 Mon Sep 17 00:00:00 2001 From: Dhaivat Pandya Date: Wed, 18 Jan 2017 14:16:09 -0800 Subject: [PATCH] some refactoring of the benchmarking utilities --- benchmark/index.ts | 4 +--- benchmark/util.ts | 49 +++++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/benchmark/index.ts b/benchmark/index.ts index db92df5c538..1ce2b29c69e 100644 --- a/benchmark/index.ts +++ b/benchmark/index.ts @@ -34,6 +34,7 @@ const simpleQuery = gql` lastName } }`; + const simpleResult = { data: { author: { @@ -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({ diff --git a/benchmark/util.ts b/benchmark/util.ts index 6ac97ca1483..076c71b3e30 100644 --- a/benchmark/util.ts +++ b/benchmark/util.ts @@ -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 @@ -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[] = []; -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, @@ -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); + })); };