Skip to content

Commit 51537f9

Browse files
authored
Async benchmarks (#3134)
* support async benchmark tests * Add benchmarks for sync and async list fields
1 parent c1fe951 commit 51537f9

File tree

3 files changed

+60
-8
lines changed

3 files changed

+60
-8
lines changed

benchmark/list-async-benchmark.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { execute } from 'graphql/execution/execute.js';
2+
import { parse } from 'graphql/language/parser.js';
3+
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
4+
5+
const schema = buildSchema('type Query { listField: [String] }');
6+
const document = parse('{ listField }');
7+
8+
function listField() {
9+
const results = [];
10+
for (let index = 0; index < 1000; index++) {
11+
results.push(Promise.resolve(index));
12+
}
13+
return results;
14+
}
15+
16+
export const benchmark = {
17+
name: 'Execute Asynchronous List Field',
18+
count: 10,
19+
async measure() {
20+
await execute({
21+
schema,
22+
document,
23+
rootValue: { listField },
24+
});
25+
},
26+
};

benchmark/list-sync-benchmark.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { execute } from 'graphql/execution/execute.js';
2+
import { parse } from 'graphql/language/parser.js';
3+
import { buildSchema } from 'graphql/utilities/buildASTSchema.js';
4+
5+
const schema = buildSchema('type Query { listField: [String] }');
6+
const document = parse('{ listField }');
7+
8+
function listField() {
9+
const results = [];
10+
for (let index = 0; index < 1000; index++) {
11+
results.push(index);
12+
}
13+
return results;
14+
}
15+
16+
export const benchmark = {
17+
name: 'Execute Synchronous List Field',
18+
count: 10,
19+
async measure() {
20+
await execute({
21+
schema,
22+
document,
23+
rootValue: { listField },
24+
});
25+
},
26+
};

resources/benchmark.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -385,20 +385,20 @@ function sampleModule(modulePath: string): BenchmarkSample {
385385
import { benchmark } from '${moduleURL}';
386386
387387
// warm up, it looks like 7 is a magic number to reliably trigger JIT
388-
benchmark.measure();
389-
benchmark.measure();
390-
benchmark.measure();
391-
benchmark.measure();
392-
benchmark.measure();
393-
benchmark.measure();
394-
benchmark.measure();
388+
await benchmark.measure();
389+
await benchmark.measure();
390+
await benchmark.measure();
391+
await benchmark.measure();
392+
await benchmark.measure();
393+
await benchmark.measure();
394+
await benchmark.measure();
395395
396396
const memBaseline = process.memoryUsage().heapUsed;
397397
398398
const resourcesStart = process.resourceUsage();
399399
const startTime = process.hrtime.bigint();
400400
for (let i = 0; i < benchmark.count; ++i) {
401-
benchmark.measure();
401+
await benchmark.measure();
402402
}
403403
const timeDiff = Number(process.hrtime.bigint() - startTime);
404404
const resourcesEnd = process.resourceUsage();

0 commit comments

Comments
 (0)