mirror of https://github.com/panjf2000/ants.git
update go test and benchmarks test
This commit is contained in:
parent
6e53c23ace
commit
63ebcb38dd
7
ants.go
7
ants.go
|
@ -22,11 +22,14 @@
|
||||||
|
|
||||||
package ants
|
package ants
|
||||||
|
|
||||||
import "errors"
|
import (
|
||||||
|
"errors"
|
||||||
|
"math"
|
||||||
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
// DefaultPoolSize is the default capacity for a default goroutine pool
|
// DefaultPoolSize is the default capacity for a default goroutine pool
|
||||||
DefaultPoolSize = 50000
|
DefaultPoolSize = math.MaxInt32
|
||||||
|
|
||||||
// DefaultCleanIntervalTime is the interval time to clean up goroutines
|
// DefaultCleanIntervalTime is the interval time to clean up goroutines
|
||||||
DefaultCleanIntervalTime = 30
|
DefaultCleanIntervalTime = 30
|
||||||
|
|
|
@ -36,7 +36,7 @@ func BenchmarkGoroutine(b *testing.B) {
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
forSleep()
|
demoFunc()
|
||||||
wg.Done()
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -50,7 +50,7 @@ func BenchmarkPoolGoroutine(b *testing.B) {
|
||||||
for j := 0; j < RunTimes; j++ {
|
for j := 0; j < RunTimes; j++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ants.Push(func() {
|
ants.Push(func() {
|
||||||
forSleep()
|
demoFunc()
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
67
ants_test.go
67
ants_test.go
|
@ -27,10 +27,9 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var n = 1000000
|
var n = 10000000
|
||||||
|
|
||||||
//func demoFunc() {
|
//func demoFunc() {
|
||||||
// var n int
|
// var n int
|
||||||
|
@ -47,25 +46,12 @@ var n = 1000000
|
||||||
// fmt.Printf("finish task with result:%d\n", n)
|
// fmt.Printf("finish task with result:%d\n", n)
|
||||||
//}
|
//}
|
||||||
|
|
||||||
func forSleep() {
|
func demoFunc() {
|
||||||
time.Sleep(time.Millisecond)
|
//time.Sleep(time.Millisecond)
|
||||||
|
var n int
|
||||||
|
for i := 0; i < 1000000; i++ {
|
||||||
|
n += i
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoPool(t *testing.T) {
|
|
||||||
var wg sync.WaitGroup
|
|
||||||
for i := 0; i < n; i++ {
|
|
||||||
wg.Add(1)
|
|
||||||
go func() {
|
|
||||||
forSleep()
|
|
||||||
//demoFunc()
|
|
||||||
wg.Done()
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
mem := runtime.MemStats{}
|
|
||||||
runtime.ReadMemStats(&mem)
|
|
||||||
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDefaultPool(t *testing.T) {
|
func TestDefaultPool(t *testing.T) {
|
||||||
|
@ -73,8 +59,7 @@ func TestDefaultPool(t *testing.T) {
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ants.Push(func() {
|
ants.Push(func() {
|
||||||
forSleep()
|
demoFunc()
|
||||||
//demoFunc()
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -89,24 +74,40 @@ func TestDefaultPool(t *testing.T) {
|
||||||
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCustomPool(t *testing.T) {
|
func TestNoPool(t *testing.T) {
|
||||||
p, _ := ants.NewPool(30000)
|
|
||||||
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)
|
||||||
p.Push(func() {
|
go func() {
|
||||||
forSleep()
|
demoFunc()
|
||||||
//demoFunc()
|
|
||||||
wg.Done()
|
wg.Done()
|
||||||
})
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
|
|
||||||
//t.Logf("pool capacity:%d", p.Cap())
|
|
||||||
//t.Logf("free workers number:%d", p.Free())
|
|
||||||
|
|
||||||
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/1024)
|
t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//func TestCustomPool(t *testing.T) {
|
||||||
|
// p, _ := ants.NewPool(30000)
|
||||||
|
// var wg sync.WaitGroup
|
||||||
|
// for i := 0; i < n; i++ {
|
||||||
|
// wg.Add(1)
|
||||||
|
// p.Push(func() {
|
||||||
|
// demoFunc()
|
||||||
|
// //demoFunc()
|
||||||
|
// wg.Done()
|
||||||
|
// })
|
||||||
|
// }
|
||||||
|
// wg.Wait()
|
||||||
|
//
|
||||||
|
// //t.Logf("pool capacity:%d", p.Cap())
|
||||||
|
// //t.Logf("free workers number:%d", p.Free())
|
||||||
|
//
|
||||||
|
// t.Logf("running workers number:%d", p.Running())
|
||||||
|
// mem := runtime.MemStats{}
|
||||||
|
// runtime.ReadMemStats(&mem)
|
||||||
|
// t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
||||||
|
//}
|
||||||
|
|
Loading…
Reference in New Issue