Make top score collector about 7 times faster
This commit is contained in:
parent
6d830a9f3e
commit
f2aba116c4
32
search/collectors/bench_test.go
Normal file
32
search/collectors/bench_test.go
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
package collectors
|
||||||
|
|
||||||
|
import (
|
||||||
|
"math/rand"
|
||||||
|
"strconv"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/blevesearch/bleve/search"
|
||||||
|
"golang.org/x/net/context"
|
||||||
|
)
|
||||||
|
|
||||||
|
func benchHelper(numOfMatches int, collector search.Collector, b *testing.B) {
|
||||||
|
matches := make(search.DocumentMatchCollection, 0, numOfMatches)
|
||||||
|
for i := 0; i < numOfMatches; i++ {
|
||||||
|
matches = append(matches, &search.DocumentMatch{
|
||||||
|
ID: strconv.Itoa(i),
|
||||||
|
Score: rand.Float64(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for run := 0; run < b.N; run++ {
|
||||||
|
searcher := &stubSearcher{
|
||||||
|
matches: matches,
|
||||||
|
}
|
||||||
|
err := collector.Collect(context.Background(), searcher)
|
||||||
|
if err != nil {
|
||||||
|
b.Fatal(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -24,6 +24,7 @@ type TopScoreCollector struct {
|
||||||
results *list.List
|
results *list.List
|
||||||
took time.Duration
|
took time.Duration
|
||||||
maxScore float64
|
maxScore float64
|
||||||
|
minScore float64
|
||||||
total uint64
|
total uint64
|
||||||
facetsBuilder *search.FacetsBuilder
|
facetsBuilder *search.FacetsBuilder
|
||||||
}
|
}
|
||||||
|
@ -98,6 +99,10 @@ func (tksc *TopScoreCollector) collectSingle(dm *search.DocumentMatch) {
|
||||||
tksc.maxScore = dm.Score
|
tksc.maxScore = dm.Score
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if dm.Score <= tksc.minScore {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
for e := tksc.results.Front(); e != nil; e = e.Next() {
|
for e := tksc.results.Front(); e != nil; e = e.Next() {
|
||||||
curr := e.Value.(*search.DocumentMatch)
|
curr := e.Value.(*search.DocumentMatch)
|
||||||
if dm.Score < curr.Score {
|
if dm.Score < curr.Score {
|
||||||
|
@ -106,7 +111,7 @@ func (tksc *TopScoreCollector) collectSingle(dm *search.DocumentMatch) {
|
||||||
// if we just made the list too long
|
// if we just made the list too long
|
||||||
if tksc.results.Len() > (tksc.k + tksc.skip) {
|
if tksc.results.Len() > (tksc.k + tksc.skip) {
|
||||||
// remove the head
|
// remove the head
|
||||||
tksc.results.Remove(tksc.results.Front())
|
tksc.minScore = tksc.results.Remove(tksc.results.Front()).(*search.DocumentMatch).Score
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -115,7 +120,7 @@ func (tksc *TopScoreCollector) collectSingle(dm *search.DocumentMatch) {
|
||||||
tksc.results.PushBack(dm)
|
tksc.results.PushBack(dm)
|
||||||
if tksc.results.Len() > (tksc.k + tksc.skip) {
|
if tksc.results.Len() > (tksc.k + tksc.skip) {
|
||||||
// remove the head
|
// remove the head
|
||||||
tksc.results.Remove(tksc.results.Front())
|
tksc.minScore = tksc.results.Remove(tksc.results.Front()).(*search.DocumentMatch).Score
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,8 +10,6 @@
|
||||||
package collectors
|
package collectors
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand"
|
|
||||||
"strconv"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
|
@ -225,27 +223,17 @@ func TestTop10ScoresSkip10(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkTop10of100000Scores(b *testing.B) {
|
func BenchmarkTop10of100000Scores(b *testing.B) {
|
||||||
|
benchHelper(10000, NewTopScorerCollector(10), b)
|
||||||
matches := make(search.DocumentMatchCollection, 0, 100000)
|
|
||||||
for i := 0; i < 100000; i++ {
|
|
||||||
matches = append(matches, &search.DocumentMatch{
|
|
||||||
ID: strconv.Itoa(i),
|
|
||||||
Score: rand.Float64(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
searcher := &stubSearcher{
|
|
||||||
matches: matches,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
collector := NewTopScorerCollector(10)
|
func BenchmarkTop100of100000Scores(b *testing.B) {
|
||||||
b.ResetTimer()
|
benchHelper(10000, NewTopScorerCollector(100), b)
|
||||||
|
}
|
||||||
|
|
||||||
err := collector.Collect(context.Background(), searcher)
|
func BenchmarkTop10of1000000Scores(b *testing.B) {
|
||||||
if err != nil {
|
benchHelper(100000, NewTopScorerCollector(10), b)
|
||||||
b.Fatal(err)
|
|
||||||
}
|
|
||||||
res := collector.Results()
|
|
||||||
for _, dm := range res {
|
|
||||||
b.Logf("%s - %f\n", dm.ID, dm.Score)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkTop100of1000000Scores(b *testing.B) {
|
||||||
|
benchHelper(100000, NewTopScorerCollector(100), b)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user