diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index c8364e5..360443a 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -25,15 +25,16 @@ package ants import ( "runtime" "sync" - "sync/atomic" "testing" "time" + + "golang.org/x/sync/errgroup" ) const ( - RunTimes = 1000000 + RunTimes = 1e6 + PoolCap = 5e4 BenchParam = 10 - BenchAntsSize = 200000 DefaultExpiredTime = 10 * time.Second ) @@ -76,10 +77,11 @@ func BenchmarkGoroutines(b *testing.B) { } } -func BenchmarkSemaphore(b *testing.B) { +func BenchmarkChannel(b *testing.B) { var wg sync.WaitGroup - sema := make(chan struct{}, BenchAntsSize) + sema := make(chan struct{}, PoolCap) + b.ResetTimer() for i := 0; i < b.N; i++ { wg.Add(RunTimes) for j := 0; j < RunTimes; j++ { @@ -94,12 +96,31 @@ func BenchmarkSemaphore(b *testing.B) { } } +func BenchmarkErrGroup(b *testing.B) { + var wg sync.WaitGroup + var pool errgroup.Group + pool.SetLimit(PoolCap) + + b.ResetTimer() + for i := 0; i < b.N; i++ { + wg.Add(RunTimes) + for j := 0; j < RunTimes; j++ { + pool.Go(func() error { + demoFunc() + wg.Done() + return nil + }) + } + wg.Wait() + } +} + func BenchmarkAntsPool(b *testing.B) { var wg sync.WaitGroup - p, _ := NewPool(BenchAntsSize, WithExpiryDuration(DefaultExpiredTime)) + p, _ := NewPool(PoolCap, WithExpiryDuration(DefaultExpiredTime)) defer p.Release() - b.StartTimer() + b.ResetTimer() for i := 0; i < b.N; i++ { wg.Add(RunTimes) for j := 0; j < RunTimes; j++ { @@ -110,7 +131,6 @@ func BenchmarkAntsPool(b *testing.B) { } wg.Wait() } - b.StopTimer() } func BenchmarkGoroutinesThroughput(b *testing.B) { @@ -122,7 +142,7 @@ func BenchmarkGoroutinesThroughput(b *testing.B) { } func BenchmarkSemaphoreThroughput(b *testing.B) { - sema := make(chan struct{}, BenchAntsSize) + sema := make(chan struct{}, PoolCap) for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { sema <- struct{}{} @@ -135,54 +155,13 @@ func BenchmarkSemaphoreThroughput(b *testing.B) { } func BenchmarkAntsPoolThroughput(b *testing.B) { - p, _ := NewPool(BenchAntsSize, WithExpiryDuration(DefaultExpiredTime)) + p, _ := NewPool(PoolCap, WithExpiryDuration(DefaultExpiredTime)) defer p.Release() - b.StartTimer() - for i := 0; i < b.N; i++ { - for j := 0; j < RunTimes; j++ { - _ = p.Submit(demoFunc) - } - } - b.StopTimer() -} - -func BenchmarkTimeNow(b *testing.B) { - for i := 0; i < b.N; i++ { - _ = time.Now() - } -} - -func BenchmarkTimeNowCache(b *testing.B) { - var ( - now atomic.Value - offset int32 - ) - - now.Store(time.Now()) - go func() { - for range time.Tick(500 * time.Millisecond) { - now.Store(time.Now()) - atomic.StoreInt32(&offset, 0) - } - }() b.ResetTimer() for i := 0; i < b.N; i++ { - _ = now.Load().(time.Time).Add(time.Duration(atomic.AddInt32(&offset, 1))) - } -} - -func BenchmarkTimeNowCache1(b *testing.B) { - var now atomic.Value - now.Store(time.Now()) - go func() { - for range time.Tick(500 * time.Millisecond) { - now.Store(time.Now()) + for j := 0; j < RunTimes; j++ { + _ = p.Submit(demoFunc) } - }() - - b.ResetTimer() - for i := 0; i < b.N; i++ { - _ = now.Load().(time.Time) } } diff --git a/go.mod b/go.mod index df32ce2..e307bfb 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module github.com/panjf2000/ants/v2 go 1.13 -require github.com/stretchr/testify v1.8.1 +require ( + github.com/stretchr/testify v1.8.1 + golang.org/x/sync v0.1.0 +) diff --git a/go.sum b/go.sum index 2ec90f7..96e82f8 100644 --- a/go.sum +++ b/go.sum @@ -10,6 +10,8 @@ github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= +golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= +golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=