github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/goquery/bench_example_test.go (about)

     1  package goquery
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"strconv"
     7  	"testing"
     8  )
     9  
    10  func BenchmarkMetalReviewExample(b *testing.B) {
    11  	var n int
    12  	var buf bytes.Buffer
    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  					buf.WriteString(fmt.Sprintf("Review %d: %s - %s.\n", i, band, title))
    31  				}
    32  			} else {
    33  				// Print all, including score
    34  				if n <= 4 {
    35  					buf.WriteString(fmt.Sprintf("Review %d: %s - %s (%2.1f).\n", i, band, title, score))
    36  				}
    37  			}
    38  		})
    39  	}
    40  	b.Log(buf.String())
    41  	b.Logf("MetalReviewExample=%d", n)
    42  }