forked from lestrrat-go/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libxml2_bench_test.go
160 lines (139 loc) · 3.02 KB
/
libxml2_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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// This file is build-tag protected because it involves loading an external
// library (xmlpath)
package libxml2_test
import (
"bytes"
"encoding/xml"
"os"
"path/filepath"
"testing"
"github.com/lestrrat-go/libxml2"
"github.com/lestrrat-go/libxml2/dom"
"github.com/lestrrat-go/libxml2/xpath"
"github.com/stretchr/testify/assert"
"gopkg.in/xmlpath.v1"
)
var xmlfile = filepath.Join("test", "feed.atom")
func BenchmarkXmlpathXmlpath(b *testing.B) {
f, err := os.Open(xmlfile)
if err != nil {
b.Fatalf("%s", err)
}
root, err := xmlpath.Parse(f)
if err != nil {
b.Fatalf("%s", err)
}
for i := 0; i < b.N; i++ {
p, err := xmlpath.Compile(`//entry`)
if err != nil {
b.Fatalf("%s", err)
}
it := p.Iter(root)
for it.Next() {
n := it.Node()
_ = n
}
}
}
func TestBenchmarkLibxml2Xmlpath(t *testing.T) {
f, err := os.Open(xmlfile)
if !assert.NoError(t, err, "os.Open succeeds") {
return
}
doc, err := libxml2.ParseReader(f)
if !assert.NoError(t, err, "ParseReader succeeds") {
return
}
xpc, err := xpath.NewContext(doc)
if !assert.NoError(t, err, "xpath.NewContext succeeds") {
return
}
xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
res, err := xpc.Find(`//atom:entry`)
if !assert.NoError(t, err, "xpc.Find succeeds") {
return
}
defer res.Free()
iter := res.NodeIter()
if !assert.NotEmpty(t, iter, "res.NodeIter succeeds") {
return
}
count := 0
for iter.Next() {
n := iter.Node()
if !assert.NotEmpty(t, n, "iter.Node returns something") {
return
}
count++
}
if !assert.True(t, count > 0, "there's at least 1 node") {
return
}
}
func BenchmarkLibxml2Xmlpath(b *testing.B) {
f, err := os.Open(xmlfile)
if err != nil {
b.Fatalf("%s", err)
}
doc, err := libxml2.ParseReader(f)
if err != nil {
b.Fatalf("%s", err)
}
xpc, err := xpath.NewContext(doc)
if err != nil {
b.Fatalf("%s", err)
}
xpc.RegisterNS("atom", "http://www.w3.org/2005/Atom")
for i := 0; i < b.N; i++ {
iter := xpath.NodeIter(xpc.Find(`//atom:entry`))
for iter.Next() {
n := iter.Node()
_ = n
}
}
}
type Foo struct {
XMLName xml.Name `xml:"https://github.com/lestrrat-go/libxml2/foo foo:foo"`
Field1 string
Field2 string `xml:",attr"`
}
func BenchmarkEncodingXMLDOM(b *testing.B) {
var buf bytes.Buffer
f := Foo{
Field1: "Hello, World!",
Field2: "Hello, Attribute!",
}
for i := 0; i < b.N; i++ {
buf.Reset()
enc := xml.NewEncoder(&buf)
enc.Encode(f)
}
}
func BenchmarkLibxml2DOM(b *testing.B) {
var buf bytes.Buffer
const nsuri = `https://github.com/lestrrat-go/libxml2/foo`
f := Foo{
Field1: "Hello, World!",
Field2: "Hello, Attribute!",
}
for i := 0; i < b.N; i++ {
d := dom.CreateDocument()
root, err := d.CreateElementNS(nsuri, "foo:foo")
if err != nil {
d.Free()
panic(err)
}
d.SetDocumentElement(root)
f1xml, err := d.CreateElement("Field1")
if err != nil {
d.Free()
panic(err)
}
root.AddChild(f1xml)
f1xml.SetAttribute("Field2", f.Field2)
f1xml.AppendText(f.Field1)
buf.Reset()
buf.WriteString(d.Dump(false))
d.Free()
}
}