-
Notifications
You must be signed in to change notification settings - Fork 3
/
bench_test.go
61 lines (56 loc) · 982 Bytes
/
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
package gosax_test
import (
"bytes"
"os"
"testing"
"github.com/orisano/gosax"
)
func BenchmarkReader_Event(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
if err := countAfrica(b); err != nil {
b.Fatal(err)
}
}
}
func countAfrica(b *testing.B) error {
f, err := os.Open("testdata/out.xml")
if err != nil {
return err
}
defer f.Close()
if stat, err := f.Stat(); err == nil {
b.SetBytes(stat.Size())
}
r := gosax.NewReader(f)
count := 0
inLocation := false
for {
e, err := r.Event()
if err != nil {
return err
}
if e.Type() == gosax.EventEOF {
break
}
switch e.Type() {
case gosax.EventStart:
name, _ := gosax.Name(e.Bytes)
if string(name) == "location" {
inLocation = true
} else {
inLocation = false
}
case gosax.EventEnd:
inLocation = false
case gosax.EventText:
if inLocation {
if bytes.Contains(e.Bytes, []byte("Africa")) {
count++
}
}
default:
}
}
return nil
}