File: bench_iteration_test.go

package info (click to toggle)
golang-github-puerkitobio-goquery 1.1.0%2Bgit20170324.3.ed7d758-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, bullseye-backports
  • size: 1,036 kB
  • sloc: sh: 39; makefile: 3
file content (68 lines) | stat: -rw-r--r-- 959 bytes parent folder | download | duplicates (2)
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)
	}
}