mirror of
https://github.com/PuerkitoBio/goquery
synced 2025-09-26 21:01:21 +08:00

- We don't need to use the iter.Seq2 type; instead, we can use func(yield func(int, *Selection) bool). In fact, the underlying type of iter.Seq2 is func(yield func(int, *Selection) bool), so we don't need to upgrade the go.mod version. - For for-range testing in version 1.23, we can use build tags to compile only for versions above 1.23.
112 lines
1.6 KiB
Go
112 lines
1.6 KiB
Go
package goquery
|
|
|
|
import (
|
|
"strconv"
|
|
"testing"
|
|
)
|
|
|
|
func BenchmarkEach(b *testing.B) {
|
|
var tmp, n int
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("td")
|
|
f := func(i int, s *Selection) {
|
|
tmp++
|
|
}
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sel.Each(f)
|
|
if n == 0 {
|
|
n = tmp
|
|
}
|
|
}
|
|
if n != 59 {
|
|
b.Fatalf("want 59, got %d", n)
|
|
}
|
|
}
|
|
|
|
func BenchmarkEachIter(b *testing.B) {
|
|
var tmp, n int
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("td")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sel.EachIter()(func(i int, s *Selection) bool {
|
|
tmp++
|
|
return true
|
|
})
|
|
if n == 0 {
|
|
n = tmp
|
|
}
|
|
}
|
|
if n != 59 {
|
|
b.Fatalf("want 59, got %d", n)
|
|
}
|
|
}
|
|
|
|
func BenchmarkEachIterWithBreak(b *testing.B) {
|
|
var tmp, n int
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("td")
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
tmp = 0
|
|
sel.EachIter()(func(i int, s *Selection) bool {
|
|
tmp++
|
|
return tmp < 10
|
|
})
|
|
|
|
if n == 0 {
|
|
n = tmp
|
|
}
|
|
}
|
|
if n != 10 {
|
|
b.Fatalf("want 10, got %d", n)
|
|
}
|
|
}
|
|
|
|
func BenchmarkMap(b *testing.B) {
|
|
var tmp, n int
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("td")
|
|
f := func(i int, s *Selection) string {
|
|
tmp++
|
|
return strconv.Itoa(tmp)
|
|
}
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
sel.Map(f)
|
|
if n == 0 {
|
|
n = tmp
|
|
}
|
|
}
|
|
if n != 59 {
|
|
b.Fatalf("want 59, got %d", n)
|
|
}
|
|
}
|
|
|
|
func BenchmarkEachWithBreak(b *testing.B) {
|
|
var tmp, n int
|
|
|
|
b.StopTimer()
|
|
sel := DocW().Find("td")
|
|
f := func(i int, s *Selection) bool {
|
|
tmp++
|
|
return tmp < 10
|
|
}
|
|
b.StartTimer()
|
|
for i := 0; i < b.N; i++ {
|
|
tmp = 0
|
|
sel.EachWithBreak(f)
|
|
if n == 0 {
|
|
n = tmp
|
|
}
|
|
}
|
|
if n != 10 {
|
|
b.Fatalf("want 10, got %d", n)
|
|
}
|
|
}
|