From 08a652b1dcb8771f9fbc1b6ae0cf54099e2d19a2 Mon Sep 17 00:00:00 2001 From: andy pan Date: Thu, 24 May 2018 13:35:57 +0800 Subject: [PATCH] go test optimization --- ants_benchmark_test.go | 100 +++++++++++++++++------------------------ ants_test.go | 66 +++++++++------------------ examples/main.go | 2 +- 3 files changed, 64 insertions(+), 104 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index f04d158..698c5d4 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -23,7 +23,6 @@ package ants_test import ( - "runtime" "sync" "testing" "time" @@ -33,68 +32,37 @@ 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 +const loop = 10 + +func demoFunc() { + time.Sleep(loop * time.Millisecond) +} func demoPoolFunc(args interface{}) error { - // m := args.(int) - // var n int - // for i := 0; i < m; i++ { - // n += i - // } - // return nil + //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 } -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:%d MB", mem.TotalAlloc/MiB) -} - -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:%d MB", mem.TotalAlloc/MiB) -} - func BenchmarkGoroutineWithFunc(b *testing.B) { for i := 0; i < b.N; i++ { var wg sync.WaitGroup - b.ResetTimer() for j := 0; j < RunTimes; j++ { wg.Add(1) go func() { @@ -104,9 +72,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) { } wg.Wait() } - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) + //b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) } func BenchmarkAntsPoolWithFunc(b *testing.B) { @@ -117,14 +83,32 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) { wg.Done() return nil }) - b.ResetTimer() for j := 0; j < RunTimes; j++ { wg.Add(1) p.Serve(loop) } wg.Wait() + //b.Logf("running goroutines: %d", p.Running()) } - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) + //b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) +} + +func BenchmarkGoroutine(b *testing.B) { + for i := 0; i < b.N; i++ { + for j := 0; j < RunTimes; j++ { + go demoFunc() + } + } + + //b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) +} + +func BenchmarkAntsPool(b *testing.B) { + for i := 0; i < b.N; i++ { + for j := 0; j < RunTimes; j++ { + ants.Push(demoFunc) + } + b.Logf("running goroutines: %d", ants.Running()) + } + //b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB) } diff --git a/ants_test.go b/ants_test.go index 74c95e9..451b5b3 100644 --- a/ants_test.go +++ b/ants_test.go @@ -24,7 +24,6 @@ package ants_test import ( "runtime" - "time" "sync" "testing" @@ -33,29 +32,6 @@ import ( var n = 10000000 -//func demoFunc() { -// var n int -// for i := 0; i < 1000000; i++ { -// n += i -// } -//} - -//func demoFunc() { -// var n int -// for i := 0; i < 10000; i++ { -// n += i -// } -// fmt.Printf("finish task with result:%d\n", n) -//} - -func demoFunc() { - 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++ { @@ -73,7 +49,7 @@ func TestDefaultPool(t *testing.T) { t.Logf("running workers number:%d", ants.Running()) mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/MiB) + t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) } func TestNoPool(t *testing.T) { @@ -89,30 +65,30 @@ func TestNoPool(t *testing.T) { wg.Wait() mem := runtime.MemStats{} runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/MiB) + t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) } -func TestAntsPoolWithFunc(t *testing.T) { - var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { - demoPoolFunc(i) - wg.Done() - return nil - }) - for i := 0; i < n; i++ { - wg.Add(1) - p.Serve(n) - } - wg.Wait() +// func TestAntsPoolWithFunc(t *testing.T) { +// var wg sync.WaitGroup +// p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { +// demoPoolFunc(i) +// wg.Done() +// return nil +// }) +// for i := 0; i < n; i++ { +// wg.Add(1) +// p.Serve(n) +// } +// wg.Wait() - //t.Logf("pool capacity:%d", ants.Cap()) - //t.Logf("free workers number:%d", ants.Free()) +// //t.Logf("pool capacity:%d", ants.Cap()) +// //t.Logf("free workers number:%d", ants.Free()) - t.Logf("running workers number:%d", p.Running()) - mem := runtime.MemStats{} - runtime.ReadMemStats(&mem) - t.Logf("memory usage:%d", mem.TotalAlloc/GiB) -} +// t.Logf("running workers number:%d", p.Running()) +// mem := runtime.MemStats{} +// runtime.ReadMemStats(&mem) +// t.Logf("memory usage:%d", mem.TotalAlloc/GiB) +// } // func TestNoPool(t *testing.T) { // var wg sync.WaitGroup diff --git a/examples/main.go b/examples/main.go index 9c5bb92..5f71174 100644 --- a/examples/main.go +++ b/examples/main.go @@ -58,7 +58,7 @@ func main() { // set 100 the size of goroutine pool var wg sync.WaitGroup - p, _ := ants.NewPoolWithFunc(100, func(i interface{}) error { + p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error { myFunc(i) wg.Done() return nil