From 222770b1cce31d61d89008efcef343d470fd9ad9 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Tue, 23 Dec 2014 15:00:04 +0100 Subject: [PATCH 1/2] Add a benchmark for the numeric facet builder --- search/facets/facet_builder_numeric_test.go | 49 +++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 search/facets/facet_builder_numeric_test.go diff --git a/search/facets/facet_builder_numeric_test.go b/search/facets/facet_builder_numeric_test.go new file mode 100644 index 00000000..6831438b --- /dev/null +++ b/search/facets/facet_builder_numeric_test.go @@ -0,0 +1,49 @@ +package facets + +import ( + "strconv" + "testing" + + "github.com/blevesearch/bleve/index" + nu "github.com/blevesearch/bleve/numeric_util" +) + +var pcodedvalues []nu.PrefixCoded + +func init() { + pcodedvalues = []nu.PrefixCoded{nu.PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x1}, nu.PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f}, nu.PrefixCoded{0x20, 0x0, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7f, 0x7a, 0x1d, 0xa}, nu.PrefixCoded{0x20, 0x1, 0x0, 0x0, 0x0, 0x0, 0x1, 0x16, 0x9, 0x4a, 0x7b}} +} + +func BenchmarkNumericFacet10(b *testing.B) { + numericFacetN(b, 10) +} + +func BenchmarkNumericFacet100(b *testing.B) { + numericFacetN(b, 100) +} + +func BenchmarkNumericFacet1000(b *testing.B) { + numericFacetN(b, 1000) +} + +func numericFacetN(b *testing.B, numTerms int) { + field := "test" + nfb := NewNumericFacetBuilder(field, numTerms) + min, max := 0.0, 9999999998.0 + + for i := 0; i <= numTerms; i++ { + max++ + min-- + + nfb.AddRange("rangename"+strconv.Itoa(i), &min, &max) + + for _, pv := range pcodedvalues { + nfb.Update(index.FieldTerms{field: []string{string(pv)}}) + } + } + + b.ResetTimer() + for i := 0; i < b.N; i++ { + nfb.Result() + } +} From bc1117e289bd1f7940dde16d7a21eb00a97306d8 Mon Sep 17 00:00:00 2001 From: Silvan Jegen Date: Mon, 22 Dec 2014 17:05:48 +0100 Subject: [PATCH 2/2] Use sort.Sort for the numeric facet builder --- search/facets/facet_builder_numeric.go | 42 +++++++------------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/search/facets/facet_builder_numeric.go b/search/facets/facet_builder_numeric.go index 2f22b5e2..f5acfb0f 100644 --- a/search/facets/facet_builder_numeric.go +++ b/search/facets/facet_builder_numeric.go @@ -10,7 +10,7 @@ package facets import ( - "container/list" + "sort" "github.com/blevesearch/bleve/index" "github.com/blevesearch/bleve/numeric_util" @@ -89,11 +89,8 @@ func (fb *NumericFacetBuilder) Result() *search.FacetResult { Missing: fb.missing, } - // FIXME better implementation needed here this is quick and dirty - topN := list.New() + rv.NumericRanges = make([]*search.NumericRangeFacet, 0, len(fb.termsCount)) - // walk entries and find top N -OUTER: for term, count := range fb.termsCount { numericRange := fb.ranges[term] tf := &search.NumericRangeFacet{ @@ -103,36 +100,19 @@ OUTER: Max: numericRange.max, } - for e := topN.Front(); e != nil; e = e.Next() { - curr := e.Value.(*search.NumericRangeFacet) - if tf.Count < curr.Count { - - topN.InsertBefore(tf, e) - // if we just made the list too long - if topN.Len() > fb.size { - // remove the head - topN.Remove(topN.Front()) - } - continue OUTER - } - } - // if we got to the end, we still have to add it - topN.PushBack(tf) - if topN.Len() > fb.size { - // remove the head - topN.Remove(topN.Front()) - } - + rv.NumericRanges = append(rv.NumericRanges, tf) } + sort.Sort(rv.NumericRanges) + // we now have the list of the top N facets - rv.NumericRanges = make([]*search.NumericRangeFacet, topN.Len()) - i := 0 + if fb.size < len(rv.NumericRanges) { + rv.NumericRanges = rv.NumericRanges[:fb.size] + } + notOther := 0 - for e := topN.Back(); e != nil; e = e.Prev() { - rv.NumericRanges[i] = e.Value.(*search.NumericRangeFacet) - i++ - notOther += e.Value.(*search.NumericRangeFacet).Count + for _, nr := range rv.NumericRanges { + notOther += nr.Count } rv.Other = fb.total - notOther