github.com/scottcagno/storage@v1.8.0/pkg/_junk/_index/_trees_test.go (about) 1 package index 2 3 import ( 4 "bytes" 5 "github.com/scottcagno/storage/pkg/_junk/_index/bptree" 6 "github.com/scottcagno/storage/pkg/_junk/_index/rbtree" 7 "math/rand" 8 "strconv" 9 "testing" 10 ) 11 12 const NumItems = 1000000 13 14 func RandomByteKey(length int) []byte { 15 key := make([]byte, length) 16 rand.Read(key) 17 return key 18 } 19 20 func RandomStringKey(length int) string { 21 return string(RandomByteKey(length)) 22 } 23 24 func BenchmarkBPlusTree0_Set(b *testing.B) { 25 k := make([][]byte, 0) 26 for i := 0; i < NumItems; i++ { 27 key := strconv.Itoa(rand.Intn(NumItems)) 28 k = append(k, []byte(key)) 29 } 30 i, l := 0, len(k) 31 32 b.ResetTimer() 33 b.ReportAllocs() 34 tr := bptree.NewBPTree() 35 for n := 0; n < b.N; n++ { 36 tr.Set(k[i], k[i]) 37 i++ 38 if i >= l { 39 i = 0 40 } 41 } 42 tr.Close() 43 } 44 45 func BenchmarkBPlusTree1_Set(b *testing.B) { 46 keys := make([][]byte, b.N) 47 for i := 0; i < b.N; i++ { 48 keys[i] = RandomByteKey(4) 49 } 50 b.ResetTimer() 51 b.ReportAllocs() 52 tr := bptree.NewBPTree() 53 for i := 0; i < b.N; i++ { 54 tr.Set(keys[i], keys[i]) 55 } 56 tr.Close() 57 } 58 59 func BenchmarkBPlusTree2_Set(b *testing.B) { 60 keys := make([][]byte, b.N) 61 for i := 0; i < b.N; i++ { 62 keys[i] = RandomByteKey(4) 63 } 64 b.ResetTimer() 65 b.ReportAllocs() 66 tr := bptree.NewBPTree() 67 for i := 0; i < b.N; i++ { 68 tr.Set(keys[i], keys[i]) 69 } 70 tr.Close() 71 } 72 73 type Arr []byte 74 75 func (this Arr) Compare(that _rbtree.Key) int { 76 return bytes.Compare(this, that.(Arr)) 77 } 78 79 func BenchmarkRBTree1_Set(b *testing.B) { 80 keys := make([]Arr, b.N) 81 for i := 0; i < b.N; i++ { 82 keys[i] = RandomByteKey(4) 83 } 84 b.ResetTimer() 85 b.ReportAllocs() 86 tr := rbtree.NewRBTree() 87 for i := 0; i < b.N; i++ { 88 tr.Set(keys[i], keys[i]) 89 } 90 tr.Close() 91 } 92 93 func BenchmarkRBTree2_Set(b *testing.B) { 94 keys := make([][]byte, b.N) 95 for i := 0; i < b.N; i++ { 96 keys[i] = RandomByteKey(4) 97 } 98 b.ResetTimer() 99 b.ReportAllocs() 100 tr := rbtree.NewRBTree() 101 for i := 0; i < b.N; i++ { 102 tr.Set(keys[i], keys[i]) 103 } 104 tr.Close() 105 } 106 107 func BenchmarkMap_Set(b *testing.B) { 108 keys := make([]string, b.N) 109 for i := 0; i < b.N; i++ { 110 keys[i] = RandomStringKey(4) 111 } 112 b.ResetTimer() 113 b.ReportAllocs() 114 tr := make(map[string]interface{}) 115 for i := 0; i < b.N; i++ { 116 tr[keys[i]] = keys[i] 117 } 118 tr = nil 119 }