mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-12-24 12:38:00 +08:00
start add benchmarks for filter.go
This commit is contained in:
79
bench_filter_test.go
Normal file
79
bench_filter_test.go
Normal 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
1214
testdata/gowiki.html
vendored
Normal file
File diff suppressed because it is too large
Load Diff
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user