github.com/sandwich-go/boost@v1.3.29/xpool/goroutine_benchmark_test.go (about) 1 package xpool 2 3 import ( 4 "sync" 5 "testing" 6 "time" 7 ) 8 9 const ( 10 runTimes = 1000000 11 poolSize = 50000 12 queueSize = 5000 13 ) 14 15 func demoTask() { 16 time.Sleep(time.Millisecond * 10) 17 } 18 19 func BenchmarkGoroutine(b *testing.B) { 20 var wg sync.WaitGroup 21 for i := 0; i < b.N; i++ { 22 wg.Add(runTimes) 23 24 for j := 0; j < runTimes; j++ { 25 go func() { 26 defer wg.Done() 27 demoTask() 28 }() 29 } 30 31 wg.Wait() 32 } 33 } 34 35 func BenchmarkGoroutinePool(b *testing.B) { 36 pool := NewGoroutinePool(poolSize, queueSize, time.Duration(0)) 37 defer pool.Close() 38 var wg sync.WaitGroup 39 40 for i := 0; i < b.N; i++ { 41 wg.Add(runTimes) 42 for j := 0; j < runTimes; j++ { 43 pool.jobQueue <- func() { 44 defer wg.Done() 45 demoTask() 46 } 47 } 48 } 49 }