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