benchmarks updated

This commit is contained in:
andy pan 2018-05-22 18:00:15 +08:00
parent b2ab15fa96
commit d606fb79f6
3 changed files with 28 additions and 21 deletions

View File

@ -23,9 +23,10 @@
package ants_test package ants_test
import ( import (
"github.com/panjf2000/ants"
"sync" "sync"
"testing" "testing"
"github.com/panjf2000/ants"
) )
const RunTimes = 1000000 const RunTimes = 1000000
@ -53,24 +54,29 @@ func BenchmarkGoroutine(b *testing.B) {
} }
} }
//func BenchmarkAntsPool(b *testing.B) { 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()
// }
//}
func BenchmarkAntsPoolWithFunc(b *testing.B) {
p, _ := ants.NewPoolWithFunc(100000, demoPoolFunc)
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
var wg sync.WaitGroup var wg sync.WaitGroup
for j := 0; j < RunTimes; j++ {
wg.Add(1)
ants.Push(func() {
demoFunc()
wg.Done()
})
}
wg.Wait()
}
}
func BenchmarkAntsPoolWithFunc(b *testing.B) {
for i := 0; i < b.N; i++ {
var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(100000, func(i interface{}) error {
demoPoolFunc(i)
wg.Done()
return nil
})
b.ResetTimer()
for j := 0; j < RunTimes; j++ { for j := 0; j < RunTimes; j++ {
wg.Add(1) wg.Add(1)
p.Serve(RunTimes) p.Serve(RunTimes)

View File

@ -23,13 +23,14 @@
package ants_test package ants_test
import ( import (
"github.com/panjf2000/ants"
"runtime" "runtime"
"sync" "sync"
"testing" "testing"
"github.com/panjf2000/ants"
) )
var n = 1000000 var n = 100000
//func demoFunc() { //func demoFunc() {
// var n int // var n int
@ -92,7 +93,7 @@ func demoFunc() {
func TestAntsPoolWithFunc(t *testing.T) { func TestAntsPoolWithFunc(t *testing.T) {
var wg sync.WaitGroup var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(100000, func(i interface{}) error { p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
demoPoolFunc(i) demoPoolFunc(i)
wg.Done() wg.Done()
return nil return nil

View File

@ -97,7 +97,7 @@ func (p *PoolWithFunc) scanAndClean() {
}() }()
} }
// Push submit a task to pool // Serve submit a task to pool
func (p *PoolWithFunc) Serve(args interface{}) error { func (p *PoolWithFunc) Serve(args interface{}) error {
if atomic.LoadInt32(&p.closed) == 1 { if atomic.LoadInt32(&p.closed) == 1 {
return ErrPoolClosed return ErrPoolClosed