optimization for benchmark test

This commit is contained in:
Andy Pan 2018-10-03 20:55:39 +08:00
parent c10f80f7b7
commit 711dbdb7a2
2 changed files with 40 additions and 43 deletions

View File

@ -31,21 +31,9 @@ import (
) )
const ( const (
_ = 1 << (10 * iota) RunTimes = 10000000
KiB // 1024 benchParam = 10
MiB // 1048576 benchAntsSize = 100000
GiB // 1073741824
TiB // 1099511627776 (超过了int32的范围)
PiB // 1125899906842624
EiB // 1152921504606846976
ZiB // 1180591620717411303424 (超过了int64的范围)
YiB // 1208925819614629174706176
)
const (
RunTimes = 10000000
Param = 100
AntsSize = 1000
TestSize = 10000
) )
func demoFunc() error { func demoFunc() error {
@ -69,10 +57,10 @@ func demoPoolFunc(args interface{}) error {
func BenchmarkGoroutineWithFunc(b *testing.B) { func BenchmarkGoroutineWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
wg.Add(1)
go func() { go func() {
demoPoolFunc(Param) demoPoolFunc(benchParam)
wg.Done() wg.Done()
}() }()
} }
@ -82,14 +70,14 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
func BenchmarkSemaphoreWithFunc(b *testing.B) { func BenchmarkSemaphoreWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
sema := make(chan struct{}, AntsSize) sema := make(chan struct{}, benchAntsSize)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
wg.Add(RunTimes) wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
sema <- struct{}{} sema <- struct{}{}
go func() { go func() {
demoPoolFunc(Param) demoPoolFunc(benchParam)
<-sema <-sema
wg.Done() wg.Done()
}() }()
@ -100,63 +88,54 @@ func BenchmarkSemaphoreWithFunc(b *testing.B) {
func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(benchAntsSize, func(i interface{}) error {
demoPoolFunc(i) demoPoolFunc(i)
wg.Done() wg.Done()
return nil return nil
}) })
defer p.Release() defer p.Release()
b.ResetTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
wg.Add(1) p.Serve(benchParam)
p.Serve(Param)
} }
wg.Wait() wg.Wait()
//b.Logf("running goroutines: %d", p.Running()) //b.Logf("running goroutines: %d", p.Running())
} }
b.StopTimer()
} }
func BenchmarkGoroutine(b *testing.B) { func BenchmarkGoroutine(b *testing.B) {
var wg sync.WaitGroup
wg.Add(b.N * RunTimes)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
go func() { go demoPoolFunc(benchParam)
demoPoolFunc(Param)
wg.Done()
}()
} }
} }
wg.Wait()
} }
func BenchmarkSemaphore(b *testing.B) { func BenchmarkSemaphore(b *testing.B) {
var wg sync.WaitGroup sema := make(chan struct{}, benchAntsSize)
sema := make(chan struct{}, AntsSize)
wg.Add(RunTimes * b.N)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
sema <- struct{}{} sema <- struct{}{}
go func() { go func() {
demoPoolFunc(Param) demoPoolFunc(benchParam)
<-sema <-sema
wg.Done()
}() }()
} }
} }
wg.Wait()
} }
func BenchmarkAntsPool(b *testing.B) { func BenchmarkAntsPool(b *testing.B) {
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) p, _ := ants.NewPoolWithFunc(benchAntsSize, demoPoolFunc)
defer p.Release() defer p.Release()
b.ResetTimer() b.StartTimer()
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
p.Serve(Param) p.Serve(benchParam)
} }
} }
b.StopTimer()
} }

View File

@ -31,7 +31,25 @@ import (
"github.com/panjf2000/ants" "github.com/panjf2000/ants"
) )
var n = 100000 const (
_ = 1 << (10 * iota)
KiB // 1024
MiB // 1048576
GiB // 1073741824
TiB // 1099511627776 (超过了int32的范围)
PiB // 1125899906842624
EiB // 1152921504606846976
ZiB // 1180591620717411303424 (超过了int64的范围)
YiB // 1208925819614629174706176
)
const (
Param = 100
AntsSize = 1000
TestSize = 10000
n = 100000
)
var curMem uint64 var curMem uint64
func TestAntsPoolWithFunc(t *testing.T) { func TestAntsPoolWithFunc(t *testing.T) {
@ -51,7 +69,7 @@ func TestAntsPoolWithFunc(t *testing.T) {
t.Logf("pool with func, running workers number:%d", p.Running()) t.Logf("pool with func, running workers number:%d", p.Running())
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
curMem = mem.TotalAlloc / MiB - curMem curMem = mem.TotalAlloc/MiB - curMem
t.Logf("memory usage:%d MB", curMem) t.Logf("memory usage:%d MB", curMem)
} }
func TestNoPool(t *testing.T) { func TestNoPool(t *testing.T) {
@ -130,4 +148,4 @@ func TestCodeCov(t *testing.T) {
p.ReSize(TestSize) p.ReSize(TestSize)
p.ReSize(AntsSize) p.ReSize(AntsSize)
t.Logf("pool with func, after resize, capacity:%d, running:%d", p.Cap(), p.Running()) t.Logf("pool with func, after resize, capacity:%d, running:%d", p.Cap(), p.Running())
} }