-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
109 lines (104 loc) · 1.83 KB
/
parser_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
package fastparse
import (
"bytes"
"io"
"sync"
"testing"
)
func TestParser(t *testing.T) {
m := NewParserManager(6, 1)
r := bytes.NewReader([]byte("a \"test\""))
p := m.Parser(r)
defer p.Done()
a := p.GetNextArg()
if a == nil {
t.Fatal("a shouldn't be nil")
return
}
if a.Text != "a" {
t.Fatal("should be a")
return
}
a = p.GetNextArg()
if a.Text != "test" {
t.Fatal("should be test")
return
}
err := a.Rewind()
if err != nil {
t.Fatal(err)
return
}
s, err := p.Remainder()
if err != nil {
t.Fatal(err)
return
}
if s != "\"test\"" {
t.Fatal("should be \"test\"")
return
}
}
func BenchmarkParser_FullyPreAllocated(b *testing.B) {
n := 10000
m := NewParserManager(2000, n)
readers := make([]io.ReadSeeker, n)
for i := range readers {
readers[i] = bytes.NewReader([]byte("\"test\""))
}
wg := sync.WaitGroup{}
wg.Add(n)
b.ResetTimer()
for i := 0; i < n; i++ {
r := readers[i]
go func() {
defer wg.Done()
p := m.Parser(r)
p.GetNextArg()
p.Done()
}()
}
wg.Wait()
}
func BenchmarkParser_HalfPreAllocated(b *testing.B) {
n := 10000
m := NewParserManager(2000, n)
readers := make([]io.ReadSeeker, n*2)
for i := range readers {
readers[i] = bytes.NewReader([]byte("\"test\""))
}
wg := sync.WaitGroup{}
wg.Add(n*2)
b.ResetTimer()
for i := 0; i < n*2; i++ {
r := readers[i]
go func() {
defer wg.Done()
p := m.Parser(r)
p.GetNextArg()
p.Done()
}()
}
wg.Wait()
}
func BenchmarkParser_NotPreAllocated(b *testing.B) {
n := 10000
m := NewParserManager(2000, 0)
readers := make([]io.ReadSeeker, n)
for i := range readers {
readers[i] = bytes.NewReader([]byte("\"test\""))
}
wg := sync.WaitGroup{}
wg.Add(n)
b.ResetTimer()
for i := 0; i < n; i++ {
r := readers[i]
go func() {
defer wg.Done()
p := m.Parser(r)
p.GetNextArg()
p.Done()
}()
}
wg.Wait()
}