-
Notifications
You must be signed in to change notification settings - Fork 87
/
hititer_test.go
111 lines (96 loc) · 2.63 KB
/
hititer_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2019 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package zoekt
import (
"fmt"
"math/rand"
"reflect"
"sort"
"testing"
"testing/quick"
"github.com/google/go-cmp/cmp"
)
func TestCompressedPostingIterator_limit(t *testing.T) {
f := func(nums, limits []uint32) bool {
if len(nums) == 0 || len(limits) == 0 {
return true
}
nums = sortedUnique(nums)
sort.Slice(limits, func(i, j int) bool { return limits[i] < limits[j] })
want := doHitIterator(&inMemoryIterator{postings: nums}, limits)
it := newCompressedPostingIterator(toDeltas(nums), stringToNGram("abc"))
got := doHitIterator(it, limits)
if !reflect.DeepEqual(want, got) {
t.Log(cmp.Diff(want, got))
return false
}
return true
}
if err := quick.Check(f, nil); err != nil {
t.Error(err)
}
}
func doHitIterator(it hitIterator, limits []uint32) []uint32 {
var nums []uint32
for _, limit := range limits {
it.next(limit)
nums = append(nums, it.first())
}
return nums
}
func BenchmarkCompressedPostingIterator(b *testing.B) {
cases := []struct{ size, limitSize int }{
{100, 50},
{10000, 100},
{10000, 1000},
{10000, 10000},
{100000, 100},
{100000, 1000},
{100000, 10000},
{100000, 100000},
}
for _, tt := range cases {
b.Run(fmt.Sprintf("%d_%d", tt.size, tt.limitSize), func(b *testing.B) {
benchmarkCompressedPostingIterator(b, tt.size, tt.limitSize)
})
}
}
func benchmarkCompressedPostingIterator(b *testing.B, size, limitsSize int) {
nums := genUints32(size)
limits := genUints32(limitsSize)
nums = sortedUnique(nums)
sort.Slice(limits, func(i, j int) bool { return limits[i] < limits[j] })
ng := stringToNGram("abc")
deltas := toDeltas(nums)
b.ResetTimer()
for n := 0; n < b.N; n++ {
it := newCompressedPostingIterator(deltas, ng)
for _, limit := range limits {
it.next(limit)
_ = it.first()
}
var s Stats
it.updateStats(&s)
b.SetBytes(s.IndexBytesLoaded)
}
}
func genUints32(size int) []uint32 {
// Deterministic for benchmarks
r := rand.New(rand.NewSource(int64(size)))
nums := make([]uint32, size)
for i := range nums {
nums[i] = r.Uint32()
}
return nums
}