-
Notifications
You must be signed in to change notification settings - Fork 10
/
segment_test.go
230 lines (217 loc) · 6.26 KB
/
segment_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package rankdb_test
// Copyright 2019 Vivino. All rights reserved
//
// See LICENSE file for license details
import (
"fmt"
"math"
"reflect"
"testing"
"unsafe"
"github.com/Vivino/rankdb"
)
func TestSegment_Size(t *testing.T) {
var info rankdb.Segment
const infoSize = unsafe.Sizeof(info)
t.Log(infoSize)
}
func TestSegment_Filter(t *testing.T) {
lst := rankdb.Elements{}
lst = append(lst, rankdb.Element{Score: 1000, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 900, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 800, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 700, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 500, TieBreaker: 3})
lst = append(lst, rankdb.Element{Score: 500, TieBreaker: 2})
lst = append(lst, rankdb.Element{Score: 400, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 300, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 2, TieBreaker: 1})
lst = append(lst, rankdb.Element{Score: 1, TieBreaker: 1})
lst.Sort()
testSet := []struct {
name string
seg rankdb.Segment
want rankdb.Elements
}{
{
name: "range where tiebreaker is above",
seg: rankdb.Segment{Min: 300, Max: 500},
want: rankdb.Elements{{Score: 400, TieBreaker: 1}, {Score: 300, TieBreaker: 1}},
},
{
name: "range where tiebreaker is equal",
seg: rankdb.Segment{Min: 300, Max: 500, MaxTie: 2},
want: rankdb.Elements{{Score: 500, TieBreaker: 2}, {Score: 400, TieBreaker: 1}, {Score: 300, TieBreaker: 1}},
},
{
name: "range where tiebreaker is only difference",
seg: rankdb.Segment{Min: 500, Max: 500, MaxTie: 10},
want: rankdb.Elements{{Score: 500, TieBreaker: 3}, {Score: 500, TieBreaker: 2}},
},
{
name: "all",
seg: rankdb.Segment{Max: math.MaxUint64, MaxTie: math.MaxUint32},
want: lst,
},
{
name: "none",
seg: rankdb.Segment{},
want: rankdb.Elements{},
},
{
name: "first",
seg: rankdb.Segment{Min: 1000, Max: 10000},
want: rankdb.Elements{{Score: 1000, TieBreaker: 1}},
},
{
name: "last",
seg: rankdb.Segment{Min: 1, Max: 2},
want: rankdb.Elements{{Score: 1, TieBreaker: 1}},
},
}
for i, test := range testSet {
t.Run(fmt.Sprint(i, "-", test.name), func(t *testing.T) {
got := test.seg.Filter(lst)
if !reflect.DeepEqual(got, test.want) {
t.Errorf("Got %v, want %v", got, test.want)
}
})
}
}
func TestSegment_FilterScoresIdx(t *testing.T) {
testSet := []struct {
name string
seg rankdb.Segment
scores []uint64
want_start, want_stop int
}{
{
name: "range where index is in the middle",
seg: rankdb.Segment{Min: 300, Max: 500},
scores: []uint64{2000, 1000, 500, 400, 300, 100, 60, 0},
want_start: 2,
want_stop: 4,
},
{
name: "range where segment is at beginning",
seg: rankdb.Segment{Min: 300, Max: 5000},
scores: []uint64{2000, 1000, 500, 400, 300, 100, 60, 0},
want_start: 0,
want_stop: 4,
},
{
name: "range where segment is at the end",
seg: rankdb.Segment{Min: 0, Max: 100},
scores: []uint64{2000, 1000, 500, 400, 300, 100, 60, 0},
want_start: 5,
want_stop: 7,
},
{
name: "range where none is in range (too big)",
seg: rankdb.Segment{Min: 3000, Max: 5000},
scores: []uint64{2000, 1000, 500, 400, 300, 100, 60, 0},
want_start: 0,
want_stop: 0,
},
{
name: "range where none is in range (too small)",
seg: rankdb.Segment{Min: 0, Max: 40},
scores: []uint64{2000, 1000, 500, 400, 300, 100, 60, 50},
want_start: 0,
want_stop: 0,
},
}
for i, test := range testSet {
t.Run(fmt.Sprint(i, "-", test.name), func(t *testing.T) {
start, stop := test.seg.FilterScoresIdx(test.scores)
if start != test.want_start {
t.Errorf("Got start %v, want %v", start, test.want_start)
}
if stop != test.want_stop {
t.Errorf("Got stop %v, want %v", stop, test.want_stop)
}
})
}
}
func BenchmarkSegment_Filter(b *testing.B) {
for n := benchStartElements * 10; n <= benchMaxElements*10; n *= benchMulElements {
in := rankdb.NewElements(randElements(n))
quarter := n / 4
segs := rankdb.NewSegments(0, false)
s := rankdb.NewSegment(segs)
s.Min, s.MinTie = in[3*quarter].Score, in[3*quarter].TieBreaker
s.Max, s.MaxTie = in[quarter].Score, in[quarter].TieBreaker
b.Run(fmt.Sprint(n, " Elements"), func(b *testing.B) {
for i := 0; i < b.N; i++ {
seg := s.Filter(in)
if len(seg) < quarter {
b.Fatalf("too short, got %v, want at least %v", len(seg), quarter)
}
if len(seg) > n-quarter {
b.Fatalf("too long, got %v, want at least %v", len(seg), n-quarter)
}
}
})
}
}
/*
func BenchmarkSegment_SaveElements(b *testing.B) {
ctx := context.Background()
for n := benchStartElements; n <= benchMaxElements; n *= benchMulElements {
in := rankdb.NewElements(randElements(n))
segs := rankdb.NewSegments(0, false)
s := rankdb.NewSegmentElements(segs, in)
store := blobstore.StoreWithSet(memstore.NewMemStore(), "test")
pl, err := store.Get(ctx, s.seg.StorageKey())
if err != nil {
b.Fatal(err)
}
b.Log(n, "Elements. Blob size:", len(pl))
b.ResetTimer()
b.Run(fmt.Sprint(n, " Elements"), func(b *testing.B) {
b.SetBytes(int64(len(pl)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
err := s.saveElements(ctx, store, in)
if err != nil {
b.Fatal(err)
}
}
})
}
}
func BenchmarkSegment_LoadElements(b *testing.B) {
ctx := context.Background()
for n := benchStartElements; n <= benchMaxElements; n *= benchMulElements {
in := rankdb.NewElements(randElements(n))
segs := rankdb.NewSegments(0, false)
s := rankdb.NewSegmentElements(segs, in)
mem := memstore.NewMemStore()
store := blobstore.StoreWithSet(mem, "test")
err := s.saveElements(ctx, store, in)
if err != nil {
b.Fatal(err)
}
pl, err := store.Get(ctx, s.StorageKey())
if err != nil {
b.Log(mem.Dir("test"))
b.Fatal(err)
}
b.ResetTimer()
b.Log(n, "Elements. Blob size:", len(pl))
b.Run(fmt.Sprint(n, " Elements"), func(b *testing.B) {
b.SetBytes(int64(len(pl)))
b.ReportAllocs()
for i := 0; i < b.N; i++ {
e, err := s.loadElements(ctx, store)
if err != nil {
b.Fatal(err)
}
if len(e) != len(in) {
b.Fatal(len(e), "!=", len(in))
}
}
})
}
}
*/