github.com/tidwall/pair@v0.0.0-20170418221434-1140497fc57a/pair_test.go (about) 1 package pair 2 3 import ( 4 "math/rand" 5 "runtime" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func expectedSizeForPair(pair Pair) int { 14 key := pair.Key() 15 value := pair.Value() 16 sz := 8 + len(key) + len(value) 17 return sz 18 } 19 20 func TestBasic(t *testing.T) { 21 mallocgc(10, 0, false) 22 item := New([]byte("hello"), []byte("world")) 23 assert.Equal(t, "hello", string(item.Key())) 24 assert.Equal(t, "world", string(item.Value())) 25 assert.Equal(t, expectedSizeForPair(item), item.Size()) 26 item = New([]byte(""), []byte("world")) 27 assert.Equal(t, "", string(item.Key())) 28 assert.Equal(t, "world", string(item.Value())) 29 assert.Equal(t, expectedSizeForPair(item), item.Size()) 30 item = New([]byte(""), nil) 31 assert.Equal(t, "", string(item.Key())) 32 assert.Equal(t, "", string(item.Value())) 33 assert.Equal(t, expectedSizeForPair(item), item.Size()) 34 assert.Equal(t, false, item.Zero()) 35 assert.Equal(t, true, (Pair{}).Zero()) 36 assert.Equal(t, item.ptr, item.Pointer()) 37 } 38 39 func TestRandom(t *testing.T) { 40 rand.Seed(time.Now().UnixNano()) 41 var items []Pair 42 var keys []string 43 var values []string 44 var sizes []int 45 for i := 0; i < 100000; i++ { 46 key := make([]byte, rand.Int()%300) 47 rand.Read(key) 48 keys = append(keys, string(key)) 49 value := make([]byte, rand.Int()%300) 50 rand.Read(value) 51 values = append(values, string(value)) 52 item := New([]byte(keys[i]), []byte(values[i])) 53 items = append(items, item) 54 sizes = append(sizes, item.Size()) 55 ikey := item.Key() 56 ivalue := item.Value() 57 assert.Equal(t, string(key), string(ikey)) 58 assert.Equal(t, string(value), string(ivalue)) 59 assert.Equal(t, expectedSizeForPair(item), item.Size()) 60 } 61 // check for memory span errors 62 var ikeys [][]byte 63 var ivalues [][]byte 64 runtime.GC() 65 for i := 0; i < len(items); i++ { 66 item := items[i] 67 key := keys[i] 68 value := values[i] 69 ikey := item.Key() 70 ikeys = append(ikeys, ikey) 71 ivalue := item.Value() 72 ivalues = append(ivalues, ivalue) 73 assert.Equal(t, string(key), string(ikey)) 74 assert.Equal(t, string(value), string(ivalue)) 75 assert.Equal(t, expectedSizeForPair(item), item.Size()) 76 } 77 items = nil 78 // check for memory span errors 79 runtime.GC() 80 for i := 0; i < len(items); i++ { 81 key := keys[i] 82 value := values[i] 83 ikey := ikeys[i] 84 ivalue := ivalues[i] 85 assert.Equal(t, string(key), string(ikey)) 86 assert.Equal(t, string(value), string(ivalue)) 87 assert.Equal(t, key, ikey) 88 assert.Equal(t, value, ivalue) 89 } 90 // larger items 91 for i := 0; i < 10000; i++ { 92 key := make([]byte, rand.Int()%300+(0xFFFF-150)) 93 value := make([]byte, rand.Int()%300+(0xFFFF-150)) 94 rand.Read(key) 95 rand.Read(value) 96 item := New(key, value) 97 ikey := item.Key() 98 ivalue := item.Value() 99 assert.Equal(t, string(key), string(ikey)) 100 assert.Equal(t, string(value), string(ivalue)) 101 assert.Equal(t, expectedSizeForPair(item), item.Size()) 102 } 103 } 104 105 func BenchmarkNew2(t *testing.B) { 106 s := []byte(strings.Repeat("*", 1)) 107 t.ReportAllocs() 108 t.ResetTimer() 109 for i := 0; i < t.N; i++ { 110 New(s, s) 111 } 112 } 113 func BenchmarkNew6(t *testing.B) { 114 s := []byte(strings.Repeat("*", 3)) 115 t.ReportAllocs() 116 t.ResetTimer() 117 for i := 0; i < t.N; i++ { 118 New(s, s) 119 } 120 } 121 func BenchmarkNew14(t *testing.B) { 122 s := []byte(strings.Repeat("*", 7)) 123 t.ReportAllocs() 124 t.ResetTimer() 125 for i := 0; i < t.N; i++ { 126 New(s, s) 127 } 128 } 129 130 func BenchmarkNew62(t *testing.B) { 131 s := []byte(strings.Repeat("*", 31)) 132 t.ReportAllocs() 133 t.ResetTimer() 134 for i := 0; i < t.N; i++ { 135 New(s, s) 136 } 137 } 138 139 func BenchmarkNew126(t *testing.B) { 140 s := []byte(strings.Repeat("*", 63)) 141 t.ReportAllocs() 142 t.ResetTimer() 143 for i := 0; i < t.N; i++ { 144 New(s, s) 145 } 146 } 147 func BenchmarkNew256(t *testing.B) { 148 s := []byte(strings.Repeat("*", 128)) 149 t.ReportAllocs() 150 t.ResetTimer() 151 for i := 0; i < t.N; i++ { 152 New(s, s) 153 } 154 } 155 func BenchmarkNew1024(t *testing.B) { 156 s := []byte(strings.Repeat("*", 512)) 157 t.ReportAllocs() 158 t.ResetTimer() 159 for i := 0; i < t.N; i++ { 160 New(s, s) 161 } 162 } 163 func BenchmarkNew0xFFFF(t *testing.B) { 164 s := []byte(strings.Repeat("*", 0xFFFF/2)) 165 t.ReportAllocs() 166 t.ReportAllocs() 167 t.ResetTimer() 168 for i := 0; i < t.N; i++ { 169 New(s, s) 170 } 171 } 172 173 func BenchmarkGet62(t *testing.B) { 174 s := []byte(strings.Repeat("*", 31)) 175 var pairs []Pair 176 for i := 0; i < t.N; i++ { 177 pairs = append(pairs, New(s, s)) 178 } 179 t.ReportAllocs() 180 t.ResetTimer() 181 for i := 0; i < t.N; i++ { 182 p := pairs[i] 183 p.Key() 184 p.Value() 185 } 186 } 187 188 func BenchmarkGet1024(t *testing.B) { 189 s := []byte(strings.Repeat("*", 512)) 190 var pairs []Pair 191 for i := 0; i < t.N; i++ { 192 pairs = append(pairs, New(s, s)) 193 } 194 t.ReportAllocs() 195 t.ResetTimer() 196 for i := 0; i < t.N; i++ { 197 p := pairs[i] 198 p.Key() 199 p.Value() 200 } 201 }