update go test and benchmarks test

This commit is contained in:
Andy Pan 2018-05-22 11:17:19 +08:00
parent 6e53c23ace
commit 63ebcb38dd
3 changed files with 41 additions and 37 deletions

View File

@ -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

View File

@ -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()
})
}

View File

@ -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)
//}