forked from etcd-io/bbolt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap_test.go
179 lines (153 loc) · 4.37 KB
/
hashmap_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
package freelist
import (
"math/rand"
"reflect"
"sort"
"testing"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt/internal/common"
)
func TestFreelistHashmap_allocate(t *testing.T) {
f := NewHashMapFreelist()
ids := []common.Pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}
f.Init(ids)
f.Allocate(1, 3)
if x := f.FreeCount(); x != 6 {
t.Fatalf("exp=6; got=%v", x)
}
f.Allocate(1, 2)
if x := f.FreeCount(); x != 4 {
t.Fatalf("exp=4; got=%v", x)
}
f.Allocate(1, 1)
if x := f.FreeCount(); x != 3 {
t.Fatalf("exp=3; got=%v", x)
}
f.Allocate(1, 0)
if x := f.FreeCount(); x != 3 {
t.Fatalf("exp=3; got=%v", x)
}
}
func TestFreelistHashmap_mergeWithExist(t *testing.T) {
bm1 := pidSet{1: struct{}{}}
bm2 := pidSet{5: struct{}{}}
tests := []struct {
name string
ids common.Pgids
pgid common.Pgid
want common.Pgids
wantForwardmap map[common.Pgid]uint64
wantBackwardmap map[common.Pgid]uint64
wantfreemap map[uint64]pidSet
}{
{
name: "test1",
ids: []common.Pgid{1, 2, 4, 5, 6},
pgid: 3,
want: []common.Pgid{1, 2, 3, 4, 5, 6},
wantForwardmap: map[common.Pgid]uint64{1: 6},
wantBackwardmap: map[common.Pgid]uint64{6: 6},
wantfreemap: map[uint64]pidSet{6: bm1},
},
{
name: "test2",
ids: []common.Pgid{1, 2, 5, 6},
pgid: 3,
want: []common.Pgid{1, 2, 3, 5, 6},
wantForwardmap: map[common.Pgid]uint64{1: 3, 5: 2},
wantBackwardmap: map[common.Pgid]uint64{6: 2, 3: 3},
wantfreemap: map[uint64]pidSet{3: bm1, 2: bm2},
},
{
name: "test3",
ids: []common.Pgid{1, 2},
pgid: 3,
want: []common.Pgid{1, 2, 3},
wantForwardmap: map[common.Pgid]uint64{1: 3},
wantBackwardmap: map[common.Pgid]uint64{3: 3},
wantfreemap: map[uint64]pidSet{3: bm1},
},
{
name: "test4",
ids: []common.Pgid{2, 3},
pgid: 1,
want: []common.Pgid{1, 2, 3},
wantForwardmap: map[common.Pgid]uint64{1: 3},
wantBackwardmap: map[common.Pgid]uint64{3: 3},
wantfreemap: map[uint64]pidSet{3: bm1},
},
}
for _, tt := range tests {
f := newTestHashMapFreelist()
f.Init(tt.ids)
f.mergeWithExistingSpan(tt.pgid)
if got := f.freePageIds(); !reflect.DeepEqual(tt.want, got) {
t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.want, got)
}
if got := f.forwardMap; !reflect.DeepEqual(tt.wantForwardmap, got) {
t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.wantForwardmap, got)
}
if got := f.backwardMap; !reflect.DeepEqual(tt.wantBackwardmap, got) {
t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.wantBackwardmap, got)
}
if got := f.freemaps; !reflect.DeepEqual(tt.wantfreemap, got) {
t.Fatalf("name %s; exp=%v; got=%v", tt.name, tt.wantfreemap, got)
}
}
}
func TestFreelistHashmap_GetFreePageIDs(t *testing.T) {
f := newTestHashMapFreelist()
N := int32(100000)
fm := make(map[common.Pgid]uint64)
i := int32(0)
val := int32(0)
for i = 0; i < N; {
val = rand.Int31n(1000)
fm[common.Pgid(i)] = uint64(val)
i += val
f.freePagesCount += uint64(val)
}
f.forwardMap = fm
res := f.freePageIds()
if !sort.SliceIsSorted(res, func(i, j int) bool { return res[i] < res[j] }) {
t.Fatalf("pgids not sorted")
}
}
func Test_Freelist_Hashmap_Rollback(t *testing.T) {
f := newTestHashMapFreelist()
f.Init([]common.Pgid{3, 5, 6, 7, 12, 13})
f.Free(100, common.NewPage(20, 0, 0, 1))
f.Allocate(100, 3)
f.Free(100, common.NewPage(25, 0, 0, 0))
f.Allocate(100, 2)
require.Equal(t, map[common.Pgid]common.Txid{5: 100, 12: 100}, f.allocs)
require.Equal(t, map[common.Txid]*txPending{100: {
ids: []common.Pgid{20, 21, 25},
alloctx: []common.Txid{0, 0, 0},
}}, f.pending)
f.Rollback(100)
require.Equal(t, map[common.Pgid]common.Txid{}, f.allocs)
require.Equal(t, map[common.Txid]*txPending{}, f.pending)
}
func Benchmark_freelist_hashmapGetFreePageIDs(b *testing.B) {
f := newTestHashMapFreelist()
N := int32(100000)
fm := make(map[common.Pgid]uint64)
i := int32(0)
val := int32(0)
for i = 0; i < N; {
val = rand.Int31n(1000)
fm[common.Pgid(i)] = uint64(val)
i += val
}
f.forwardMap = fm
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
f.freePageIds()
}
}
func newTestHashMapFreelist() *hashMap {
f := NewHashMapFreelist()
return f.(*hashMap)
}