From 19e41472a9662e721971df8c23741621c8ae1927 Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 23 May 2018 11:05:23 +0800 Subject: [PATCH 1/4] update - --- ants_benchmark_test.go | 39 ++++++++++++++++++++------ examples/main.go | 62 +++++++++++++++++++++++------------------- 2 files changed, 65 insertions(+), 36 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 6485b61..2ed592a 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -23,21 +23,37 @@ package ants_test import ( + "runtime" "sync" "testing" + "time" "github.com/panjf2000/ants" ) -const RunTimes = 1000000 -const loop = 1000000 +const ( + _ = 1 << (10 * iota) + KiB // 1024 + MiB // 1048576 + GiB // 1073741824 + TiB // 1099511627776 (超过了int32的范围) + PiB // 1125899906842624 + EiB // 1152921504606846976 + ZiB // 1180591620717411303424 (超过了int64的范围) + YiB // 1208925819614629174706176 +) +const RunTimes = 10000000 +const loop = 5 func demoPoolFunc(args interface{}) error { - m := args.(int) - var n int - for i := 0; i < m; i++ { - n += i - } + // m := args.(int) + // var n int + // for i := 0; i < m; i++ { + // n += i + // } + // return nil + n := args.(int) + time.Sleep(time.Duration(n) * time.Millisecond) return nil } @@ -53,6 +69,9 @@ func BenchmarkGoroutine(b *testing.B) { }() } wg.Wait() + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) } } @@ -73,7 +92,7 @@ func BenchmarkGoroutine(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(100000, func(i interface{}) error { + p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { demoPoolFunc(i) wg.Done() return nil @@ -84,5 +103,9 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { p.Serve(loop) } wg.Wait() + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + b.Logf("GB:%d", GiB) + b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) } } diff --git a/examples/main.go b/examples/main.go index cd8c2eb..9c5bb92 100644 --- a/examples/main.go +++ b/examples/main.go @@ -24,44 +24,50 @@ package main import ( "fmt" - "github.com/panjf2000/ants" "sync" + + "github.com/panjf2000/ants" ) -func myFunc() { - fmt.Println("Hello World!") +var str = "Hello World!" + +func myFunc(i interface{}) error { + s := i.(string) + fmt.Println(s) + return nil } +// func main() { +// runTimes := 10000 +// var wg sync.WaitGroup +// // submit all your tasks to ants pool +// for i := 0; i < runTimes; i++ { +// wg.Add(1) +// ants.Push(func() { +// myFunc() +// wg.Done() +// }) +// } +// wg.Wait() +// fmt.Println("finish all tasks!") +// } + func main() { - runTimes := 10000 + runTimes := 1000 + + // set 100 the size of goroutine pool + var wg sync.WaitGroup - // submit all your tasks to ants pool + p, _ := ants.NewPoolWithFunc(100, func(i interface{}) error { + myFunc(i) + wg.Done() + return nil + }) + // submit for i := 0; i < runTimes; i++ { wg.Add(1) - ants.Push(func() { - myFunc() - wg.Done() - }) + p.Serve(str) } wg.Wait() fmt.Println("finish all tasks!") } - -//func main() { -// runTimes := 10000 -// -// // set 100 the size of goroutine pool -// p, _ := ants.NewPool(100) -// -// var wg sync.WaitGroup -// // submit -// for i := 0; i < runTimes; i++ { -// wg.Add(1) -// p.Push(func() { -// myFunc() -// wg.Done() -// }) -// } -// wg.Wait() -// fmt.Println("finish all tasks!") -//} From 86325a5f3e71c632dcafc8e4ace4072cd3e3434d Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 23 May 2018 11:17:00 +0800 Subject: [PATCH 2/4] update - --- ants_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ants_test.go b/ants_test.go index 79a3a57..7e95097 100644 --- a/ants_test.go +++ b/ants_test.go @@ -30,7 +30,7 @@ import ( "github.com/panjf2000/ants" ) -var n = 100000 +var n = 10000000 //func demoFunc() { // var n int @@ -110,7 +110,7 @@ func TestAntsPoolWithFunc(t *testing.T) { t.Logf("running workers number:%d", p.Running()) mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/1024) + t.Logf("memory usage:%d", mem.TotalAlloc/GiB) } func TestNoPool(t *testing.T) { @@ -126,7 +126,7 @@ func TestNoPool(t *testing.T) { wg.Wait() mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/1024) + t.Logf("memory usage:%d", mem.TotalAlloc/GiB) } //func TestCustomPool(t *testing.T) { From 8e7ee16f0d1860aae82635613dc38fe017441318 Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 23 May 2018 16:43:05 +0800 Subject: [PATCH 3/4] update benchmarks --- ants_benchmark_test.go | 77 ++++++++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 29 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 2ed592a..95e86d2 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -33,14 +33,14 @@ import ( const ( _ = 1 << (10 * iota) - KiB // 1024 - MiB // 1048576 - GiB // 1073741824 - TiB // 1099511627776 (超过了int32的范围) - PiB // 1125899906842624 - EiB // 1152921504606846976 - ZiB // 1180591620717411303424 (超过了int64的范围) - YiB // 1208925819614629174706176 + KiB // 1024 + MiB // 1048576 + GiB // 1073741824 + TiB // 1099511627776 (超过了int32的范围) + PiB // 1125899906842624 + EiB // 1152921504606846976 + ZiB // 1180591620717411303424 (超过了int64的范围) + YiB // 1208925819614629174706176 ) const RunTimes = 10000000 const loop = 5 @@ -58,6 +58,40 @@ func demoPoolFunc(args interface{}) error { } func BenchmarkGoroutine(b *testing.B) { + for i := 0; i < b.N; i++ { + var wg sync.WaitGroup + for j := 0; j < RunTimes; j++ { + wg.Add(1) + go func() { + demoFunc() + wg.Done() + }() + } + wg.Wait() + } + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) +} + +func BenchmarkAntsPool(b *testing.B) { + for i := 0; i < b.N; i++ { + var wg sync.WaitGroup + for j := 0; j < RunTimes; j++ { + wg.Add(1) + ants.Push(func() { + demoFunc() + wg.Done() + }) + } + wg.Wait() + } + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) +} + +func BenchmarkGoroutineWithFunc(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup b.ResetTimer() @@ -69,26 +103,12 @@ func BenchmarkGoroutine(b *testing.B) { }() } wg.Wait() - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) } + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) } -//func BenchmarkAntsPool(b *testing.B) { -// for i := 0; i < b.N; i++ { -// var wg sync.WaitGroup -// for j := 0; j < RunTimes; j++ { -// wg.Add(1) -// ants.Push(func() { -// demoFunc() -// wg.Done() -// }) -// } -// wg.Wait() -// } -//} - func BenchmarkAntsPoolWithFunc(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup @@ -103,9 +123,8 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { p.Serve(loop) } wg.Wait() - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - b.Logf("GB:%d", GiB) - b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) } + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) } From 2e3a9a650eab9b60082620bd4e03f51cec08b62c Mon Sep 17 00:00:00 2001 From: andy pan Date: Wed, 23 May 2018 17:21:39 +0800 Subject: [PATCH 4/4] update go test --- ants_benchmark_test.go | 8 +-- ants_test.go | 107 +++++++++++++++++++++-------------------- pool.go | 2 +- pool_func.go | 2 +- 4 files changed, 60 insertions(+), 59 deletions(-) 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