Skip to content

Commit

Permalink
add benchmark for large array with field resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
sixmen committed Dec 11, 2019
1 parent 36f1421 commit 38f24b4
Show file tree
Hide file tree
Showing 7 changed files with 406 additions and 0 deletions.
65 changes: 65 additions & 0 deletions benchmarks/large/raw-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLBoolean,
GraphQLInt,
GraphQLList,
} from "graphql";

import { runBenchmark, ARRAY_ITEMS } from "./run";

const SampleObjectType: GraphQLObjectType = new GraphQLObjectType({
name: "SampleObject",
fields: () => ({
sampleField: {
type: new GraphQLNonNull(GraphQLString),
resolve: async (source) => {
return source.sampleField;
}
},
numberField: {
type: new GraphQLNonNull(GraphQLInt),
resolve: async (source) => {
return source.numberField;
}
},
booleanField: {
type: new GraphQLNonNull(GraphQLBoolean),
resolve: async (source) => {
return source.booleanField;
}
},
nestedField: {
type: SampleObjectType,
resolve: async (source) => {
return source.nestedField;
}
},
}),
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
multipleNestedObjects: {
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(SampleObjectType))),
resolve: () =>
Array.from({ length: ARRAY_ITEMS }, (_, index) => ({
stringField: "stringField",
booleanField: true,
numberField: index,
nestedField: {
stringField: "stringField",
booleanField: true,
numberField: index,
},
})),
},
},
}),
});

runBenchmark(schema).catch(console.error);
53 changes: 53 additions & 0 deletions benchmarks/large/raw-basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLBoolean,
GraphQLInt,
GraphQLList,
} from "graphql";

import { runBenchmark, ARRAY_ITEMS } from "./run";

const SampleObjectType: GraphQLObjectType = new GraphQLObjectType({
name: "SampleObject",
fields: () => ({
sampleField: {
type: new GraphQLNonNull(GraphQLString),
},
numberField: {
type: new GraphQLNonNull(GraphQLInt),
},
booleanField: {
type: new GraphQLNonNull(GraphQLBoolean),
},
nestedField: {
type: SampleObjectType,
},
}),
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
multipleNestedObjects: {
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(SampleObjectType))),
resolve: () =>
Array.from({ length: ARRAY_ITEMS }, (_, index) => ({
stringField: "stringField",
booleanField: true,
numberField: index,
nestedField: {
stringField: "stringField",
booleanField: true,
numberField: index,
},
})),
},
},
}),
});

runBenchmark(schema).catch(console.error);
65 changes: 65 additions & 0 deletions benchmarks/large/raw-sync.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLString,
GraphQLNonNull,
GraphQLBoolean,
GraphQLInt,
GraphQLList,
} from "graphql";

import { runBenchmark, ARRAY_ITEMS } from "./run";

const SampleObjectType: GraphQLObjectType = new GraphQLObjectType({
name: "SampleObject",
fields: () => ({
sampleField: {
type: new GraphQLNonNull(GraphQLString),
resolve: (source) => {
return source.sampleField;
}
},
numberField: {
type: new GraphQLNonNull(GraphQLInt),
resolve: (source) => {
return source.numberField;
}
},
booleanField: {
type: new GraphQLNonNull(GraphQLBoolean),
resolve: (source) => {
return source.booleanField;
}
},
nestedField: {
type: SampleObjectType,
resolve: (source) => {
return source.nestedField;
}
},
}),
});

const schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: "Query",
fields: {
multipleNestedObjects: {
type: new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(SampleObjectType))),
resolve: () =>
Array.from({ length: ARRAY_ITEMS }, (_, index) => ({
stringField: "stringField",
booleanField: true,
numberField: index,
nestedField: {
stringField: "stringField",
booleanField: true,
numberField: index,
},
})),
},
},
}),
});

runBenchmark(schema).catch(console.error);
36 changes: 36 additions & 0 deletions benchmarks/large/run.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { GraphQLSchema, execute } from "graphql";
import { gql } from "apollo-server";

const BENCHMARK_ITERATIONS = 100;
export const ARRAY_ITEMS = 5000;

export async function runBenchmark(schema: GraphQLSchema) {
const multipleNestedObjectsQuery = gql`
query {
multipleNestedObjects {
stringField
booleanField
numberField
nestedField {
stringField
booleanField
numberField
}
}
}
`;
console.time("multipleNestedObjects");
for (let i = 0; i < BENCHMARK_ITERATIONS; i++) {
const result = await execute({ schema, document: multipleNestedObjectsQuery });
console.assert(result.data !== undefined, "result data is undefined");
console.assert(
result.data!.multipleNestedObjects.length === ARRAY_ITEMS,
"result data is not a proper array",
);
console.assert(
result.data!.multipleNestedObjects[0].nestedField.booleanField === true,
"data nestedField are incorrect",
);
}
console.timeEnd("multipleNestedObjects");
}
69 changes: 69 additions & 0 deletions benchmarks/large/tg-async.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import "reflect-metadata";
import { buildSchema, Field, ObjectType, Resolver, Query, Int, FieldResolver, Root } from "../../build/package";

import { runBenchmark, ARRAY_ITEMS } from "./run";

@ObjectType()
class SampleObject {
@Field()
stringField!: string;

@Field(type => Int)
numberField!: number;

@Field()
booleanField!: boolean;

@Field({ nullable: true })
nestedField?: SampleObject;
}

@Resolver(SampleObject)
class SampleResolver {
@Query(returns => [SampleObject])
multipleNestedObjects(): SampleObject[] {
return Array.from(
{ length: ARRAY_ITEMS },
(_, index): SampleObject => ({
stringField: "stringField",
booleanField: true,
numberField: index,
nestedField: {
stringField: "stringField",
booleanField: true,
numberField: index,
},
}),
);
}

@FieldResolver()
async stringField(@Root() source: SampleObject) {
return source.stringField;
}

@FieldResolver()
async numberField(@Root() source: SampleObject) {
return source.numberField;
}

@FieldResolver()
async booleanField(@Root() source: SampleObject) {
return source.booleanField;
}

@FieldResolver()
async nestedField(@Root() source: SampleObject) {
return source.nestedField;
}
}

async function main() {
const schema = await buildSchema({
resolvers: [SampleResolver],
});

await runBenchmark(schema);
}

main().catch(console.error);
49 changes: 49 additions & 0 deletions benchmarks/large/tg-basic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import "reflect-metadata";
import { buildSchema, Field, ObjectType, Resolver, Query, Int } from "../../build/package";

import { runBenchmark, ARRAY_ITEMS } from "./run";

@ObjectType()
class SampleObject {
@Field()
stringField!: string;

@Field(type => Int)
numberField!: number;

@Field()
booleanField!: boolean;

@Field({ nullable: true })
nestedField?: SampleObject;
}

@Resolver()
class SampleResolver {
@Query(returns => [SampleObject])
multipleNestedObjects(): SampleObject[] {
return Array.from(
{ length: ARRAY_ITEMS },
(_, index): SampleObject => ({
stringField: "stringField",
booleanField: true,
numberField: index,
nestedField: {
stringField: "stringField",
booleanField: true,
numberField: index,
},
}),
);
}
}

async function main() {
const schema = await buildSchema({
resolvers: [SampleResolver],
});

await runBenchmark(schema);
}

main().catch(console.error);
Loading

0 comments on commit 38f24b4

Please sign in to comment.