-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark_test.go
99 lines (90 loc) · 2.35 KB
/
benchmark_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
package benchmark
import (
"errors"
"io/ioutil"
"sync"
"testing"
"github.com/anthony-dong/protobuf"
"github.com/jhump/protoreflect/desc/protoparse"
descriptor "google.golang.org/protobuf/types/descriptorpb"
)
// Benchmark_ParsePBFileDesc_Cgo
// use "github.com/anthony-dong/protobuf"
func Benchmark_ParsePBFileDesc_Cgo(b *testing.B) {
b.StopTimer()
mainIdl := loadIdl(b)
b.StartTimer()
for i := 0; i < b.N; i++ {
rr, err := protobuf.ParsePBFileDesc(mainIdl, protobuf.WithRequireSyntaxIdentifier())
if err != nil {
b.Fatal(err)
}
if rr == nil {
b.Fatal("must error")
}
}
}
// Benchmark_ParsePBFileDesc_Jhump
// use "github.com/jhump/protoreflect"
func Benchmark_ParsePBFileDesc_Jhump(b *testing.B) {
b.StopTimer()
mainIdl := string(loadIdl(b))
mainIdlName := "main.proto"
idlMap := map[string]string{mainIdlName: mainIdl}
b.StartTimer()
for i := 0; i < b.N; i++ {
rr, err := ParsePBFileDescJhump(idlMap, mainIdlName)
if err != nil {
b.Fatal(err)
}
if rr == nil {
b.Fatal("must error")
}
}
}
func ParsePBFileDescJhump(idls map[string]string, main string) (*descriptor.FileDescriptorProto, error) {
var pbParser protoparse.Parser
pbParser.Accessor = protoparse.FileContentsFromMap(idls)
//pbParser.IncludeSourceCodeInfo = true // 关闭这个可以降低内存开销. 尤其对于大型idl来说!
pbParser.ValidateUnlinkedFiles = true
pbParser.InterpretOptionsInUnlinkedFiles = true
fds, err := pbParser.ParseFiles(main)
if err != nil {
return nil, err
}
for _, fd := range fds {
return fd.AsFileDescriptorProto(), nil
}
return nil, errors.New("no file desc")
}
func TestParsePBFileDesc(t *testing.T) {
mainIdl := string(loadIdl(t))
mainIdlName := "main.proto"
idlMap := map[string]string{mainIdlName: mainIdl}
t.Run("jhump", func(t *testing.T) {
result, err := ParsePBFileDescJhump(idlMap, mainIdlName)
if err != nil {
t.Fatal(err)
}
t.Log(protobuf.MessageToJson(result, true))
})
t.Run("cgo", func(t *testing.T) {
result, err := protobuf.ParsePBFileDesc(loadIdl(t), protobuf.WithRequireSyntaxIdentifier())
if err != nil {
t.Fatal(err)
}
t.Log(protobuf.MessageToJson(result, true))
})
}
var initOnce sync.Once
var idl []byte
func loadIdl(t testing.TB) []byte {
initOnce.Do(func() {
file, err := ioutil.ReadFile("../idl/test.proto")
if err != nil {
t.Fatal(err)
}
idl = file
})
return idl
}