-
Notifications
You must be signed in to change notification settings - Fork 0
/
reductor_test.go
162 lines (140 loc) · 3.24 KB
/
reductor_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
//
// Tests for delta compressed postings lists
// Author: Abhinav Dangeti
//
package reductor
import (
"fmt"
"reflect"
"testing"
"time"
)
func checkEq(a, b []uint64) bool {
if a == nil && b == nil {
return true
}
if a == nil || b == nil {
return false
}
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
func test(t *testing.T, postings []uint64, sorted bool) {
dcp := NewDeltaCompPostings()
start := time.Now()
if sorted {
_ = dcp.EncodeSorted(postings)
} else {
_ = dcp.Encode(postings)
}
encodeTime := time.Since(start)
start = time.Now()
got := dcp.Decode()
decodeTime := time.Since(start)
// account for overhead from slice as well
postingsFootprint := len(postings)*8 + int(reflect.TypeOf(postings).Size())
fmt.Println("======================== RESULTS ==========================")
fmt.Println("Encoding time: ", encodeTime)
fmt.Printf("Achieved a compression from %v bytes to %v bytes => %.4v%%\n",
postingsFootprint, dcp.SizeInBytes(),
float64(postingsFootprint-dcp.SizeInBytes())*100/float64(postingsFootprint))
fmt.Println("Decoding time: ", decodeTime)
fmt.Println("===========================================================")
if !checkEq(postings, got) {
t.Errorf("Expected: %v, Got: %v", postings, got)
}
}
func TestSorted1(t *testing.T) {
postings := []uint64{
100, 102, 104, 108, 110,
}
test(t, postings, true)
}
func TestSorted2(t *testing.T) {
postings := []uint64{
101, 105, 215, 218, 240,
260, 280, 290, 320, 325,
375, 480, 578, 690, 755,
}
test(t, postings, true)
}
func TestSorted3(t *testing.T) {
postings := []uint64{
100, 102, 104, 108, 110,
120, 140, 200, 500, 622,
1402, 1550, 2000, 2529,
}
test(t, postings, true)
}
func TestSorted4(t *testing.T) {
postings := []uint64{
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,
}
test(t, postings, true)
}
func TestSorted5(t *testing.T) {
postings := []uint64{
34, 556, 600, 1234, 1270,
1400, 1592, 1946, 2000, 2239,
2500, 2501, 2503, 3991, 4728,
4780, 5290, 6992, 7000, 8262,
9618, 9762, 9872, 10021, 10245,
13892, 15001, 15002, 18269, 28651,
29590, 39200, 59109, 82693, 100351,
}
test(t, postings, true)
}
func TestUnsorted1(t *testing.T) {
postings := []uint64{
102, 100, 110, 108, 104,
}
test(t, postings, false)
}
func TestUnsorted2(t *testing.T) {
postings := []uint64{
101, 105, 215, 218, 240,
260, 280, 290, 320, 325,
375, 480, 578, 690, 755,
}
test(t, postings, false)
}
func TestUnsorted3(t *testing.T) {
postings := []uint64{
280, 105, 215, 690, 240,
578, 101, 320, 755, 325,
375, 480, 260, 218, 290,
}
test(t, postings, false)
}
func TestUnsorted4(t *testing.T) {
postings := []uint64{
2000, 1010, 700, 120, 110,
300, 100, 250, 500, 622,
1402, 550, 2600, 1300,
}
test(t, postings, false)
}
func TestUnsorted5(t *testing.T) {
postings := []uint64{
1400, 1592, 1946, 2000, 2239,
34, 556, 600, 1234, 1270,
4780, 5290, 6992, 7000, 8262,
29590, 39200, 59109, 82693, 100351,
2500, 2501, 2503, 3991, 4728,
13892, 15001, 15002, 18269, 28651,
9618, 9762, 9872, 10021, 10245,
}
test(t, postings, false)
}