Skip to content

Commit

Permalink
Add a benchmark
Browse files Browse the repository at this point in the history
go test -benchtime=5s -bench=. -benchmem
goos: linux
goarch: amd64
pkg: github.com/99designs/gqlgen/example/starwars
BenchmarkSimpleQueryNoArgs-8      200000             32680 ns/op            6357 B/op        126 allocs/op
PASS
ok      github.com/99designs/gqlgen/example/starwars    9.901s
  • Loading branch information
vektah committed Dec 8, 2018
1 parent 5c870a4 commit 2cf5a5b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
/codegen/gen

.idea/
*.test
*.out
30 changes: 30 additions & 0 deletions example/starwars/benchmarks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package starwars

import (
"net/http/httptest"
"strings"
"testing"

"github.com/99designs/gqlgen/handler"
)

func BenchmarkSimpleQueryNoArgs(b *testing.B) {
server := handler.GraphQL(NewExecutableSchema(NewResolver()))

q := `{"query":"{ search(text:\"Luke\") { ... on Human { starships { name } } } }"}`

var body strings.Reader
r := httptest.NewRequest("POST", "/graphql", &body)

b.ResetTimer()
rec := httptest.NewRecorder()
for i := 0; i < b.N; i++ {
body.Reset(q)
rec.Body.Reset()
server.ServeHTTP(rec, r)
if rec.Body.String() != `{"data":{"search":[{"starships":[{"name":"X-Wing"},{"name":"Imperial shuttle"}]}]}}` {
b.Fatalf("Unexpected response")
}

}
}

0 comments on commit 2cf5a5b

Please sign in to comment.