bench_example_test.go 922 B

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