github.com/pawelgaczynski/gain@v0.4.0-alpha.0.20230821120126-41f1e60a18da/pkg/pool/ringbuffer/ringbuffer_test.go (about)

     1  // Copyright (c) 2023 Paweł Gaczyński
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ringbuffer
    16  
    17  import (
    18  	"os"
    19  	"runtime"
    20  	"testing"
    21  
    22  	"github.com/pawelgaczynski/gain/pkg/buffer/magicring"
    23  	. "github.com/stretchr/testify/require"
    24  )
    25  
    26  func TestIndexRingBufferPool(t *testing.T) {
    27  	idx := indexRingBufferPool(64)
    28  	Equal(t, 0, idx)
    29  	idx = indexRingBufferPool(128)
    30  	Equal(t, 0, idx)
    31  	idx = indexRingBufferPool(256)
    32  	Equal(t, 0, idx)
    33  	idx = indexRingBufferPool(512)
    34  	Equal(t, 0, idx)
    35  	idx = indexRingBufferPool(1024)
    36  	Equal(t, 0, idx)
    37  	idx = indexRingBufferPool(2048)
    38  	Equal(t, 0, idx)
    39  	idx = indexRingBufferPool(4096)
    40  	Equal(t, 0, idx)
    41  	idx = indexRingBufferPool(8192)
    42  	Equal(t, 1, idx)
    43  	idx = indexRingBufferPool(16384)
    44  	Equal(t, 2, idx)
    45  	idx = indexRingBufferPool(32768)
    46  	Equal(t, 3, idx)
    47  	idx = indexRingBufferPool(65536)
    48  	Equal(t, 4, idx)
    49  	idx = indexRingBufferPool(131072)
    50  	Equal(t, 5, idx)
    51  	idx = indexRingBufferPool(262144)
    52  	Equal(t, 6, idx)
    53  	idx = indexRingBufferPool(524288)
    54  	Equal(t, 7, idx)
    55  	idx = indexRingBufferPool(1048576)
    56  	Equal(t, 8, idx)
    57  	idx = indexRingBufferPool(2097152)
    58  	Equal(t, 9, idx)
    59  	idx = indexRingBufferPool(4194304)
    60  	Equal(t, 10, idx)
    61  	idx = indexRingBufferPool(8388608)
    62  	Equal(t, 11, idx)
    63  	idx = indexRingBufferPool(16777216)
    64  	Equal(t, 12, idx)
    65  	idx = indexRingBufferPool(33554432)
    66  	Equal(t, 13, idx)
    67  	idx = indexRingBufferPool(67108864)
    68  	Equal(t, 14, idx)
    69  	idx = indexRingBufferPool(134217728)
    70  	Equal(t, 14, idx)
    71  }
    72  
    73  func TestRingBufferPool(t *testing.T) {
    74  	pool := NewRingBufferPool()
    75  
    76  	ringBuffer := magicring.NewMagicBuffer(os.Getpagesize())
    77  
    78  	pool.Put(ringBuffer)
    79  
    80  	ringBufferFromPool := pool.Get()
    81  
    82  	Equal(t, ringBuffer, ringBufferFromPool)
    83  	runtime.KeepAlive(ringBuffer)
    84  
    85  	ringBuffer = Get()
    86  
    87  	Put(ringBuffer)
    88  
    89  	ringBufferFromPool = Get()
    90  
    91  	Equal(t, ringBuffer, ringBufferFromPool)
    92  	runtime.KeepAlive(ringBuffer)
    93  }
    94  
    95  func TestRingBufferPoolCalibrate(t *testing.T) {
    96  	pool := NewRingBufferPool()
    97  
    98  	Equal(t, uint64(magicring.DefaultMagicBufferSize), pool.defaultSize)
    99  	// Equal(t, uint64(magicring.DefaultMagicBufferSize), pool.maxSize)
   100  
   101  	pool.calibrating = 0
   102  
   103  	pool.calibrate()
   104  
   105  	Equal(t, uint64(magicring.DefaultMagicBufferSize), pool.defaultSize)
   106  	Equal(t, uint64(67108864), pool.maxSize)
   107  
   108  	pool.calls[4] = 3278
   109  	pool.calls[9] = 12312
   110  	pool.calls[13] = 1000
   111  
   112  	pool.calibrate()
   113  
   114  	Equal(t, uint64(2097152), pool.defaultSize)
   115  	Equal(t, uint64(33554432), pool.maxSize)
   116  
   117  	pool.calibrating = 1
   118  
   119  	pool.calls[4] = 32780
   120  	pool.calls[9] = 12312
   121  	pool.calls[13] = 21000
   122  
   123  	pool.calibrate()
   124  
   125  	Equal(t, uint64(2097152), pool.defaultSize)
   126  	Equal(t, uint64(33554432), pool.maxSize)
   127  }