-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbench_test.go
110 lines (101 loc) · 2.18 KB
/
bench_test.go
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package gqlscan_test
import (
"testing"
"github.com/dustin/go-humanize"
"github.com/graph-guard/gqlscan"
)
func BenchmarkScan(b *testing.B) {
for _, td := range testdata {
b.Run(td.decl, func(b *testing.B) {
in := []byte(td.input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := gqlscan.Scan(in, func(*gqlscan.Iterator) (err bool) {
return false
}); err.IsErr() {
panic(err)
}
}
})
}
}
func BenchmarkScanAll(b *testing.B) {
for _, td := range testdata {
b.Run(td.decl, func(b *testing.B) {
in := []byte(td.input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := gqlscan.ScanAll(in, func(*gqlscan.Iterator) {
}); err.IsErr() {
panic(err)
}
}
})
}
}
func BenchmarkScanErr(b *testing.B) {
for _, td := range testdataErr {
b.Run(td.decl, func(b *testing.B) {
in := []byte(td.input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := gqlscan.Scan(in, func(*gqlscan.Iterator) (err bool) {
return false
}); !err.IsErr() {
panic("unexpected success")
}
}
})
}
}
func BenchmarkScanAllErr(b *testing.B) {
for _, td := range testdataErr {
b.Run(td.decl, func(b *testing.B) {
in := []byte(td.input)
b.ResetTimer()
for i := 0; i < b.N; i++ {
if err := gqlscan.ScanAll(in, func(*gqlscan.Iterator) {
}); !err.IsErr() {
panic("unexpected success")
}
}
})
}
}
var InterpretedBuffer []byte
func BenchmarkScanInterpreted(b *testing.B) {
for _, td := range testdataBlockStrings {
b.Run(td.Decl, func(b *testing.B) {
for _, bufSize := range []int{
1, 1024, 65536,
} {
b.Run(humanize.Bytes(uint64(bufSize)), func(b *testing.B) {
input := []byte(td.Input)
buffer := make([]byte, bufSize)
b.ResetTimer()
for n := 0; n < b.N; n++ {
c := 0
if err := gqlscan.ScanAll(
input,
func(i *gqlscan.Iterator) {
c++
if c != td.TokenIndex {
return
}
i.ScanInterpreted(
buffer,
func(buffer []byte) (stop bool) {
InterpretedBuffer = buffer
return false
},
)
},
); err.IsErr() {
panic(err)
}
}
})
}
})
}
}