File: bench_example_test.go

package info (click to toggle)
golang-github-puerkitobio-goquery 1.10.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 880 kB
  • sloc: sh: 27; makefile: 3
file content (40 lines) | stat: -rw-r--r-- 922 bytes parent folder | download | duplicates (3)
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
package goquery

import (
	"bytes"
	"fmt"
	"strconv"
	"testing"
)

func BenchmarkMetalReviewExample(b *testing.B) {
	var n int
	var buf bytes.Buffer

	b.StopTimer()
	doc := loadDoc("metalreview.html")
	b.StartTimer()
	for i := 0; i < b.N; i++ {
		doc.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) {
			var band, title string
			var score float64
			var e error

			n++
			// For each item found, get the band, title and score, and print it
			band = s.Find("strong").Text()
			title = s.Find("em").Text()
			if score, e = strconv.ParseFloat(s.Find(".score").Text(), 64); e != nil {
				// Not a valid float, ignore score
				if n <= 4 {
					buf.WriteString(fmt.Sprintf("Review %d: %s - %s.\n", i, band, title))
				}
			} else {
				// Print all, including score
				if n <= 4 {
					buf.WriteString(fmt.Sprintf("Review %d: %s - %s (%2.1f).\n", i, band, title, score))
				}
			}
		})
	}
}