diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 95e86d2..f04d158 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -71,7 +71,7 @@ func BenchmarkGoroutine(b *testing.B) { } mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) + b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) } func BenchmarkAntsPool(b *testing.B) { @@ -88,7 +88,7 @@ func BenchmarkAntsPool(b *testing.B) { } mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) + b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) } func BenchmarkGoroutineWithFunc(b *testing.B) { @@ -106,7 +106,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { } mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) + b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) } func BenchmarkAntsPoolWithFunc(b *testing.B) { @@ -126,5 +126,5 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { } mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) + b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) } diff --git a/ants_test.go b/ants_test.go index 7e95097..74c95e9 100644 --- a/ants_test.go +++ b/ants_test.go @@ -24,6 +24,7 @@ package ants_test import ( "runtime" + "time" "sync" "testing" @@ -48,48 +49,48 @@ var n = 10000000 //} func demoFunc() { - //time.Sleep(time.Millisecond) - var n int - for i := 0; i < 1000000; i++ { - n += i - } + time.Sleep(10 * time.Millisecond) + // var n int + // for i := 0; i < 1000000; i++ { + // n += i + // } } -//func TestDefaultPool(t *testing.T) { -// var wg sync.WaitGroup -// for i := 0; i < n; i++ { -// wg.Add(1) -// ants.Push(func() { -// demoFunc() -// wg.Done() -// }) -// } -// wg.Wait() -// -// //t.Logf("pool capacity:%d", ants.Cap()) -// //t.Logf("free workers number:%d", ants.Free()) -// -// t.Logf("running workers number:%d", ants.Running()) -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// t.Logf("memory usage:%d", mem.TotalAlloc/1024) -//} +func TestDefaultPool(t *testing.T) { + var wg sync.WaitGroup + for i := 0; i < n; i++ { + wg.Add(1) + ants.Push(func() { + demoFunc() + wg.Done() + }) + } + wg.Wait() -//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", mem.TotalAlloc/1024) -//} + //t.Logf("pool capacity:%d", ants.Cap()) + //t.Logf("free workers number:%d", ants.Free()) + + t.Logf("running workers number:%d", ants.Running()) + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + t.Logf("memory usage:%d", mem.TotalAlloc/MiB) +} + +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", mem.TotalAlloc/MiB) +} func TestAntsPoolWithFunc(t *testing.T) { var wg sync.WaitGroup @@ -113,21 +114,21 @@ func TestAntsPoolWithFunc(t *testing.T) { t.Logf("memory usage:%d", mem.TotalAlloc/GiB) } -func TestNoPool(t *testing.T) { - var wg sync.WaitGroup - for i := 0; i < n; i++ { - wg.Add(1) - go func() { - demoPoolFunc(n) - wg.Done() - }() - } +// func TestNoPool(t *testing.T) { +// var wg sync.WaitGroup +// for i := 0; i < n; i++ { +// wg.Add(1) +// go func() { +// demoPoolFunc(n) +// wg.Done() +// }() +// } - wg.Wait() - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/GiB) -} +// wg.Wait() +// mem := runtime.MemStats{} +// runtime.ReadMemStats(&mem) +// t.Logf("memory usage:%d", mem.TotalAlloc/GiB) +// } //func TestCustomPool(t *testing.T) { // p, _ := ants.NewPool(30000) diff --git a/pool.go b/pool.go index 58b2dd2..113027f 100644 --- a/pool.go +++ b/pool.go @@ -42,7 +42,7 @@ type Pool struct { // running is the number of the currently running goroutines. running int32 - // signal is used to notice pool there are available + // freeSignal is used to notice pool there are available // workers which can be sent to work. freeSignal chan sig diff --git a/pool_func.go b/pool_func.go index 7223bfa..0e888e5 100644 --- a/pool_func.go +++ b/pool_func.go @@ -40,7 +40,7 @@ type PoolWithFunc struct { // running is the number of the currently running goroutines. running int32 - // signal is used to notice pool there are available + // freeSignal is used to notice pool there are available // workers which can be sent to work. freeSignal chan sig