Skip to content

Commit

Permalink
Initial benchmark tests for basic queries; something we can work upon…
Browse files Browse the repository at this point in the history
… from here

for perf-related PRs.
- simple hello world equivalent
- with a list
- deeper query with list
- query with many root fields and list in each of them
- deeper query with many root fields and lists

To really bring out probable issues with query perf, a latency is
introduced to the data retrieval (previously it was just in-memory data fetch)

When testing against branch before PR #20 and #21, this really highlighted the problem with evaluating list fields.

In the future, more benchmarks for queries with fragments probably would be useful.
  • Loading branch information
sogko committed Jun 11, 2016
1 parent f7aec0f commit 8a5ddd4
Show file tree
Hide file tree
Showing 2 changed files with 224 additions and 0 deletions.
220 changes: 220 additions & 0 deletions benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package graphql_test

import (
"testing"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/testutil"
)

// to avoid possible compiler optimization
var benchmark_result *graphql.Result

func benchmarkQuery(b *testing.B, schema graphql.Schema, query string) {
params := graphql.Params{
Schema: testutil.StarWarsSchema,
RequestString: query,
}

var result *graphql.Result
for n := 0; n < b.N; n++ {
result = graphql.Do(params)
}
benchmark_result = result
}

func BenchmarkQuery_BasicQuery(b *testing.B) {
query := `
{
hero {
name
}
}
`
benchmarkQuery(b, testutil.StarWarsSchema, query)
}

func BenchmarkQuery_BasicQueryWithList(b *testing.B) {
query := `
{
hero {
id
name
friends {
id
name
}
appearsIn
}
}
`
benchmarkQuery(b, testutil.StarWarsSchema, query)
}

func BenchmarkQuery_DeeperQueryWithList(b *testing.B) {
query := `
{
hero {
id
name
friends {
id
name
friends {
id
name
appearsIn
}
appearsIn
}
appearsIn
}
}
`
benchmarkQuery(b, testutil.StarWarsSchema, query)
}

func BenchmarkQuery_QueryWithManyRootFieldsAndList(b *testing.B) {
query := `
{
hero {
id
name
friends {
name
}
appearsIn
}
vader: human(id: "1001") {
id
name
friends {
id
name
appearsIn
}
appearsIn
homePlanet
}
leia: human(id: "1003") {
id
name
friends {
id
name
appearsIn
}
appearsIn
homePlanet
}
threepio: droid(id: "2000") {
id
name
friends {
id
name
appearsIn
}
appearsIn
PrimaryFunction
}
artoo: droid(id: "2001") {
id
name
friends {
id
name
appearsIn
}
appearsIn
PrimaryFunction
}
}
`
benchmarkQuery(b, testutil.StarWarsSchema, query)
}

func BenchmarkQuery_DeeperQueryWithManyRootFieldsAndList(b *testing.B) {
query := `
{
hero {
id
name
friends {
name
friends {
id
name
appearsIn
}
}
appearsIn
}
vader: human(id: "1001") {
id
name
friends {
id
name
appearsIn
friends {
id
name
appearsIn
}
}
appearsIn
homePlanet
}
leia: human(id: "1003") {
id
name
friends {
id
name
appearsIn
friends {
id
name
appearsIn
}
}
appearsIn
homePlanet
}
threepio: droid(id: "2000") {
id
name
friends {
id
name
appearsIn
friends {
id
name
appearsIn
}
}
appearsIn
PrimaryFunction
}
artoo: droid(id: "2001") {
id
name
friends {
id
name
appearsIn
friends {
id
name
appearsIn
}
}
appearsIn
PrimaryFunction
}
}
`
benchmarkQuery(b, testutil.StarWarsSchema, query)
}
4 changes: 4 additions & 0 deletions testutil/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"reflect"
"strconv"
"testing"
"time"

"github.com/graphql-go/graphql"
"github.com/graphql-go/graphql/language/ast"
Expand Down Expand Up @@ -323,18 +324,21 @@ func init() {
}

func GetHuman(id int) StarWarsChar {
time.Sleep(1 * time.Millisecond)
if human, ok := HumanData[id]; ok {
return human
}
return StarWarsChar{}
}
func GetDroid(id int) StarWarsChar {
time.Sleep(1 * time.Millisecond)
if droid, ok := DroidData[id]; ok {
return droid
}
return StarWarsChar{}
}
func GetHero(episode interface{}) interface{} {
time.Sleep(1 * time.Millisecond)
if episode == 5 {
return Luke
}
Expand Down

0 comments on commit 8a5ddd4

Please sign in to comment.