diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index e22a4a1..2f44ba1 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -23,9 +23,10 @@ package ants_test import ( - "github.com/panjf2000/ants" "sync" "testing" + + "github.com/panjf2000/ants" ) const RunTimes = 1000000 @@ -53,24 +54,29 @@ func BenchmarkGoroutine(b *testing.B) { } } -//func BenchmarkAntsPool(b *testing.B) { -// for i := 0; i < b.N; i++ { -// var wg sync.WaitGroup -// for j := 0; j < RunTimes; j++ { -// wg.Add(1) -// ants.Push(func() { -// demoFunc() -// wg.Done() -// }) -// } -// wg.Wait() -// } -//} - -func BenchmarkAntsPoolWithFunc(b *testing.B) { - p, _ := ants.NewPoolWithFunc(100000, demoPoolFunc) +func BenchmarkAntsPool(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup + for j := 0; j < RunTimes; j++ { + wg.Add(1) + ants.Push(func() { + demoFunc() + wg.Done() + }) + } + wg.Wait() + } +} + +func BenchmarkAntsPoolWithFunc(b *testing.B) { + for i := 0; i < b.N; i++ { + var wg sync.WaitGroup + p, _ := ants.NewPoolWithFunc(100000, func(i interface{}) error { + demoPoolFunc(i) + wg.Done() + return nil + }) + b.ResetTimer() for j := 0; j < RunTimes; j++ { wg.Add(1) p.Serve(RunTimes) diff --git a/ants_test.go b/ants_test.go index be18314..79a3a57 100644 --- a/ants_test.go +++ b/ants_test.go @@ -23,13 +23,14 @@ package ants_test import ( - "github.com/panjf2000/ants" "runtime" "sync" "testing" + + "github.com/panjf2000/ants" ) -var n = 1000000 +var n = 100000 //func demoFunc() { // var n int @@ -92,7 +93,7 @@ func demoFunc() { func TestAntsPoolWithFunc(t *testing.T) { var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(100000, func(i interface{}) error { + p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { demoPoolFunc(i) wg.Done() return nil diff --git a/pool_func.go b/pool_func.go index 7fcc9b6..81af5a2 100644 --- a/pool_func.go +++ b/pool_func.go @@ -97,7 +97,7 @@ func (p *PoolWithFunc) scanAndClean() { }() } -// Push submit a task to pool +// Serve submit a task to pool func (p *PoolWithFunc) Serve(args interface{}) error { if atomic.LoadInt32(&p.closed) == 1 { return ErrPoolClosed