|
| 1 | +import { Suite } from 'benchmark'; |
| 2 | +import { |
| 3 | + parse, |
| 4 | + execute, |
| 5 | + GraphQLSchema, |
| 6 | + GraphQLObjectType, |
| 7 | + GraphQLInt, |
| 8 | +} from 'graphql'; |
| 9 | +import { createBatchResolver } from './batch'; |
| 10 | + |
| 11 | +const suite = new Suite(); |
| 12 | + |
| 13 | +const TestType = new GraphQLObjectType({ |
| 14 | + name: 'Test', |
| 15 | + fields: { |
| 16 | + singleSync: { |
| 17 | + type: GraphQLInt, |
| 18 | + resolve: () => 42, |
| 19 | + }, |
| 20 | + singleAsync: { |
| 21 | + type: GraphQLInt, |
| 22 | + resolve: () => Promise.resolve(42), |
| 23 | + }, |
| 24 | + batchConstant: { |
| 25 | + type: GraphQLInt, |
| 26 | + resolve: createBatchResolver(sources => Array(sources.length).fill(42)), |
| 27 | + }, |
| 28 | + batchLinear: { |
| 29 | + type: GraphQLInt, |
| 30 | + resolve: createBatchResolver(sources => sources.map(42)), |
| 31 | + }, |
| 32 | + }, |
| 33 | +}); |
| 34 | + |
| 35 | +const schema = new GraphQLSchema({ |
| 36 | + query: TestType, |
| 37 | +}); |
| 38 | + |
| 39 | +const singleSyncQuery = parse('{ singleSync }'); |
| 40 | +const singleAsyncQuery = parse('{ singleAsync }'); |
| 41 | +const batchConstantQuery = parse('{ batchConstant }'); |
| 42 | +const batchLinearQuery = parse('{ batchLinear }'); |
| 43 | + |
| 44 | +suite |
| 45 | + .add('single sync field', { |
| 46 | + defer: true, |
| 47 | + fn(deferred) { |
| 48 | + execute(schema, singleSyncQuery).then(() => deferred.resolve()); |
| 49 | + }, |
| 50 | + }) |
| 51 | + .add('single async field', { |
| 52 | + defer: true, |
| 53 | + fn(deferred) { |
| 54 | + execute(schema, singleAsyncQuery).then(() => deferred.resolve()); |
| 55 | + }, |
| 56 | + }) |
| 57 | + .add('batched constant time field', { |
| 58 | + defer: true, |
| 59 | + fn(deferred) { |
| 60 | + execute(schema, batchConstantQuery).then(() => deferred.resolve()); |
| 61 | + }, |
| 62 | + }) |
| 63 | + .add('batched linear time field', { |
| 64 | + defer: true, |
| 65 | + fn(deferred) { |
| 66 | + execute(schema, batchLinearQuery).then(() => deferred.resolve()); |
| 67 | + }, |
| 68 | + }) |
| 69 | + .on('cycle', event => { |
| 70 | + // eslint-disable-next-line no-console |
| 71 | + console.log(String(event.target)); |
| 72 | + }) |
| 73 | + .run({ async: true }); |
0 commit comments