<commit_before>import gql from 'graphql-tag';

import {
  ApolloClient,
  createNetworkInterface,
} from '../src/index';

import mockNetworkInterface from '../test/mocks/mockNetworkInterface';

import {
  Deferred, 
} from 'benchmark';

const Benchmark = require('benchmark');
const bsuite = new Benchmark.Suite();

let globalClient: ApolloClient = null;
const simpleQuery = gql`
  query {
    author {
      firstName
      lastName
    }
}`;
const simpleResult = {
  data: {
    author: {
      firstName: 'John',
      lastName: 'Smith',
    },
  },
};
const simpleReqResp = {
  request: { query: simpleQuery },
  result: simpleResult,
};

bsuite
  .add('construct instance', () => {
    new ApolloClient({});
  })
  .add('create network interface', () => {
    createNetworkInterface({
      uri: '/graphql',
    });
  })
  .add('fetch a query result from mocked server', {
    defer: true,
    setup: () => {
      globalClient = new ApolloClient({
        networkInterface: mockNetworkInterface({
          request: { query: simpleQuery },
          result: simpleResult,
        }),
        addTypename: false,
      });
    },
    fn: (deferred: any) => {
      globalClient.query({ query: simpleQuery }).then((result) => {
        deferred.resolve();
      });
    },
  })
  .add('read simple query result from cache', {
    defer: true,
    setup: (deferred: any) => {
      globalClient = new ApolloClient({
        networkInterface: mockNetworkInterface(simpleReqResp),
        addTypename: false,
      });

      // insert the result into the cache 
      globalClient.query({ query: simpleQuery }).then((result) => {
        deferred.resolve();
      });
    },
    fn: (deferred: any) => {
      globalClient.query({
        query: simpleQuery,
        forceFetch: false,
      }).then((result) => {
        deferred.resolve();
      }).catch((err) => {
        console.log('Threw error: ');
        console.log(err);
      });
    },
  })
  .on('cycle', function(event: any) {
    console.log('Mean time in ms: ', event.target.stats.mean * 1000);
    console.log(String(event.target));
  })
  .run({'async': true});<commit_msg>have a working benchmark for reading a simple result out of the cache<commit_after>import gql from 'graphql-tag';

import {
  ApolloClient,
} from '../src/index';

import mockNetworkInterface from '../test/mocks/mockNetworkInterface';

import {
  Deferred, 
} from 'benchmark';

const Benchmark = require('benchmark');
const bsuite = new Benchmark.Suite();

let globalClient: ApolloClient = null;
const simpleQuery = gql`
  query {
    author {
      firstName
      lastName
    }
}`;
const simpleResult = {
  data: {
    author: {
      firstName: 'John',
      lastName: 'Smith',
    },
  },
};
const simpleReqResp = {
  request: { query: simpleQuery },
  result: simpleResult,
};

bsuite
  .add('construct instance', () => {
    new ApolloClient({});
  })
  .add('fetch a query result from mocked server', {
    defer: true,
    setup: () => {
      globalClient = new ApolloClient({
        networkInterface: mockNetworkInterface({
          request: { query: simpleQuery },
          result: simpleResult,
        }),
        addTypename: false,
      });
    },
    fn: (deferred: any) => {
      globalClient.query({ query: simpleQuery }).then((result) => {
        deferred.resolve();
      });
    },
  })
  .add('read simple query result from cache', {
    defer: true,
    setup: (deferred: any) => {
      globalClient = new ApolloClient({
        networkInterface: mockNetworkInterface(simpleReqResp),
        addTypename: false,
      });

      // insert the result into the cache
      globalClient.query({ query: simpleQuery }).then((result) => {
        deferred.resolve();
      });
    },
    fn: (deferred: any) => {
      globalClient.query({
        query: simpleQuery,
        noFetch: true,
      }).then((result) => {
        deferred.resolve();
      })
    },
  })
  .on('cycle', function(event: any) {
    console.log('Mean time in ms: ', event.target.stats.mean * 1000);
    console.log(String(event.target));
  })
  .run({'async': true});
{'fraction_non_alphanumeric': 0.15264948954788526, 'fraction_numerical': 0.0019445794846864365, 'mean_word_length': 2.565857885615251, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4288430', 'n_tokens_mistral': 1274, 'n_tokens_neox': 1229, 'n_words': 292}