github.com/XiaoMi/Gaea@v1.2.5/util/bucketpool/bucketpool_test.go (about) 1 package bucketpool 2 3 import ( 4 "math/rand" 5 "testing" 6 ) 7 8 func TestPool(t *testing.T) { 9 maxSize := 16384 10 pool := New(1024, maxSize) 11 if pool.maxSize != maxSize { 12 t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) 13 } 14 if len(pool.pools) != 5 { 15 t.Fatalf("Invalid number of pools: %d, expected %d", len(pool.pools), 5) 16 } 17 18 buf := pool.Get(64) 19 if len(*buf) != 64 { 20 t.Fatalf("unexpected buf length: %d", len(*buf)) 21 } 22 if cap(*buf) != 1024 { 23 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 24 } 25 26 // get from same pool, check that length is right 27 buf = pool.Get(128) 28 if len(*buf) != 128 { 29 t.Fatalf("unexpected buf length: %d", len(*buf)) 30 } 31 if cap(*buf) != 1024 { 32 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 33 } 34 pool.Put(buf) 35 36 // get boundary size 37 buf = pool.Get(1024) 38 if len(*buf) != 1024 { 39 t.Fatalf("unexpected buf length: %d", len(*buf)) 40 } 41 if cap(*buf) != 1024 { 42 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 43 } 44 pool.Put(buf) 45 46 // get from the middle 47 buf = pool.Get(5000) 48 if len(*buf) != 5000 { 49 t.Fatalf("unexpected buf length: %d", len(*buf)) 50 } 51 if cap(*buf) != 8192 { 52 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 53 } 54 pool.Put(buf) 55 56 // check last pool 57 buf = pool.Get(16383) 58 if len(*buf) != 16383 { 59 t.Fatalf("unexpected buf length: %d", len(*buf)) 60 } 61 if cap(*buf) != 16384 { 62 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 63 } 64 pool.Put(buf) 65 66 // get big buffer 67 buf = pool.Get(16385) 68 if len(*buf) != 16385 { 69 t.Fatalf("unexpected buf length: %d", len(*buf)) 70 } 71 if cap(*buf) != 16385 { 72 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 73 } 74 pool.Put(buf) 75 } 76 77 func TestPoolOneSize(t *testing.T) { 78 maxSize := 1024 79 pool := New(1024, maxSize) 80 if pool.maxSize != maxSize { 81 t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) 82 } 83 buf := pool.Get(64) 84 if len(*buf) != 64 { 85 t.Fatalf("unexpected buf length: %d", len(*buf)) 86 } 87 if cap(*buf) != 1024 { 88 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 89 } 90 pool.Put(buf) 91 92 buf = pool.Get(1025) 93 if len(*buf) != 1025 { 94 t.Fatalf("unexpected buf length: %d", len(*buf)) 95 } 96 if cap(*buf) != 1025 { 97 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 98 } 99 pool.Put(buf) 100 } 101 102 func TestPoolTwoSizeNotMultiplier(t *testing.T) { 103 maxSize := 2000 104 pool := New(1024, maxSize) 105 if pool.maxSize != maxSize { 106 t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) 107 } 108 buf := pool.Get(64) 109 if len(*buf) != 64 { 110 t.Fatalf("unexpected buf length: %d", len(*buf)) 111 } 112 if cap(*buf) != 1024 { 113 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 114 } 115 pool.Put(buf) 116 117 buf = pool.Get(2001) 118 if len(*buf) != 2001 { 119 t.Fatalf("unexpected buf length: %d", len(*buf)) 120 } 121 if cap(*buf) != 2001 { 122 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 123 } 124 pool.Put(buf) 125 } 126 127 func TestPoolWeirdMaxSize(t *testing.T) { 128 maxSize := 15000 129 pool := New(1024, maxSize) 130 if pool.maxSize != maxSize { 131 t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize) 132 } 133 134 buf := pool.Get(14000) 135 if len(*buf) != 14000 { 136 t.Fatalf("unexpected buf length: %d", len(*buf)) 137 } 138 if cap(*buf) != 15000 { 139 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 140 } 141 pool.Put(buf) 142 143 buf = pool.Get(16383) 144 if len(*buf) != 16383 { 145 t.Fatalf("unexpected buf length: %d", len(*buf)) 146 } 147 if cap(*buf) != 16383 { 148 t.Fatalf("unexepected buf cap: %d", cap(*buf)) 149 } 150 pool.Put(buf) 151 } 152 153 func TestFuzz(t *testing.T) { 154 maxTestSize := 16384 155 for i := 0; i < 20000; i++ { 156 minSize := rand.Intn(maxTestSize) 157 maxSize := rand.Intn(maxTestSize-minSize) + minSize 158 p := New(minSize, maxSize) 159 bufSize := rand.Intn(maxTestSize) 160 buf := p.Get(bufSize) 161 if len(*buf) != bufSize { 162 t.Fatalf("Invalid length %d, expected %d", len(*buf), bufSize) 163 } 164 sPool := p.findPool(bufSize) 165 if sPool == nil { 166 if cap(*buf) != len(*buf) { 167 t.Fatalf("Invalid cap %d, expected %d", cap(*buf), len(*buf)) 168 } 169 } else { 170 if cap(*buf) != sPool.size { 171 t.Fatalf("Invalid cap %d, expected %d", cap(*buf), sPool.size) 172 } 173 } 174 p.Put(buf) 175 } 176 } 177 178 func BenchmarkPool(b *testing.B) { 179 pool := New(2, 16384) 180 b.SetParallelism(16) 181 b.ResetTimer() 182 b.RunParallel(func(pb *testing.PB) { 183 for pb.Next() { 184 randomSize := rand.Intn(pool.maxSize) 185 data := pool.Get(randomSize) 186 pool.Put(data) 187 } 188 }) 189 } 190 191 func BenchmarkPoolGet(b *testing.B) { 192 pool := New(2, 16384) 193 b.SetParallelism(16) 194 b.ResetTimer() 195 b.RunParallel(func(pb *testing.PB) { 196 for pb.Next() { 197 randomSize := rand.Intn(pool.maxSize) 198 data := pool.Get(randomSize) 199 _ = data 200 } 201 }) 202 }