github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zpool/pool_benchmark_test.go (about) 1 /* 2 go test -bench='.*' -benchmem -run none -race ./zpool 3 */ 4 5 package zpool_test 6 7 import ( 8 "sync" 9 "testing" 10 "time" 11 12 "github.com/sohaha/zlsgo/zpool" 13 ) 14 15 const runCount = 100000 16 17 func demoFunc() { 18 time.Sleep(time.Duration(10) * time.Millisecond) 19 } 20 21 func BenchmarkGoroutines(b *testing.B) { 22 var wg sync.WaitGroup 23 24 b.StartTimer() 25 for i := 0; i < b.N; i++ { 26 wg.Add(runCount) 27 for j := 0; j < runCount; j++ { 28 go func() { 29 demoFunc() 30 wg.Done() 31 }() 32 } 33 wg.Wait() 34 } 35 b.StopTimer() 36 } 37 38 func BenchmarkPoolWorkerNum100(b *testing.B) { 39 var wg sync.WaitGroup 40 p := zpool.New(100) 41 defer p.Close() 42 _ = p.PreInit() 43 b.StartTimer() 44 for i := 0; i < b.N; i++ { 45 wg.Add(runCount) 46 for j := 0; j < runCount; j++ { 47 _ = p.Do(func() { 48 demoFunc() 49 wg.Done() 50 }) 51 } 52 wg.Wait() 53 } 54 b.StopTimer() 55 } 56 57 func BenchmarkPoolWorkerNum500(b *testing.B) { 58 var wg sync.WaitGroup 59 p := zpool.New(500) 60 defer p.Close() 61 62 b.StartTimer() 63 for i := 0; i < b.N; i++ { 64 wg.Add(runCount) 65 for j := 0; j < runCount; j++ { 66 _ = p.Do(func() { 67 demoFunc() 68 wg.Done() 69 }) 70 } 71 wg.Wait() 72 } 73 b.StopTimer() 74 } 75 76 func BenchmarkPoolWorkerNum1500(b *testing.B) { 77 var wg sync.WaitGroup 78 p := zpool.New(1500) 79 defer p.Close() 80 81 b.StartTimer() 82 for i := 0; i < b.N; i++ { 83 wg.Add(runCount) 84 for j := 0; j < runCount; j++ { 85 _ = p.Do(func() { 86 demoFunc() 87 wg.Done() 88 }) 89 } 90 wg.Wait() 91 } 92 b.StopTimer() 93 } 94 95 func BenchmarkPoolWorkerNum15000(b *testing.B) { 96 var wg sync.WaitGroup 97 p := zpool.New(15000) 98 defer p.Close() 99 100 b.StartTimer() 101 for i := 0; i < b.N; i++ { 102 wg.Add(runCount) 103 for j := 0; j < runCount; j++ { 104 _ = p.Do(func() { 105 demoFunc() 106 wg.Done() 107 }) 108 } 109 wg.Wait() 110 } 111 b.StopTimer() 112 }