aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/PuerkitoBio/goquery/bench_iteration_test.go
blob: 39445b0a642a118b8b31c0fc2d72ace4a9fffe75 (plain)
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
package goquery

import (
	"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 BenchmarkMap(b *testing.B) {
	var tmp, n int

	b.StopTimer()
	sel := DocW().Find("td")
	f := func(i int, s *Selection) string {
		tmp++
		return string(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)
	}
}