vitess.io/vitess@v0.16.2/go/bucketpool/bucketpool_test.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package bucketpool
    18  
    19  import (
    20  	"math/rand"
    21  	"testing"
    22  )
    23  
    24  func TestPool(t *testing.T) {
    25  	maxSize := 16384
    26  	pool := New(1024, maxSize)
    27  	if pool.maxSize != maxSize {
    28  		t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
    29  	}
    30  	if len(pool.pools) != 5 {
    31  		t.Fatalf("Invalid number of pools: %d, expected %d", len(pool.pools), 5)
    32  	}
    33  
    34  	buf := pool.Get(64)
    35  	if len(*buf) != 64 {
    36  		t.Fatalf("unexpected buf length: %d", len(*buf))
    37  	}
    38  	if cap(*buf) != 1024 {
    39  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
    40  	}
    41  
    42  	// get from same pool, check that length is right
    43  	buf = pool.Get(128)
    44  	if len(*buf) != 128 {
    45  		t.Fatalf("unexpected buf length: %d", len(*buf))
    46  	}
    47  	if cap(*buf) != 1024 {
    48  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
    49  	}
    50  	pool.Put(buf)
    51  
    52  	// get boundary size
    53  	buf = pool.Get(1024)
    54  	if len(*buf) != 1024 {
    55  		t.Fatalf("unexpected buf length: %d", len(*buf))
    56  	}
    57  	if cap(*buf) != 1024 {
    58  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
    59  	}
    60  	pool.Put(buf)
    61  
    62  	// get from the middle
    63  	buf = pool.Get(5000)
    64  	if len(*buf) != 5000 {
    65  		t.Fatalf("unexpected buf length: %d", len(*buf))
    66  	}
    67  	if cap(*buf) != 8192 {
    68  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
    69  	}
    70  	pool.Put(buf)
    71  
    72  	// check last pool
    73  	buf = pool.Get(16383)
    74  	if len(*buf) != 16383 {
    75  		t.Fatalf("unexpected buf length: %d", len(*buf))
    76  	}
    77  	if cap(*buf) != 16384 {
    78  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
    79  	}
    80  	pool.Put(buf)
    81  
    82  	// get big buffer
    83  	buf = pool.Get(16385)
    84  	if len(*buf) != 16385 {
    85  		t.Fatalf("unexpected buf length: %d", len(*buf))
    86  	}
    87  	if cap(*buf) != 16385 {
    88  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
    89  	}
    90  	pool.Put(buf)
    91  }
    92  
    93  func TestPoolOneSize(t *testing.T) {
    94  	maxSize := 1024
    95  	pool := New(1024, maxSize)
    96  	if pool.maxSize != maxSize {
    97  		t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
    98  	}
    99  	buf := pool.Get(64)
   100  	if len(*buf) != 64 {
   101  		t.Fatalf("unexpected buf length: %d", len(*buf))
   102  	}
   103  	if cap(*buf) != 1024 {
   104  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
   105  	}
   106  	pool.Put(buf)
   107  
   108  	buf = pool.Get(1025)
   109  	if len(*buf) != 1025 {
   110  		t.Fatalf("unexpected buf length: %d", len(*buf))
   111  	}
   112  	if cap(*buf) != 1025 {
   113  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
   114  	}
   115  	pool.Put(buf)
   116  }
   117  
   118  func TestPoolTwoSizeNotMultiplier(t *testing.T) {
   119  	maxSize := 2000
   120  	pool := New(1024, maxSize)
   121  	if pool.maxSize != maxSize {
   122  		t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
   123  	}
   124  	buf := pool.Get(64)
   125  	if len(*buf) != 64 {
   126  		t.Fatalf("unexpected buf length: %d", len(*buf))
   127  	}
   128  	if cap(*buf) != 1024 {
   129  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
   130  	}
   131  	pool.Put(buf)
   132  
   133  	buf = pool.Get(2001)
   134  	if len(*buf) != 2001 {
   135  		t.Fatalf("unexpected buf length: %d", len(*buf))
   136  	}
   137  	if cap(*buf) != 2001 {
   138  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
   139  	}
   140  	pool.Put(buf)
   141  }
   142  
   143  func TestPoolWeirdMaxSize(t *testing.T) {
   144  	maxSize := 15000
   145  	pool := New(1024, maxSize)
   146  	if pool.maxSize != maxSize {
   147  		t.Fatalf("Invalid max pool size: %d, expected %d", pool.maxSize, maxSize)
   148  	}
   149  
   150  	buf := pool.Get(14000)
   151  	if len(*buf) != 14000 {
   152  		t.Fatalf("unexpected buf length: %d", len(*buf))
   153  	}
   154  	if cap(*buf) != 15000 {
   155  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
   156  	}
   157  	pool.Put(buf)
   158  
   159  	buf = pool.Get(16383)
   160  	if len(*buf) != 16383 {
   161  		t.Fatalf("unexpected buf length: %d", len(*buf))
   162  	}
   163  	if cap(*buf) != 16383 {
   164  		t.Fatalf("unexpected buf cap: %d", cap(*buf))
   165  	}
   166  	pool.Put(buf)
   167  }
   168  
   169  func TestFuzz(t *testing.T) {
   170  	maxTestSize := 16384
   171  	for i := 0; i < 20000; i++ {
   172  		minSize := rand.Intn(maxTestSize)
   173  		if minSize == 0 {
   174  			minSize = 1
   175  		}
   176  		maxSize := rand.Intn(maxTestSize-minSize) + minSize
   177  		p := New(minSize, maxSize)
   178  		bufSize := rand.Intn(maxTestSize)
   179  		buf := p.Get(bufSize)
   180  		if len(*buf) != bufSize {
   181  			t.Fatalf("Invalid length %d, expected %d", len(*buf), bufSize)
   182  		}
   183  		sPool := p.findPool(bufSize)
   184  		if sPool == nil {
   185  			if cap(*buf) != len(*buf) {
   186  				t.Fatalf("Invalid cap %d, expected %d", cap(*buf), len(*buf))
   187  			}
   188  		} else {
   189  			if cap(*buf) != sPool.size {
   190  				t.Fatalf("Invalid cap %d, expected %d", cap(*buf), sPool.size)
   191  			}
   192  		}
   193  		p.Put(buf)
   194  	}
   195  }
   196  
   197  func BenchmarkPool(b *testing.B) {
   198  	pool := New(2, 16384)
   199  	b.SetParallelism(16)
   200  	b.ResetTimer()
   201  	b.RunParallel(func(pb *testing.PB) {
   202  		for pb.Next() {
   203  			randomSize := rand.Intn(pool.maxSize)
   204  			data := pool.Get(randomSize)
   205  			pool.Put(data)
   206  		}
   207  	})
   208  }
   209  
   210  func BenchmarkPoolGet(b *testing.B) {
   211  	pool := New(2, 16384)
   212  	b.SetParallelism(16)
   213  	b.ResetTimer()
   214  	b.RunParallel(func(pb *testing.PB) {
   215  		for pb.Next() {
   216  			randomSize := rand.Intn(pool.maxSize)
   217  			data := pool.Get(randomSize)
   218  			_ = data
   219  		}
   220  	})
   221  }