Merge branch 'master' into develop

This commit is contained in:
Andy Pan 2018-10-03 21:03:47 +08:00
commit 92acf74bb7
2 changed files with 66 additions and 28 deletions

View File

@ -30,22 +30,10 @@ import (
"github.com/panjf2000/ants"
)
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
Param = 100
AntsSize = 1000
TestSize = 10000
benchParam = 10
benchAntsSize = 100000
)
func demoFunc() error {
@ -68,12 +56,29 @@ func demoPoolFunc(args interface{}) error {
func BenchmarkGoroutineWithFunc(b *testing.B) {
var wg sync.WaitGroup
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
go func() {
demoPoolFunc(benchParam)
wg.Done()
}()
}
wg.Wait()
}
}
func BenchmarkSemaphoreWithFunc(b *testing.B) {
var wg sync.WaitGroup
sema := make(chan struct{}, benchAntsSize)
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
wg.Add(1)
sema <- struct{}{}
go func() {
demoPoolFunc(Param)
demoPoolFunc(benchParam)
<-sema
wg.Done()
}()
}
@ -83,39 +88,54 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
func BenchmarkAntsPoolWithFunc(b *testing.B) {
var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error {
p, _ := ants.NewPoolWithFunc(benchAntsSize, func(i interface{}) error {
demoPoolFunc(i)
wg.Done()
return nil
})
defer p.Release()
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
wg.Add(RunTimes)
for j := 0; j < RunTimes; j++ {
wg.Add(1)
p.Serve(Param)
p.Serve(benchParam)
}
wg.Wait()
//b.Logf("running goroutines: %d", p.Running())
}
b.StopTimer()
}
func BenchmarkGoroutine(b *testing.B) {
for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ {
go demoPoolFunc(Param)
go demoPoolFunc(benchParam)
}
}
}
func BenchmarkSemaphore(b *testing.B) {
sema := make(chan struct{}, benchAntsSize)
for i := 0; i < b.N; i++ {
for j := 0; j < RunTimes; j++ {
sema <- struct{}{}
go func() {
demoPoolFunc(benchParam)
<-sema
}()
}
}
}
func BenchmarkAntsPool(b *testing.B) {
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
p, _ := ants.NewPoolWithFunc(benchAntsSize, demoPoolFunc)
defer p.Release()
b.ResetTimer()
b.StartTimer()
for i := 0; i < b.N; i++ {
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"
)
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
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())
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
curMem = mem.TotalAlloc / MiB - curMem
curMem = mem.TotalAlloc/MiB - curMem
t.Logf("memory usage:%d MB", curMem)
}
func TestNoPool(t *testing.T) {