| 8 | ) |
| 9 | |
| 10 | func BenchmarkMetalReviewExample(b *testing.B) { |
| 11 | var n int |
| 12 | var builder strings.Builder |
| 13 | |
| 14 | b.StopTimer() |
| 15 | doc := loadDoc("metalreview.html") |
| 16 | b.StartTimer() |
| 17 | for i := 0; i < b.N; i++ { |
| 18 | doc.Find(".slider-row:nth-child(1) .slider-item").Each(func(i int, s *Selection) { |
| 19 | var band, title string |
| 20 | var score float64 |
| 21 | var e error |
| 22 | |
| 23 | n++ |
| 24 | // For each item found, get the band, title and score, and print it |
| 25 | band = s.Find("strong").Text() |
| 26 | title = s.Find("em").Text() |
| 27 | if score, e = strconv.ParseFloat(s.Find(".score").Text(), 64); e != nil { |
| 28 | // Not a valid float, ignore score |
| 29 | if n <= 4 { |
| 30 | builder.WriteString(fmt.Sprintf("Review %d: %s - %s.\n", i, band, title)) |
| 31 | } |
| 32 | } else { |
| 33 | // Print all, including score |
| 34 | if n <= 4 { |
| 35 | builder.WriteString(fmt.Sprintf("Review %d: %s - %s (%2.1f).\n", i, band, title, score)) |
| 36 | } |
| 37 | } |
| 38 | }) |
| 39 | } |
| 40 | } |