forked from mirror/ants
update go test and benchmarks test
This commit is contained in:
parent
6e53c23ace
commit
63ebcb38dd
7
ants.go
7
ants.go
|
@ -22,11 +22,14 @@
|
|||
|
||||
package ants
|
||||
|
||||
import "errors"
|
||||
import (
|
||||
"errors"
|
||||
"math"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultPoolSize is the default capacity for a default goroutine pool
|
||||
DefaultPoolSize = 50000
|
||||
DefaultPoolSize = math.MaxInt32
|
||||
|
||||
// DefaultCleanIntervalTime is the interval time to clean up goroutines
|
||||
DefaultCleanIntervalTime = 30
|
||||
|
|
|
@ -36,7 +36,7 @@ func BenchmarkGoroutine(b *testing.B) {
|
|||
for j := 0; j < RunTimes; j++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
forSleep()
|
||||
demoFunc()
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ func BenchmarkPoolGoroutine(b *testing.B) {
|
|||
for j := 0; j < RunTimes; j++ {
|
||||
wg.Add(1)
|
||||
ants.Push(func() {
|
||||
forSleep()
|
||||
demoFunc()
|
||||
wg.Done()
|
||||
})
|
||||
}
|
||||
|
|
67
ants_test.go
67
ants_test.go
|
@ -27,10 +27,9 @@ import (
|
|||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var n = 1000000
|
||||
var n = 10000000
|
||||
|
||||
//func demoFunc() {
|
||||
// var n int
|
||||
|
@ -47,25 +46,12 @@ var n = 1000000
|
|||
// fmt.Printf("finish task with result:%d\n", n)
|
||||
//}
|
||||
|
||||
func forSleep() {
|
||||
time.Sleep(time.Millisecond)
|
||||
func demoFunc() {
|
||||
//time.Sleep(time.Millisecond)
|
||||
var n int
|
||||
for i := 0; i < 1000000; i++ {
|
||||
n += i
|
||||
}
|
||||
|
||||
func TestNoPool(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
forSleep()
|
||||
//demoFunc()
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||
}
|
||||
|
||||
func TestDefaultPool(t *testing.T) {
|
||||
|
@ -73,8 +59,7 @@ func TestDefaultPool(t *testing.T) {
|
|||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
ants.Push(func() {
|
||||
forSleep()
|
||||
//demoFunc()
|
||||
demoFunc()
|
||||
wg.Done()
|
||||
})
|
||||
}
|
||||
|
@ -89,24 +74,40 @@ func TestDefaultPool(t *testing.T) {
|
|||
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||
}
|
||||
|
||||
func TestCustomPool(t *testing.T) {
|
||||
p, _ := ants.NewPool(30000)
|
||||
func TestNoPool(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
p.Push(func() {
|
||||
forSleep()
|
||||
//demoFunc()
|
||||
go func() {
|
||||
demoFunc()
|
||||
wg.Done()
|
||||
})
|
||||
}()
|
||||
}
|
||||
|
||||
wg.Wait()
|
||||
|
||||
//t.Logf("pool capacity:%d", p.Cap())
|
||||
//t.Logf("free workers number:%d", p.Free())
|
||||
|
||||
t.Logf("running workers number:%d", p.Running())
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||
}
|
||||
|
||||
//func TestCustomPool(t *testing.T) {
|
||||
// p, _ := ants.NewPool(30000)
|
||||
// var wg sync.WaitGroup
|
||||
// for i := 0; i < n; i++ {
|
||||
// wg.Add(1)
|
||||
// p.Push(func() {
|
||||
// demoFunc()
|
||||
// //demoFunc()
|
||||
// wg.Done()
|
||||
// })
|
||||
// }
|
||||
// wg.Wait()
|
||||
//
|
||||
// //t.Logf("pool capacity:%d", p.Cap())
|
||||
// //t.Logf("free workers number:%d", p.Free())
|
||||
//
|
||||
// t.Logf("running workers number:%d", p.Running())
|
||||
// mem := runtime.MemStats{}
|
||||
// runtime.ReadMemStats(&mem)
|
||||
// t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||
//}
|
||||
|
|
Loading…
Reference in New Issue