-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathvalidation_overlap_bench_test.go
More file actions
95 lines (85 loc) · 2.37 KB
/
validation_overlap_bench_test.go
File metadata and controls
95 lines (85 loc) · 2.37 KB
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 := range count {
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 := range top {
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 := range fragA {
frags.WriteString(" a")
frags.WriteString(strconv.Itoa(i))
frags.WriteString(":id")
}
frags.WriteString(" }")
frags.WriteString(" fragment FragB on Thing {")
for i := range fragB {
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)
}
})
}