This commit is contained in:
Andy Pan 2018-05-20 11:51:14 +08:00
parent c5e17c566b
commit 5b274e54b4
5 changed files with 73 additions and 54 deletions

View File

@ -19,7 +19,3 @@ func Cap() int {
func Free() int { func Free() int {
return defaultPool.Free() return defaultPool.Free()
} }
func Wait() {
defaultPool.Wait()
}

View File

@ -6,14 +6,33 @@ import (
"sync" "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) { func BenchmarkPoolGroutine(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
for j := 0; j < RunTimes; j++ { 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()
}
}

View File

@ -5,16 +5,17 @@ import (
"github.com/panjf2000/ants" "github.com/panjf2000/ants"
"sync" "sync"
"runtime" "runtime"
"time"
) )
var n = 100000 var n = 1000000
func demoFunc() { //func demoFunc() {
var n int // var n int
for i := 0; i < 1000000; i++ { // for i := 0; i < 1000000; i++ {
n += i // n += i
} // }
} //}
//func demoFunc() { //func demoFunc() {
// var n int // var n int
@ -24,17 +25,41 @@ func demoFunc() {
// fmt.Printf("finish task with result:%d\n", n) // fmt.Printf("finish task with result:%d\n", n)
//} //}
func TestDefaultPool(t *testing.T) { func forSleep() {
for i := 0; i < n; i++ { time.Sleep(time.Millisecond)
ants.Push(demoFunc)
} }
func TestNoPool(t *testing.T) {
var wg sync.WaitGroup
for i := 0; i < n; i++ {
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("pool capacity:%d", ants.Cap())
//t.Logf("running workers number:%d", ants.Running()) //t.Logf("running workers number:%d", ants.Running())
//t.Logf("free workers number:%d", ants.Free()) //t.Logf("free workers number:%d", ants.Free())
ants.Wait()
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
t.Logf("memory usage:%d", mem.TotalAlloc/1024) t.Logf("memory usage:%d", mem.TotalAlloc/1024)
@ -54,19 +79,3 @@ func TestDefaultPool(t *testing.T) {
// runtime.ReadMemStats(&mem) // 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)
}

21
pool.go
View File

@ -18,7 +18,6 @@ type Pool struct {
workerPool sync.Pool workerPool sync.Pool
destroy chan sig destroy chan sig
m sync.Mutex m sync.Mutex
wg sync.WaitGroup
} }
func NewPool(size int) *Pool { func NewPool(size int) *Pool {
@ -27,16 +26,30 @@ func NewPool(size int) *Pool {
freeSignal: make(chan sig, size), freeSignal: make(chan sig, size),
destroy: make(chan sig, runtime.GOMAXPROCS(-1)), destroy: make(chan sig, runtime.GOMAXPROCS(-1)),
} }
return p 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 { func (p *Pool) Push(task f) error {
if len(p.destroy) > 0 { if len(p.destroy) > 0 {
return nil return nil
} }
p.wg.Add(1)
w := p.getWorker() w := p.getWorker()
w.sendTask(task) w.sendTask(task)
return nil return nil
@ -54,10 +67,6 @@ func (p *Pool) Cap() int {
return int(atomic.LoadInt32(&p.capacity)) return int(atomic.LoadInt32(&p.capacity))
} }
func (p *Pool) Wait() {
p.wg.Wait()
}
func (p *Pool) Destroy() error { func (p *Pool) Destroy() error {
p.m.Lock() p.m.Lock()
defer p.m.Unlock() defer p.m.Unlock()

View File

@ -18,7 +18,6 @@ func (w *Worker) run() {
} }
f() f()
w.pool.putWorker(w) w.pool.putWorker(w)
w.pool.wg.Done()
} }
}() }()
} }