| 1 |
package bolt |
| 2 |
|
| 3 |
import ( |
| 4 |
"math/rand" |
| 5 |
"reflect" |
| 6 |
"sort" |
| 7 |
"testing" |
| 8 |
"unsafe" |
| 9 |
) |
| 10 |
|
| 11 |
|
| 12 |
func TestFreelist_free(t *testing.T) { |
| 13 |
f := newFreelist() |
| 14 |
f.free(100, &page{id: 12}) |
| 15 |
if !reflect.DeepEqual([]pgid{12}, f.pending[100]) { |
| 16 |
t.Fatalf("exp=%v; got=%v", []pgid{12}, f.pending[100]) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
|
| 21 |
func TestFreelist_free_overflow(t *testing.T) { |
| 22 |
f := newFreelist() |
| 23 |
f.free(100, &page{id: 12, overflow: 3}) |
| 24 |
if exp := []pgid{12, 13, 14, 15}; !reflect.DeepEqual(exp, f.pending[100]) { |
| 25 |
t.Fatalf("exp=%v; got=%v", exp, f.pending[100]) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func TestFreelist_release(t *testing.T) { |
| 31 |
f := newFreelist() |
| 32 |
f.free(100, &page{id: 12, overflow: 1}) |
| 33 |
f.free(100, &page{id: 9}) |
| 34 |
f.free(102, &page{id: 39}) |
| 35 |
f.release(100) |
| 36 |
f.release(101) |
| 37 |
if exp := []pgid{9, 12, 13}; !reflect.DeepEqual(exp, f.ids) { |
| 38 |
t.Fatalf("exp=%v; got=%v", exp, f.ids) |
| 39 |
} |
| 40 |
|
| 41 |
f.release(102) |
| 42 |
if exp := []pgid{9, 12, 13, 39}; !reflect.DeepEqual(exp, f.ids) { |
| 43 |
t.Fatalf("exp=%v; got=%v", exp, f.ids) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
func TestFreelist_allocate(t *testing.T) { |
| 49 |
f := &freelist{ids: []pgid{3, 4, 5, 6, 7, 9, 12, 13, 18}} |
| 50 |
if id := int(f.allocate(3)); id != 3 { |
| 51 |
t.Fatalf("exp=3; got=%v", id) |
| 52 |
} |
| 53 |
if id := int(f.allocate(1)); id != 6 { |
| 54 |
t.Fatalf("exp=6; got=%v", id) |
| 55 |
} |
| 56 |
if id := int(f.allocate(3)); id != 0 { |
| 57 |
t.Fatalf("exp=0; got=%v", id) |
| 58 |
} |
| 59 |
if id := int(f.allocate(2)); id != 12 { |
| 60 |
t.Fatalf("exp=12; got=%v", id) |
| 61 |
} |
| 62 |
if id := int(f.allocate(1)); id != 7 { |
| 63 |
t.Fatalf("exp=7; got=%v", id) |
| 64 |
} |
| 65 |
if id := int(f.allocate(0)); id != 0 { |
| 66 |
t.Fatalf("exp=0; got=%v", id) |
| 67 |
} |
| 68 |
if id := int(f.allocate(0)); id != 0 { |
| 69 |
t.Fatalf("exp=0; got=%v", id) |
| 70 |
} |
| 71 |
if exp := []pgid{9, 18}; !reflect.DeepEqual(exp, f.ids) { |
| 72 |
t.Fatalf("exp=%v; got=%v", exp, f.ids) |
| 73 |
} |
| 74 |
|
| 75 |
if id := int(f.allocate(1)); id != 9 { |
| 76 |
t.Fatalf("exp=9; got=%v", id) |
| 77 |
} |
| 78 |
if id := int(f.allocate(1)); id != 18 { |
| 79 |
t.Fatalf("exp=18; got=%v", id) |
| 80 |
} |
| 81 |
if id := int(f.allocate(1)); id != 0 { |
| 82 |
t.Fatalf("exp=0; got=%v", id) |
| 83 |
} |
| 84 |
if exp := []pgid{}; !reflect.DeepEqual(exp, f.ids) { |
| 85 |
t.Fatalf("exp=%v; got=%v", exp, f.ids) |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
func TestFreelist_read(t *testing.T) { |
| 91 |
|
| 92 |
var buf [4096]byte |
| 93 |
page := (*page)(unsafe.Pointer(&buf[0])) |
| 94 |
page.flags = freelistPageFlag |
| 95 |
page.count = 2 |
| 96 |
|
| 97 |
|
| 98 |
ids := (*[3]pgid)(unsafe.Pointer(&page.ptr)) |
| 99 |
ids[0] = 23 |
| 100 |
ids[1] = 50 |
| 101 |
|
| 102 |
|
| 103 |
f := newFreelist() |
| 104 |
f.read(page) |
| 105 |
|
| 106 |
|
| 107 |
if exp := []pgid{23, 50}; !reflect.DeepEqual(exp, f.ids) { |
| 108 |
t.Fatalf("exp=%v; got=%v", exp, f.ids) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
func TestFreelist_write(t *testing.T) { |
| 114 |
|
| 115 |
var buf [4096]byte |
| 116 |
f := &freelist{ids: []pgid{12, 39}, pending: make(map[txid][]pgid)} |
| 117 |
f.pending[100] = []pgid{28, 11} |
| 118 |
f.pending[101] = []pgid{3} |
| 119 |
p := (*page)(unsafe.Pointer(&buf[0])) |
| 120 |
if err := f.write(p); err != nil { |
| 121 |
t.Fatal(err) |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
f2 := newFreelist() |
| 126 |
f2.read(p) |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
if exp := []pgid{3, 11, 12, 28, 39}; !reflect.DeepEqual(exp, f2.ids) { |
| 131 |
t.Fatalf("exp=%v; got=%v", exp, f2.ids) |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
func Benchmark_FreelistRelease10K(b *testing.B) { benchmark_FreelistRelease(b, 10000) } |
| 136 |
func Benchmark_FreelistRelease100K(b *testing.B) { benchmark_FreelistRelease(b, 100000) } |
| 137 |
func Benchmark_FreelistRelease1000K(b *testing.B) { benchmark_FreelistRelease(b, 1000000) } |
| 138 |
func Benchmark_FreelistRelease10000K(b *testing.B) { benchmark_FreelistRelease(b, 10000000) } |
| 139 |
|
| 140 |
func benchmark_FreelistRelease(b *testing.B, size int) { |
| 141 |
ids := randomPgids(size) |
| 142 |
pending := randomPgids(len(ids) / 400) |
| 143 |
b.ResetTimer() |
| 144 |
for i := 0; i < b.N; i++ { |
| 145 |
f := &freelist{ids: ids, pending: map[txid][]pgid{1: pending}} |
| 146 |
f.release(1) |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
func randomPgids(n int) []pgid { |
| 151 |
rand.Seed(42) |
| 152 |
pgids := make(pgids, n) |
| 153 |
for i := range pgids { |
| 154 |
pgids[i] = pgid(rand.Int63()) |
| 155 |
} |
| 156 |
sort.Sort(pgids) |
| 157 |
return pgids |
| 158 |
} |