start add benchmarks for filter.go

This commit is contained in:
Martin Angers
2012-09-11 15:36:21 -04:00
parent 489bc34355
commit 30f50b483f
3 changed files with 1300 additions and 0 deletions

79
bench_filter_test.go Normal file
View File

@@ -0,0 +1,79 @@
package goquery
import (
"testing"
)
func BenchmarkFilter(b *testing.B) {
var n int
b.StopTimer()
sel := DocW().Root.Find("li")
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
n = sel.Filter(".toclevel-1").Length()
} else {
sel.Filter(".toclevel-1")
}
}
b.Logf("Filter=%d", n)
}
func BenchmarkNot(b *testing.B) {
var n int
b.StopTimer()
sel := DocW().Root.Find("li")
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
n = sel.Not(".toclevel-2").Length()
} else {
sel.Filter(".toclevel-2")
}
}
b.Logf("Not=%d", n)
}
func BenchmarkFilterFunction(b *testing.B) {
var n int
b.StopTimer()
sel := DocW().Root.Find("li")
f := func(i int, s *Selection) bool {
return len(s.Get(0).Attr) > 0
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
n = sel.FilterFunction(f).Length()
} else {
sel.FilterFunction(f)
}
}
b.Logf("FilterFunction=%d", n)
}
func BenchmarkNotFunction(b *testing.B) {
var n int
b.StopTimer()
sel := DocW().Root.Find("li")
f := func(i int, s *Selection) bool {
return len(s.Get(0).Attr) > 0
}
b.StartTimer()
for i := 0; i < b.N; i++ {
if n == 0 {
n = sel.NotFunction(f).Length()
} else {
sel.NotFunction(f)
}
}
b.Logf("NotFunction=%d", n)
}
func BenchmarkFilterNodes(b *testing.B) {
}

1214
testdata/gowiki.html vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -11,6 +11,7 @@ import (
var doc *Document
var doc2 *Document
var docB *Document
var docW *Document
func Doc() *Document {
if doc == nil {
@@ -30,6 +31,12 @@ func DocB() *Document {
}
return docB
}
func DocW() *Document {
if docW == nil {
docW = LoadDoc("gowiki.html")
}
return docW
}
func AssertLength(t *testing.T, nodes []*html.Node, length int) {
if len(nodes) != length {