-
-
Notifications
You must be signed in to change notification settings - Fork 676
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add benchmark for large array with field resolver
- Loading branch information
Showing
7 changed files
with
406 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Oops, something went wrong.