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