forked from mirror/ants
Merge branch 'develop'
This commit is contained in:
commit
6c322c6373
|
@ -178,7 +178,7 @@ In that benchmark-picture, the first and second benchmarks performed test with 1
|
||||||
|
|
||||||
![](ants_bench_1000w.png)
|
![](ants_bench_1000w.png)
|
||||||
|
|
||||||
There was only the test of `ants` Pool because my computer was crash when it reached 10M goroutines.
|
There was only the test of `ants` Pool because my computer was crash when it reached 10M goroutines without pool.
|
||||||
|
|
||||||
**As you can see, `ants` can up to 2x~6x faster than goroutines without pool and the memory consumption is reduced by 10 to 20 times.**
|
**As you can see, `ants` can up to 2x~6x faster than goroutines without pool and the memory consumption is reduced by 10 to 20 times.**
|
||||||
|
|
||||||
|
|
26
ants.go
26
ants.go
|
@ -28,39 +28,49 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultPoolSize is the default capacity for a default goroutine pool
|
// DefaultAntsPoolSize is the default capacity for a default goroutine pool
|
||||||
DefaultPoolSize = math.MaxInt32
|
DefaultAntsPoolSize = math.MaxInt32
|
||||||
|
|
||||||
// DefaultCleanIntervalTime is the interval time to clean up goroutines
|
// DefaultCleanIntervalTime is the interval time to clean up goroutines
|
||||||
DefaultCleanIntervalTime = 10
|
DefaultCleanIntervalTime = 10
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
defaultAntsPool *Pool
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
// Init a instance pool when importing ants
|
// Init a instance pool when importing ants
|
||||||
var defaultPool, _ = NewPool(DefaultPoolSize)
|
func init() {
|
||||||
|
defaultAntsPool, err = NewPool(DefaultAntsPoolSize)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Submit submit a task to pool
|
// Submit submit a task to pool
|
||||||
func Submit(task f) error {
|
func Submit(task f) error {
|
||||||
return defaultPool.Submit(task)
|
return defaultAntsPool.Submit(task)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Running returns the number of the currently running goroutines
|
// Running returns the number of the currently running goroutines
|
||||||
func Running() int {
|
func Running() int {
|
||||||
return defaultPool.Running()
|
return defaultAntsPool.Running()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cap returns the capacity of this default pool
|
// Cap returns the capacity of this default pool
|
||||||
func Cap() int {
|
func Cap() int {
|
||||||
return defaultPool.Cap()
|
return defaultAntsPool.Cap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Free returns the available goroutines to work
|
// Free returns the available goroutines to work
|
||||||
func Free() int {
|
func Free() int {
|
||||||
return defaultPool.Free()
|
return defaultAntsPool.Free()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release Closed the default pool
|
// Release Closed the default pool
|
||||||
func Release() {
|
func Release() {
|
||||||
defaultPool.Release()
|
defaultAntsPool.Release()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errors for the Ants API
|
// Errors for the Ants API
|
||||||
|
|
39
ants_test.go
39
ants_test.go
|
@ -32,6 +32,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
var n = 100000
|
var n = 100000
|
||||||
|
var curMem uint64
|
||||||
|
|
||||||
func TestAntsPoolWithFunc(t *testing.T) {
|
func TestAntsPoolWithFunc(t *testing.T) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -50,7 +51,24 @@ func TestAntsPoolWithFunc(t *testing.T) {
|
||||||
t.Logf("pool with func, running workers number:%d", p.Running())
|
t.Logf("pool with func, running workers number:%d", p.Running())
|
||||||
mem := runtime.MemStats{}
|
mem := runtime.MemStats{}
|
||||||
runtime.ReadMemStats(&mem)
|
runtime.ReadMemStats(&mem)
|
||||||
t.Logf("memory usage:%d", mem.TotalAlloc/GiB)
|
curMem = mem.TotalAlloc / MiB - curMem
|
||||||
|
t.Logf("memory usage:%d", curMem)
|
||||||
|
}
|
||||||
|
func TestNoPool(t *testing.T) {
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
for i := 0; i < n; i++ {
|
||||||
|
wg.Add(1)
|
||||||
|
go func() {
|
||||||
|
demoFunc()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
mem := runtime.MemStats{}
|
||||||
|
runtime.ReadMemStats(&mem)
|
||||||
|
curMem = mem.TotalAlloc/MiB - curMem
|
||||||
|
t.Logf("memory usage:%d MB", curMem)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAntsPool(t *testing.T) {
|
func TestAntsPool(t *testing.T) {
|
||||||
|
@ -72,23 +90,8 @@ func TestAntsPool(t *testing.T) {
|
||||||
|
|
||||||
mem := runtime.MemStats{}
|
mem := runtime.MemStats{}
|
||||||
runtime.ReadMemStats(&mem)
|
runtime.ReadMemStats(&mem)
|
||||||
t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB)
|
curMem = mem.TotalAlloc/MiB - curMem
|
||||||
}
|
t.Logf("memory usage:%d MB", curMem)
|
||||||
|
|
||||||
func TestNoPool(t *testing.T) {
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
demoFunc()
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
mem := runtime.MemStats{}
|
|
||||||
runtime.ReadMemStats(&mem)
|
|
||||||
t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCodeCov(t *testing.T) {
|
func TestCodeCov(t *testing.T) {
|
||||||
|
|
Loading…
Reference in New Issue