Skip to content

Commit 5e9edeb

Browse files
committed
add benchmark
1 parent 0773937 commit 5e9edeb

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
"test": "jest",
77
"test-watch": "jest --watch",
88
"ci": "npm run format && git diff --exit-code && npm run lint && npm test",
9-
"build": "rm -rf lib && babel src --out-dir lib"
9+
"build": "rm -rf lib && babel src --out-dir lib",
10+
"benchmark": "npm run build && echo && node lib/batch.benchmark.js && echo"
1011
},
1112
"babel": {
1213
"presets": [
@@ -41,6 +42,7 @@
4142
"babel-jest": "^19.0.0",
4243
"babel-preset-env": "^1.1.10",
4344
"babel-preset-es2015": "^6.22.0",
45+
"benchmark": "^2.1.3",
4446
"eslint": "^3.16.1",
4547
"graphql": "^0.9.1",
4648
"jest": "^19.0.2",

src/batch.benchmark.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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

Comments
 (0)