go test optimization

This commit is contained in:
andy pan 2018-05-24 13:35:57 +08:00
parent 2e3a9a650e
commit 08a652b1dc
3 changed files with 64 additions and 104 deletions

View File

@ -23,7 +23,6 @@
package ants_test package ants_test
import ( import (
"runtime"
"sync" "sync"
"testing" "testing"
"time" "time"
@ -43,7 +42,11 @@ const (
YiB // 1208925819614629174706176 YiB // 1208925819614629174706176
) )
const RunTimes = 10000000 const RunTimes = 10000000
const loop = 5 const loop = 10
func demoFunc() {
time.Sleep(loop * time.Millisecond)
}
func demoPoolFunc(args interface{}) error { func demoPoolFunc(args interface{}) error {
//m := args.(int) //m := args.(int)
@ -57,44 +60,9 @@ func demoPoolFunc(args interface{}) error {
return nil 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) { func BenchmarkGoroutineWithFunc(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var wg sync.WaitGroup var wg sync.WaitGroup
b.ResetTimer()
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
wg.Add(1) wg.Add(1)
go func() { go func() {
@ -104,9 +72,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
} }
wg.Wait() wg.Wait()
} }
mem := runtime.MemStats{} //b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
runtime.ReadMemStats(&mem)
b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
} }
func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
@ -117,14 +83,32 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
wg.Done() wg.Done()
return nil return nil
}) })
b.ResetTimer()
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
wg.Add(1) wg.Add(1)
p.Serve(loop) p.Serve(loop)
} }
wg.Wait() wg.Wait()
//b.Logf("running goroutines: %d", p.Running())
} }
mem := runtime.MemStats{} //b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
runtime.ReadMemStats(&mem) }
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)
} }

View File

@ -24,7 +24,6 @@ package ants_test
import ( import (
"runtime" "runtime"
"time"
"sync" "sync"
"testing" "testing"
@ -33,29 +32,6 @@ import (
var n = 10000000 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) { func TestDefaultPool(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -73,7 +49,7 @@ func TestDefaultPool(t *testing.T) {
t.Logf("running workers number:%d", ants.Running()) t.Logf("running workers number:%d", ants.Running())
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) 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) { func TestNoPool(t *testing.T) {
@ -89,30 +65,30 @@ func TestNoPool(t *testing.T) {
wg.Wait() wg.Wait()
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) 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) { // func TestAntsPoolWithFunc(t *testing.T) {
var wg sync.WaitGroup // var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { // p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
demoPoolFunc(i) // demoPoolFunc(i)
wg.Done() // wg.Done()
return nil // return nil
}) // })
for i := 0; i < n; i++ { // for i := 0; i < n; i++ {
wg.Add(1) // wg.Add(1)
p.Serve(n) // p.Serve(n)
} // }
wg.Wait() // wg.Wait()
//t.Logf("pool capacity:%d", ants.Cap()) // //t.Logf("pool capacity:%d", ants.Cap())
//t.Logf("free workers number:%d", ants.Free()) // //t.Logf("free workers number:%d", ants.Free())
t.Logf("running workers number:%d", p.Running()) // t.Logf("running workers number:%d", p.Running())
mem := runtime.MemStats{} // mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) // runtime.ReadMemStats(&mem)
t.Logf("memory usage:%d", mem.TotalAlloc/GiB) // t.Logf("memory usage:%d", mem.TotalAlloc/GiB)
} // }
// func TestNoPool(t *testing.T) { // func TestNoPool(t *testing.T) {
// var wg sync.WaitGroup // var wg sync.WaitGroup

View File

@ -58,7 +58,7 @@ func main() {
// set 100 the size of goroutine pool // set 100 the size of goroutine pool
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(100, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
myFunc(i) myFunc(i)
wg.Done() wg.Done()
return nil return nil