
diff --git a/bench_example_test.go b/bench_example_test.go new file mode 100644 index 0000000..07f7c82 --- /dev/null +++ b/bench_example_test.go @@ -0,0 +1,42 @@ +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.Root.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)) + } + } + }) + } + b.Log(buf.String()) + b.Logf("MetalReviewExample=%d", n) +} diff --git a/testdata/metalreview.html b/testdata/metalreview.html new file mode 100644 index 0000000..fc4a38f --- /dev/null +++ b/testdata/metalreview.html @@ -0,0 +1,413 @@ + + + + + +
+ + + + +
+
+ Serpentine Path
+ Serpentine Path
+
+
+
+ Hunter's Ground
+ No God But the Wild
+
+
+
+ Blut Aus Nord
+ 777 - Cosmosophy
+ Black
+
+
+ Ufomammut
+ Oro: Opus Alter
+ Doom
+
+
+ Resurgency
+ False Enlightenment
+ Death
+
+
+ Krallice
+ Years Past Matter
+ Black
+
+
+ Murder Construct
+ Results
+ Grindcore
+
+
+ Grave
+ Endless Procession of Souls
+ Death
+
+
+ Master
+ The New Elite
+ Death
+