github.com/bytedance/gopkg@v0.0.0-20240514070511-01b2cbcf35e1/lang/syncx/pool_test.go (about)

     1  // Copyright 2021 ByteDance Inc.
     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 syncx
    16  
    17  import (
    18  	"sync"
    19  	"testing"
    20  )
    21  
    22  func TestSyncXPool(t *testing.T) {
    23  	var pool = sync.Pool{
    24  		New: func() interface{} {
    25  			return make([]byte, 1024)
    26  		},
    27  	}
    28  	for i := 0; i < 1024; i++ {
    29  		b := pool.Get().([]byte)
    30  		pool.Put(b)
    31  	}
    32  
    33  	var bs = make([][]byte, p)
    34  	for i := 0; i < 1024; i++ {
    35  		for i := 0; i < p; i++ {
    36  			bs[i] = pool.Get().([]byte)
    37  		}
    38  		for i := 0; i < p; i++ {
    39  			pool.Put(bs[i])
    40  		}
    41  	}
    42  }
    43  
    44  func TestRaceSyncXPool(t *testing.T) {
    45  	var pool = sync.Pool{
    46  		New: func() interface{} {
    47  			return make([]byte, 1024)
    48  		},
    49  	}
    50  	parallel := 8
    51  	var wg sync.WaitGroup
    52  	wg.Add(parallel)
    53  	for i := 0; i < parallel; i++ {
    54  		go func() {
    55  			defer wg.Done()
    56  			var bs = make([][]byte, p)
    57  			for i := 0; i < 8; i++ {
    58  				for i := 0; i < p; i++ {
    59  					bs[i] = pool.Get().([]byte)
    60  				}
    61  				for i := 0; i < p; i++ {
    62  					pool.Put(bs[i])
    63  				}
    64  			}
    65  		}()
    66  	}
    67  	wg.Wait()
    68  }
    69  
    70  var p = 1024
    71  
    72  func BenchmarkSyncPool(b *testing.B) {
    73  	var pool = sync.Pool{
    74  		New: func() interface{} {
    75  			return make([]byte, 1024)
    76  		},
    77  	}
    78  
    79  	var bs = make([][]byte, p)
    80  
    81  	// benchmark
    82  	b.ReportAllocs()
    83  	b.ResetTimer()
    84  	for i := 0; i < b.N; i++ {
    85  		for i := 0; i < p; i++ {
    86  			bs[i] = pool.Get().([]byte)
    87  		}
    88  		for i := 0; i < p; i++ {
    89  			pool.Put(bs[i])
    90  		}
    91  	}
    92  }
    93  
    94  func BenchmarkSyncXPool(b *testing.B) {
    95  	var pool = Pool{
    96  		New: func() interface{} {
    97  			return make([]byte, 1024)
    98  		},
    99  		NoGC: true,
   100  	}
   101  
   102  	var bs = make([][]byte, p)
   103  
   104  	// benchmark
   105  	b.ReportAllocs()
   106  	b.ResetTimer()
   107  	for i := 0; i < b.N; i++ {
   108  		for i := 0; i < p; i++ {
   109  			bs[i] = pool.Get().([]byte)
   110  		}
   111  		for i := 0; i < p; i++ {
   112  			pool.Put(bs[i])
   113  		}
   114  	}
   115  }
   116  
   117  func BenchmarkSyncPoolParallel(b *testing.B) {
   118  	var pool = sync.Pool{
   119  		New: func() interface{} {
   120  			return make([]byte, 1024)
   121  		},
   122  	}
   123  
   124  	// benchmark
   125  	b.ReportAllocs()
   126  	b.SetParallelism(16)
   127  	b.ResetTimer()
   128  	b.RunParallel(func(pb *testing.PB) {
   129  		var bs = make([][]byte, p)
   130  		for pb.Next() {
   131  			for i := 0; i < p; i++ {
   132  				bs[i] = pool.Get().([]byte)
   133  			}
   134  			for i := 0; i < p; i++ {
   135  				pool.Put(bs[i])
   136  			}
   137  		}
   138  	})
   139  }
   140  
   141  func BenchmarkSyncXPoolParallel(b *testing.B) {
   142  	var pool = Pool{
   143  		New: func() interface{} {
   144  			return make([]byte, 1024)
   145  		},
   146  		NoGC: true,
   147  	}
   148  
   149  	// benchmark
   150  	b.ReportAllocs()
   151  	b.SetParallelism(16)
   152  	b.ResetTimer()
   153  	b.RunParallel(func(pb *testing.PB) {
   154  		var bs = make([][]byte, p)
   155  		for pb.Next() {
   156  			for i := 0; i < p; i++ {
   157  				bs[i] = pool.Get().([]byte)
   158  			}
   159  			for i := 0; i < p; i++ {
   160  				pool.Put(bs[i])
   161  			}
   162  		}
   163  	})
   164  }
   165  
   166  func BenchmarkSyncPoolParallel1(b *testing.B) {
   167  	var pool = sync.Pool{
   168  		New: func() interface{} {
   169  			return make([]byte, 1024)
   170  		},
   171  	}
   172  
   173  	// benchmark
   174  	b.ReportAllocs()
   175  	b.SetParallelism(16)
   176  	b.ResetTimer()
   177  	b.RunParallel(func(pb *testing.PB) {
   178  		var bs []byte
   179  		for pb.Next() {
   180  			bs = pool.Get().([]byte)
   181  			pool.Put(bs)
   182  		}
   183  	})
   184  }
   185  
   186  func BenchmarkSyncXPoolParallel1(b *testing.B) {
   187  	var pool = Pool{
   188  		New: func() interface{} {
   189  			return make([]byte, 1024)
   190  		},
   191  		NoGC: true,
   192  	}
   193  
   194  	// benchmark
   195  	b.ReportAllocs()
   196  	b.SetParallelism(16)
   197  	b.ResetTimer()
   198  	b.RunParallel(func(pb *testing.PB) {
   199  		var bs []byte
   200  		for pb.Next() {
   201  			bs = pool.Get().([]byte)
   202  			pool.Put(bs)
   203  		}
   204  	})
   205  }