github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/pool_test.go (about)

     1  // Copyright 2022 Matrix Origin
     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 fileservice
    16  
    17  import (
    18  	"encoding/binary"
    19  	"sync"
    20  	"testing"
    21  )
    22  
    23  func TestBytesPool(t *testing.T) {
    24  	pool := NewPool(8, func() *[]byte {
    25  		bs := make([]byte, 8)
    26  		return &bs
    27  	}, nil, nil)
    28  
    29  	wg := new(sync.WaitGroup)
    30  	for i := 0; i < 200; i++ {
    31  		wg.Add(1)
    32  		i := i
    33  		go func() {
    34  			defer wg.Done()
    35  			for j := 0; j < 200; j++ {
    36  				var bs *[]byte
    37  				put := pool.Get(&bs)
    38  				defer put.Put()
    39  				binary.PutUvarint(*bs, uint64(i))
    40  			}
    41  		}()
    42  	}
    43  	wg.Wait()
    44  
    45  }
    46  
    47  func BenchmarkBytesPool(b *testing.B) {
    48  	pool := NewPool(1024, func() any {
    49  		bs := make([]byte, 8)
    50  		return &bs
    51  	}, nil, nil)
    52  	b.ResetTimer()
    53  	for i := 0; i < b.N; i++ {
    54  		var v any
    55  		put := pool.Get(&v)
    56  		put.Put()
    57  	}
    58  }
    59  
    60  func BenchmarkParallelBytesPool(b *testing.B) {
    61  	pool := NewPool(1024, func() any {
    62  		bs := make([]byte, 8)
    63  		return &bs
    64  	}, nil, nil)
    65  	b.ResetTimer()
    66  	b.RunParallel(func(pb *testing.PB) {
    67  		for pb.Next() {
    68  			var v any
    69  			put := pool.Get(&v)
    70  			put.Put()
    71  		}
    72  	})
    73  }