update go test

This commit is contained in:
Andy Pan 2018-05-19 15:22:14 +08:00
parent 5c30d8f881
commit 2f6f0de82b
4 changed files with 68 additions and 12 deletions

11
ants.go
View File

@ -10,10 +10,15 @@ func Push(task f) error {
return defaultPool.Push(task)
}
func Size() int {
return int(defaultPool.Running())
func Running() int {
return defaultPool.Running()
}
func Cap() int {
return int(defaultPool.Cap())
return defaultPool.Cap()
}
func Free() int {
return defaultPool.Free()
}

1
ants_benchmark_test.go Normal file
View File

@ -0,0 +1 @@
package ants

View File

@ -1,3 +1,53 @@
package ants
package ants_test
import "testing"
import (
"testing"
"github.com/panjf2000/ants"
"fmt"
"runtime"
)
var n = 100000
func demoFunc() {
for i := 0; i < 1000000; i++ {}
}
func TestDefaultPool(t *testing.T) {
for i := 0; i < n; i++ {
ants.Push(demoFunc)
}
t.Logf("pool capacity:%d", ants.Cap())
t.Logf("running workers number:%d", ants.Running())
t.Logf("free workers number:%d", ants.Free())
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
fmt.Println("memory usage:", mem.TotalAlloc/1024)
}
//func TestCustomPool(t *testing.T) {
// p := ants.NewPool(1000)
// for i := 0; i < n; i++ {
// p.Push(demoFunc)
// }
//
// t.Logf("pool capacity:%d", p.Cap())
// t.Logf("running workers number:%d", p.Running())
// t.Logf("free workers number:%d", p.Free())
//
// mem := runtime.MemStats{}
// runtime.ReadMemStats(&mem)
//
//}
func TestNoPool(t *testing.T) {
for i := 0; i < n; i++ {
go demoFunc()
}
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
fmt.Println("memory usage:", mem.TotalAlloc/1024)
}

14
pool.go
View File

@ -56,16 +56,16 @@ func (p *Pool) Push(task f) error {
p.tasks <- task
return nil
}
func (p *Pool) Running() int32 {
return atomic.LoadInt32(&p.running)
func (p *Pool) Running() int {
return int(atomic.LoadInt32(&p.running))
}
func (p *Pool) Free() int32 {
return atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running)
func (p *Pool) Free() int {
return int(atomic.LoadInt32(&p.capacity) - atomic.LoadInt32(&p.running))
}
func (p *Pool) Cap() int32 {
return atomic.LoadInt32(&p.capacity)
func (p *Pool) Cap() int {
return int(atomic.LoadInt32(&p.capacity))
}
func (p *Pool) Destroy() error {
@ -90,11 +90,11 @@ func (p *Pool) newWorker() *Worker {
exit: make(chan sig),
}
worker.run()
atomic.AddInt32(&p.running, 1)
return worker
}
func (p *Pool) getWorker() *Worker {
defer atomic.AddInt32(&p.running, 1)
var worker *Worker
if p.reachLimit() {
worker = <-p.workers