From 2cf5a5b8049198e130ccdcd8068cfe321b82e17e Mon Sep 17 00:00:00 2001 From: Adam Scarr Date: Tue, 27 Nov 2018 19:26:02 +1100 Subject: [PATCH] Add a benchmark 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 --- .gitignore | 2 ++ example/starwars/benchmarks_test.go | 30 +++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 example/starwars/benchmarks_test.go diff --git a/.gitignore b/.gitignore index a9712beb0b..c0ff0ef28b 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,5 @@ /codegen/gen .idea/ +*.test +*.out diff --git a/example/starwars/benchmarks_test.go b/example/starwars/benchmarks_test.go new file mode 100644 index 0000000000..8386b7963f --- /dev/null +++ b/example/starwars/benchmarks_test.go @@ -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") + } + + } +}