From 5b274e54b4fb17c88d66191c600a7d61fa9a75a3 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 20 May 2018 11:51:14 +0800 Subject: [PATCH] test --- ants.go | 4 --- ants_benchmark_test.go | 38 ++++++++++++++----------- ants_test.go | 63 ++++++++++++++++++++++++------------------ pool.go | 21 ++++++++++---- worker.go | 1 - 5 files changed, 73 insertions(+), 54 deletions(-) diff --git a/ants.go b/ants.go index 5f1166c..b55872e 100644 --- a/ants.go +++ b/ants.go @@ -19,7 +19,3 @@ func Cap() int { func Free() int { return defaultPool.Free() } - -func Wait() { - defaultPool.Wait() -} diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 029c768..be54823 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -6,14 +6,33 @@ import ( "sync" ) -const RunTimes = 100000 +const RunTimes = 10 + +func BenchmarkGoroutine(b *testing.B) { + for i := 0; i < b.N; i++ { + var wg sync.WaitGroup + for j := 0; j < RunTimes; j++ { + wg.Add(1) + go func() { + forSleep() + wg.Done() + }() + } + wg.Wait() + } +} func BenchmarkPoolGroutine(b *testing.B) { for i := 0; i < b.N; i++ { + var wg sync.WaitGroup for j := 0; j < RunTimes; j++ { - ants.Push(demoFunc) + wg.Add(1) + ants.Push(func() { + forSleep() + wg.Done() + }) } - ants.Wait() + wg.Wait() } } @@ -25,16 +44,3 @@ func BenchmarkPoolGroutine(b *testing.B) { // } //} -func BenchmarkGoroutine(b *testing.B) { - for i := 0; i < b.N; i++ { - var wg sync.WaitGroup - for j := 0; j < RunTimes; j++ { - wg.Add(1) - go func() { - defer wg.Done() - demoFunc() - }() - } - wg.Wait() - } -} diff --git a/ants_test.go b/ants_test.go index 21fb2f7..a8b79a6 100644 --- a/ants_test.go +++ b/ants_test.go @@ -5,16 +5,17 @@ import ( "github.com/panjf2000/ants" "sync" "runtime" + "time" ) -var n = 100000 +var n = 1000000 -func demoFunc() { - var n int - for i := 0; i < 1000000; i++ { - n += i - } -} +//func demoFunc() { +// var n int +// for i := 0; i < 1000000; i++ { +// n += i +// } +//} //func demoFunc() { // var n int @@ -24,17 +25,41 @@ func demoFunc() { // fmt.Printf("finish task with result:%d\n", n) //} -func TestDefaultPool(t *testing.T) { +func forSleep() { + time.Sleep(time.Millisecond) +} + +func TestNoPool(t *testing.T) { + var wg sync.WaitGroup for i := 0; i < n; i++ { - ants.Push(demoFunc) + wg.Add(1) + go func() { + forSleep() + wg.Done() + }() } + wg.Wait() + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + t.Logf("memory usage:%d", mem.TotalAlloc/1024) +} + +func TestDefaultPool(t *testing.T) { + var wg sync.WaitGroup + for i := 0; i < n; i++ { + wg.Add(1) + ants.Push(func() { + forSleep() + wg.Done() + }) + } + wg.Wait() + //t.Logf("pool capacity:%d", ants.Cap()) //t.Logf("running workers number:%d", ants.Running()) //t.Logf("free workers number:%d", ants.Free()) - ants.Wait() - mem := runtime.MemStats{} runtime.ReadMemStats(&mem) t.Logf("memory usage:%d", mem.TotalAlloc/1024) @@ -54,19 +79,3 @@ func TestDefaultPool(t *testing.T) { // runtime.ReadMemStats(&mem) // //} - -func TestNoPool(t *testing.T) { - var wg sync.WaitGroup - for i := 0; i < n; i++ { - wg.Add(1) - go func() { - defer wg.Done() - demoFunc() - }() - } - - wg.Wait() - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/1024) -} diff --git a/pool.go b/pool.go index 3d01531..8eb4215 100644 --- a/pool.go +++ b/pool.go @@ -18,7 +18,6 @@ type Pool struct { workerPool sync.Pool destroy chan sig m sync.Mutex - wg sync.WaitGroup } func NewPool(size int) *Pool { @@ -27,16 +26,30 @@ func NewPool(size int) *Pool { freeSignal: make(chan sig, size), destroy: make(chan sig, runtime.GOMAXPROCS(-1)), } + return p } //------------------------------------------------------------------------- +//func (p *Pool) loop() { +// for i := 0; i < runtime.GOMAXPROCS(-1); i++ { +// go func() { +// for { +// select { +// case <-p.launchSignal: +// p.getWorker().sendTask(p.tasks.pop().(f)) +// case <-p.destroy: +// return +// } +// } +// }() +// } +//} func (p *Pool) Push(task f) error { if len(p.destroy) > 0 { return nil } - p.wg.Add(1) w := p.getWorker() w.sendTask(task) return nil @@ -54,10 +67,6 @@ func (p *Pool) Cap() int { return int(atomic.LoadInt32(&p.capacity)) } -func (p *Pool) Wait() { - p.wg.Wait() -} - func (p *Pool) Destroy() error { p.m.Lock() defer p.m.Unlock() diff --git a/worker.go b/worker.go index ed13b39..137ad7e 100644 --- a/worker.go +++ b/worker.go @@ -18,7 +18,6 @@ func (w *Worker) run() { } f() w.pool.putWorker(w) - w.pool.wg.Done() } }() }