From c1738ae964488c4bc02662d5a52e54bd239a2e44 Mon Sep 17 00:00:00 2001 From: Egon Elbre Date: Wed, 3 Oct 2018 10:12:58 +0300 Subject: [PATCH 1/5] 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/5] 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 ) From 143118be9ad690560f6805212edd6b1058500297 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Wed, 3 Oct 2018 19:11:04 +0800 Subject: [PATCH 3/5] add go1.11 into ci --- .travis.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index d0d41d5..3c10beb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,6 +4,7 @@ go: - "1.8.x" - "1.9.x" - "1.10.x" + - "1.10.x" - master before_install: @@ -14,4 +15,4 @@ script: # - go test -bench=. -benchmem=true -run=none ./... after_success: - - bash <(curl -s https://codecov.io/bash) \ No newline at end of file + - bash <(curl -s https://codecov.io/bash) From cabe428527e77dfd148057403b19d74604a3ab3c Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Wed, 3 Oct 2018 19:12:05 +0800 Subject: [PATCH 4/5] add go1.11 into ci --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 3c10beb..dd159d0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ go: - "1.8.x" - "1.9.x" - "1.10.x" - - "1.10.x" + - "1.11.x" - master before_install: From 711dbdb7a222771ce15aaee1bb7b7c6e9731f208 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Wed, 3 Oct 2018 20:55:39 +0800 Subject: [PATCH 5/5] optimization for benchmark test --- ants_benchmark_test.go | 59 ++++++++++++++---------------------------- ants_test.go | 24 ++++++++++++++--- 2 files changed, 40 insertions(+), 43 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index f79cac1..416f2a7 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -31,21 +31,9 @@ import ( ) const ( - _ = 1 << (10 * iota) - KiB // 1024 - MiB // 1048576 - GiB // 1073741824 - TiB // 1099511627776 (超过了int32的范围) - PiB // 1125899906842624 - EiB // 1152921504606846976 - ZiB // 1180591620717411303424 (超过了int64的范围) - YiB // 1208925819614629174706176 -) -const ( - RunTimes = 10000000 - Param = 100 - AntsSize = 1000 - TestSize = 10000 + RunTimes = 10000000 + benchParam = 10 + benchAntsSize = 100000 ) func demoFunc() error { @@ -69,10 +57,10 @@ func demoPoolFunc(args interface{}) error { func BenchmarkGoroutineWithFunc(b *testing.B) { var wg sync.WaitGroup for i := 0; i < b.N; i++ { + wg.Add(RunTimes) for j := 0; j < RunTimes; j++ { - wg.Add(1) go func() { - demoPoolFunc(Param) + demoPoolFunc(benchParam) wg.Done() }() } @@ -82,14 +70,14 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { func BenchmarkSemaphoreWithFunc(b *testing.B) { var wg sync.WaitGroup - sema := make(chan struct{}, AntsSize) + sema := make(chan struct{}, benchAntsSize) for i := 0; i < b.N; i++ { wg.Add(RunTimes) for j := 0; j < RunTimes; j++ { sema <- struct{}{} go func() { - demoPoolFunc(Param) + demoPoolFunc(benchParam) <-sema wg.Done() }() @@ -100,63 +88,54 @@ func BenchmarkSemaphoreWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) { var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error { + p, _ := ants.NewPoolWithFunc(benchAntsSize, func(i interface{}) error { demoPoolFunc(i) wg.Done() return nil }) defer p.Release() - b.ResetTimer() + b.StartTimer() for i := 0; i < b.N; i++ { + wg.Add(RunTimes) for j := 0; j < RunTimes; j++ { - wg.Add(1) - p.Serve(Param) + p.Serve(benchParam) } wg.Wait() //b.Logf("running goroutines: %d", p.Running()) } + b.StopTimer() } 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 func() { - demoPoolFunc(Param) - wg.Done() - }() + go demoPoolFunc(benchParam) } } - wg.Wait() } func BenchmarkSemaphore(b *testing.B) { - var wg sync.WaitGroup - sema := make(chan struct{}, AntsSize) - - wg.Add(RunTimes * b.N) + sema := make(chan struct{}, benchAntsSize) for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { sema <- struct{}{} go func() { - demoPoolFunc(Param) + demoPoolFunc(benchParam) <-sema - wg.Done() }() } } - wg.Wait() } func BenchmarkAntsPool(b *testing.B) { - p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) + p, _ := ants.NewPoolWithFunc(benchAntsSize, demoPoolFunc) defer p.Release() - b.ResetTimer() + b.StartTimer() for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { - p.Serve(Param) + p.Serve(benchParam) } } + b.StopTimer() } diff --git a/ants_test.go b/ants_test.go index 3fb42a0..2ced83d 100644 --- a/ants_test.go +++ b/ants_test.go @@ -31,7 +31,25 @@ import ( "github.com/panjf2000/ants" ) -var n = 100000 +const ( + _ = 1 << (10 * iota) + KiB // 1024 + MiB // 1048576 + GiB // 1073741824 + TiB // 1099511627776 (超过了int32的范围) + PiB // 1125899906842624 + EiB // 1152921504606846976 + ZiB // 1180591620717411303424 (超过了int64的范围) + YiB // 1208925819614629174706176 +) + +const ( + Param = 100 + AntsSize = 1000 + TestSize = 10000 + n = 100000 +) + var curMem uint64 func TestAntsPoolWithFunc(t *testing.T) { @@ -51,7 +69,7 @@ func TestAntsPoolWithFunc(t *testing.T) { t.Logf("pool with func, running workers number:%d", p.Running()) mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - curMem = mem.TotalAlloc / MiB - curMem + curMem = mem.TotalAlloc/MiB - curMem t.Logf("memory usage:%d MB", curMem) } func TestNoPool(t *testing.T) { @@ -130,4 +148,4 @@ func TestCodeCov(t *testing.T) { p.ReSize(TestSize) p.ReSize(AntsSize) t.Logf("pool with func, after resize, capacity:%d, running:%d", p.Cap(), p.Running()) -} \ No newline at end of file +}