-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark_test.go
131 lines (117 loc) · 2.7 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
package xmltokenizer_test
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"io/fs"
"os"
"path/filepath"
"strings"
"testing"
"github.com/muktihari/xmltokenizer"
"github.com/muktihari/xmltokenizer/internal/gpx"
"github.com/muktihari/xmltokenizer/internal/xlsx"
)
func BenchmarkToken(b *testing.B) {
filepath.Walk("testdata", func(path string, info fs.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
name := strings.TrimPrefix(path, "testdata/")
data, err := os.ReadFile(path)
if err != nil {
b.Logf("%v: %v", path, err)
return nil
}
b.Run(fmt.Sprintf("stdlib.xml:%q", name), func(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
if err = unmarshalWithStdlibXML(bytes.NewReader(data)); err != nil {
b.Skipf("could not unmarshal: %v", err)
}
}
})
b.Run(fmt.Sprintf("xmltokenizer:%q", name), func(b *testing.B) {
var err error
for i := 0; i < b.N; i++ {
if err = unmarshalWithXMLTokenizer(bytes.NewReader(data)); err != nil {
b.Skipf("could not unmarshal: %v", err)
}
}
})
return nil
})
}
func unmarshalWithXMLTokenizer(r io.Reader) error {
tok := xmltokenizer.New(r)
for {
token, err := tok.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
_ = token
}
return nil
}
func unmarshalWithStdlibXML(r io.Reader) error {
dec := xml.NewDecoder(r)
for {
token, err := dec.Token()
if err == io.EOF {
break
}
if err != nil {
return err
}
_ = token
}
return nil
}
func BenchmarkUnmarshalGPX(b *testing.B) {
filepath.Walk("testdata", func(path string, info fs.FileInfo, _ error) error {
if info.IsDir() {
return nil
}
if strings.ToLower(filepath.Ext(path)) != ".gpx" {
return nil
}
name := strings.TrimPrefix(path, "testdata/")
data, err := os.ReadFile(path)
if err != nil {
panic(err)
}
b.Run(fmt.Sprintf("stdlib.xml:%q", name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gpx.UnmarshalWithStdlibXML(bytes.NewReader(data))
}
})
b.Run(fmt.Sprintf("xmltokenizer:%q", name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = gpx.UnmarshalWithXMLTokenizer(bytes.NewReader(data))
}
})
return nil
})
}
func BenchmarkUnmarshalXLSX(b *testing.B) {
path := filepath.Join("testdata", "xlsx_sheet1.xml")
name := strings.TrimPrefix(path, "testdata/")
data, err := os.ReadFile(path)
if err != nil {
panic(err)
}
b.Run(fmt.Sprintf("stdlib.xml:%q", name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = xlsx.UnmarshalWithStdlibXML(bytes.NewReader(data))
}
})
b.Run(fmt.Sprintf("xmltokenizer:%q", name), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, _ = xlsx.UnmarshalWithXMLTokenizer(bytes.NewReader(data))
}
})
}