update go test

This commit is contained in:
andy pan 2018-05-23 17:21:39 +08:00
parent 8e7ee16f0d
commit 2e3a9a650e
4 changed files with 60 additions and 59 deletions

View File

@ -71,7 +71,7 @@ func BenchmarkGoroutine(b *testing.B) {
} }
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) 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) { func BenchmarkAntsPool(b *testing.B) {
@ -88,7 +88,7 @@ func BenchmarkAntsPool(b *testing.B) {
} }
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) 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) { func BenchmarkGoroutineWithFunc(b *testing.B) {
@ -106,7 +106,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
} }
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) 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) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
@ -126,5 +126,5 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
} }
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB) b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
} }

View File

@ -24,6 +24,7 @@ package ants_test
import ( import (
"runtime" "runtime"
"time"
"sync" "sync"
"testing" "testing"
@ -48,48 +49,48 @@ var n = 10000000
//} //}
func demoFunc() { func demoFunc() {
//time.Sleep(time.Millisecond) time.Sleep(10 * time.Millisecond)
var n int // var n int
for i := 0; i < 1000000; i++ { // for i := 0; i < 1000000; i++ {
n += 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++ {
// wg.Add(1) wg.Add(1)
// ants.Push(func() { ants.Push(func() {
// demoFunc() demoFunc()
// wg.Done() wg.Done()
// }) })
// } }
// wg.Wait() 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 TestNoPool(t *testing.T) { //t.Logf("pool capacity:%d", ants.Cap())
// var wg sync.WaitGroup //t.Logf("free workers number:%d", ants.Free())
// for i := 0; i < n; i++ {
// wg.Add(1) t.Logf("running workers number:%d", ants.Running())
// go func() { mem := runtime.MemStats{}
// demoFunc() runtime.ReadMemStats(&mem)
// wg.Done() t.Logf("memory usage:%d", mem.TotalAlloc/MiB)
// }() }
// }
// func TestNoPool(t *testing.T) {
// wg.Wait() var wg sync.WaitGroup
// mem := runtime.MemStats{} for i := 0; i < n; i++ {
// runtime.ReadMemStats(&mem) wg.Add(1)
// t.Logf("memory usage:%d", mem.TotalAlloc/1024) 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) { func TestAntsPoolWithFunc(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
@ -113,21 +114,21 @@ func TestAntsPoolWithFunc(t *testing.T) {
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
for i := 0; i < n; i++ { // for i := 0; i < n; i++ {
wg.Add(1) // wg.Add(1)
go func() { // go func() {
demoPoolFunc(n) // demoPoolFunc(n)
wg.Done() // wg.Done()
}() // }()
} // }
wg.Wait() // wg.Wait()
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 TestCustomPool(t *testing.T) { //func TestCustomPool(t *testing.T) {
// p, _ := ants.NewPool(30000) // p, _ := ants.NewPool(30000)

View File

@ -42,7 +42,7 @@ type Pool struct {
// running is the number of the currently running goroutines. // running is the number of the currently running goroutines.
running int32 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. // workers which can be sent to work.
freeSignal chan sig freeSignal chan sig

View File

@ -40,7 +40,7 @@ type PoolWithFunc struct {
// running is the number of the currently running goroutines. // running is the number of the currently running goroutines.
running int32 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. // workers which can be sent to work.
freeSignal chan sig freeSignal chan sig