-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbenchmark_stdlib_test.go
76 lines (67 loc) · 1.68 KB
/
benchmark_stdlib_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
package tokeq
import (
"io"
"strings"
"testing"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func benchStandardLibraryToken(r io.Reader, match func(tt html.TokenType, a atom.Atom) bool, output func(html.Token)) {
parser := html.NewTokenizer(r)
for {
tt := parser.Next() // TokenType
if tt == html.ErrorToken {
break
}
token := parser.Token()
switch tt {
case html.StartTagToken:
if match(tt, token.DataAtom) {
output(token)
}
case html.TextToken:
if match(tt, token.DataAtom) {
output(token)
}
case html.EndTagToken:
if match(tt, token.DataAtom) {
output(token)
}
case html.SelfClosingTagToken:
if match(tt, token.DataAtom) {
output(token)
}
case html.CommentToken:
if match(tt, token.DataAtom) {
output(token)
}
case html.DoctypeToken:
if match(tt, token.DataAtom) {
output(token)
}
}
}
}
func BenchmarkStandardLibraryTokenFindP(t *testing.B) {
for n := 0; n < t.N; n++ {
benchStandardLibraryToken(strings.NewReader(benchTestHTML), func(t html.TokenType, a atom.Atom) bool {
return t == html.StartTagToken && a == atom.P
}, func(t html.Token) {})
}
}
func benchStandardLibraryNode(input *html.Node, match func(n *html.Node) bool, output func(n *html.Node)) {
for c := input.FirstChild; c != nil; c = c.NextSibling {
if match(c) {
output(c)
}
benchStandardLibraryNode(c, match, output)
}
}
func BenchmarkStandardLibraryNodeFindP(t *testing.B) {
document, _ := html.Parse(strings.NewReader(benchTestHTML))
for n := 0; n < t.N; n++ {
benchStandardLibraryNode(document, func(n *html.Node) bool {
return n.Type == html.ElementNode && n.DataAtom == atom.P
}, func(n *html.Node) {})
}
}