mirror of https://github.com/panjf2000/ants.git
test
This commit is contained in:
parent
c5e17c566b
commit
5b274e54b4
4
ants.go
4
ants.go
|
@ -19,7 +19,3 @@ func Cap() int {
|
|||
func Free() int {
|
||||
return defaultPool.Free()
|
||||
}
|
||||
|
||||
func Wait() {
|
||||
defaultPool.Wait()
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
63
ants_test.go
63
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)
|
||||
}
|
||||
|
|
21
pool.go
21
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()
|
||||
|
|
Loading…
Reference in New Issue