File: validation_overlap_bench_test.go

package info (click to toggle)
golang-github-graph-gophers-graphql-go 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,516 kB
  • sloc: sh: 373; javascript: 21; makefile: 5
file content (95 lines) | stat: -rw-r--r-- 2,453 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package graphql_test

import (
	"context"
	"strconv"
	"strings"
	"testing"

	graphql "github.com/graph-gophers/graphql-go"
)

const overlapBenchSchema = `schema { query: Query } type Query { root: Thing } type Thing { id: ID! name: String value: String }`

func buildLargeQuery(count int) string {
	var b strings.Builder
	b.Grow(20 + count*8)
	b.WriteString("query{root{")
	for i := 0; i < count; i++ {
		b.WriteString("f")
		b.WriteString(strconv.Itoa(i))
		b.WriteString(":id ")
	}
	b.WriteString("}}")
	return b.String()
}

func buildFragmentedQuery(total int) string {
	if total < 4 {
		return buildLargeQuery(total)
	}
	top := total / 2
	rest := total - top
	fragA := rest / 2
	fragB := rest - fragA
	var topSel strings.Builder
	topSel.Grow(32 + top*8)
	topSel.WriteString("query{root{")
	for i := 0; i < top; i++ {
		topSel.WriteString("t")
		topSel.WriteString(strconv.Itoa(i))
		topSel.WriteString(":id ")
	}
	topSel.WriteString(" ...FragA ...FragB }}")
	var frags strings.Builder
	frags.Grow(32 + (fragA+fragB)*8)
	frags.WriteString(" fragment FragA on Thing {")
	for i := 0; i < fragA; i++ {
		frags.WriteString(" a")
		frags.WriteString(strconv.Itoa(i))
		frags.WriteString(":id")
	}
	frags.WriteString(" }")
	frags.WriteString(" fragment FragB on Thing {")
	for i := 0; i < fragB; i++ {
		frags.WriteString(" b")
		frags.WriteString(strconv.Itoa(i))
		frags.WriteString(":id")
	}
	frags.WriteString(" }")
	return topSel.String() + frags.String()
}

type overlapRoot struct{}

func (r *overlapRoot) Root() *thingResolver { return &thingResolver{} }

type thingResolver struct{}

func (t *thingResolver) ID() graphql.ID { return graphql.ID("1") }
func (t *thingResolver) Name() *string  { s := "n"; return &s }
func (t *thingResolver) Value() *string { s := "v"; return &s }

func BenchmarkValidateOverlap(b *testing.B) {
	sizes := []int{500, 1000, 2000, 5000}
	for _, n := range sizes {
		b.Run("fields_"+strconv.Itoa(n), func(b *testing.B) {
			schema := graphql.MustParseSchema(overlapBenchSchema, &overlapRoot{})
			query := buildLargeQuery(n)
			ctx := context.Background()
			b.ReportAllocs()
			for b.Loop() {
				_ = schema.Exec(ctx, query, "", nil)
			}
		})
	}
	b.Run("fragments_1000", func(b *testing.B) {
		schema := graphql.MustParseSchema(overlapBenchSchema, &overlapRoot{})
		query := buildFragmentedQuery(1000)
		ctx := context.Background()
		b.ReportAllocs()
		for b.Loop() {
			_ = schema.Exec(ctx, query, "", nil)
		}
	})
}