Skip to content

Commit

Permalink
Merge pull request #1361 from apollographql/fixed_cost_benchmark
Browse files Browse the repository at this point in the history
Benchmark: Fixed cost of reading from store
  • Loading branch information
helfer authored Mar 5, 2017
2 parents d63f335 + 0dc215f commit e91f3cf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 12 deletions.
76 changes: 64 additions & 12 deletions benchmark/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
runBenchmarks,
DescriptionObject,
log,
dataIdFromObject,
} from './util';

import {
Expand All @@ -19,6 +20,10 @@ import {
ObservableQuery,
} from '../src/index';

import {
diffQueryAgainstStore,
} from '../src/data/readFromStore';

import mockNetworkInterface, {
MockedResponse,
} from '../test/mocks/mockNetworkInterface';
Expand Down Expand Up @@ -195,12 +200,7 @@ times(25, (countR: number) => {
const client = new ApolloClient({
networkInterface: mockNetworkInterface(...mockedResponses),
addTypename: false,
dataIdFromObject: (object: any) => {
if (object.__typename && object.id) {
return object.__typename + '__' + object.id;
}
return null;
},
dataIdFromObject,
});

// insert a bunch of stuff into the cache
Expand Down Expand Up @@ -275,12 +275,7 @@ times(50, (index) => {
result,
}),
addTypename: false,
dataIdFromObject: (object: any) => {
if (object.__typename && object.id) {
return object.__typename + '__' + object.id;
}
return null;
},
dataIdFromObject,
});

const myBenchmark = benchmark;
Expand All @@ -303,4 +298,61 @@ times(50, (index) => {
});
});

// Measure only the amount of time it takes to diff a query against the store
//
// This test allows us to differentiate between the fixed cost of .query() and the fixed cost
// of actually reading from the store.
group((end) => {
// Prime the cache.
const query = gql`
query($id: String) {
house(id: $id) {
reservations {
name
id
}
}
}`;
const variables = { id: '7' };
const reservations: {
name: string,
id: string,
}[] = [{ name: 'a', id: '1'}, {name: 'b', id: '2'}, {name: 'c', id: '3'}];
const result = {
data: {
house: { reservations },
},
};
const client = new ApolloClient({
networkInterface: mockNetworkInterface({
request: { query, variables },
result,
}),
addTypename: false,
dataIdFromObject,
});

const myBenchmark = benchmark;

// We only keep track of the results so that V8 doesn't decide to just throw
// away our cache read code.
const results: any[] = [];
client.query({
query,
variables,
}).then(() => {
myBenchmark('diff query against store', (done) => {

results.push(diffQueryAgainstStore({
query,
variables,
store: client.store.getState()['apollo'].data,
}));
done();
});

end();
});
});

runBenchmarks();
9 changes: 9 additions & 0 deletions benchmark/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export function log(logString: string, ...args: any[]) {
console.log(logString, ...args);
}

// A reasonable implementation of dataIdFromObject that we use within
// the benchmarks.
export const dataIdFromObject = (object: any) => {
if (object.__typename && object.id) {
return object.__typename + '__' + object.id;
}
return null;
};

interface Scope {
benchmark?: BenchmarkFunction;
afterEach?: AfterEachFunction;
Expand Down

0 comments on commit e91f3cf

Please sign in to comment.