From c1738ae964488c4bc02662d5a52e54bd239a2e44 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Wed, 3 Oct 2018 10:12:58 +0300 Subject: [PATCH 1/2] fix benchmarks and add semaphore --- ants_benchmark_test.go | 47 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index b6c67ed..7633220 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -43,7 +43,7 @@ const ( ) const ( RunTimes = 10000000 - Param = 100 + Param = 0 AntsSize = 1000 TestSize = 10000 ) @@ -68,7 +68,6 @@ func demoPoolFunc(args interface{}) error { func BenchmarkGoroutineWithFunc(b *testing.B) { var wg sync.WaitGroup - for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { wg.Add(1) @@ -81,6 +80,24 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { } } +func BenchmarkSemaphoreWithFunc(b *testing.B) { + var wg sync.WaitGroup + sema := make(chan struct{}, AntsSize) + + for i := 0; i < b.N; i++ { + wg.Add(RunTimes) + for j := 0; j < RunTimes; j++ { + sema <- struct{}{} + go func() { + demoPoolFunc(Param) + <-sema + wg.Done() + }() + } + wg.Wait() + } +} + func BenchmarkAntsPoolWithFunc(b *testing.B) { var wg sync.WaitGroup p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error { @@ -102,11 +119,35 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { } func BenchmarkGoroutine(b *testing.B) { + var wg sync.WaitGroup + wg.Add(b.N * RunTimes) for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { - go demoPoolFunc(Param) + go func() { + demoPoolFunc(Param) + wg.Done() + }() } } + wg.Wait() +} + +func BenchmarkSemaphore(b *testing.B) { + var wg sync.WaitGroup + sema := make(chan struct{}, AntsSize) + + wg.Add(RunTimes * b.N) + for i := 0; i < b.N; i++ { + for j := 0; j < RunTimes; j++ { + sema <- struct{}{} + go func() { + demoPoolFunc(Param) + <-sema + wg.Done() + }() + } + } + wg.Wait() } func BenchmarkAntsPool(b *testing.B) { From 3070771e41f0df591619c94ed0bc5c8025451302 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Wed, 3 Oct 2018 10:15:09 +0300 Subject: [PATCH 2/2] reset param change --- ants_benchmark_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 7633220..f79cac1 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -43,7 +43,7 @@ const ( ) const ( RunTimes = 10000000 - Param = 0 + Param = 100 AntsSize = 1000 TestSize = 10000 )