From 63ebcb38ddc4d1480e38cdde694d3c291f22c26f Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Tue, 22 May 2018 11:17:19 +0800 Subject: [PATCH] update go test and benchmarks test --- ants.go | 7 +++-- ants_benchmark_test.go | 4 +-- ants_test.go | 67 +++++++++++++++++++++--------------------- 3 files changed, 41 insertions(+), 37 deletions(-) diff --git a/ants.go b/ants.go index af76b67..2f27f3b 100644 --- a/ants.go +++ b/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 diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 0358dfc..188a926 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -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() }) } diff --git a/ants_test.go b/ants_test.go index d1020ab..ff05806 100644 --- a/ants_test.go +++ b/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 TestNoPool(t *testing.T) { - var wg sync.WaitGroup - for i := 0; i < n; i++ { - wg.Add(1) - go func() { - forSleep() - //demoFunc() - wg.Done() - }() +func demoFunc() { + //time.Sleep(time.Millisecond) + var n int + for i := 0; i < 1000000; i++ { + n += i } - - 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) +//}