mirror of https://github.com/panjf2000/ants.git
update go test
This commit is contained in:
parent
5c30d8f881
commit
2f6f0de82b
11
ants.go
11
ants.go
|
@ -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()
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
package ants
|
54
ants_test.go
54
ants_test.go
|
@ -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
14
pool.go
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue