forked from mirror/ants
go test optimization
This commit is contained in:
parent
2e3a9a650e
commit
08a652b1dc
|
@ -23,7 +23,6 @@
|
|||
package ants_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
@ -33,68 +32,37 @@ import (
|
|||
|
||||
const (
|
||||
_ = 1 << (10 * iota)
|
||||
KiB // 1024
|
||||
MiB // 1048576
|
||||
GiB // 1073741824
|
||||
TiB // 1099511627776 (超过了int32的范围)
|
||||
PiB // 1125899906842624
|
||||
EiB // 1152921504606846976
|
||||
ZiB // 1180591620717411303424 (超过了int64的范围)
|
||||
YiB // 1208925819614629174706176
|
||||
KiB // 1024
|
||||
MiB // 1048576
|
||||
GiB // 1073741824
|
||||
TiB // 1099511627776 (超过了int32的范围)
|
||||
PiB // 1125899906842624
|
||||
EiB // 1152921504606846976
|
||||
ZiB // 1180591620717411303424 (超过了int64的范围)
|
||||
YiB // 1208925819614629174706176
|
||||
)
|
||||
const RunTimes = 10000000
|
||||
const loop = 5
|
||||
const loop = 10
|
||||
|
||||
func demoFunc() {
|
||||
time.Sleep(loop * time.Millisecond)
|
||||
}
|
||||
|
||||
func demoPoolFunc(args interface{}) error {
|
||||
// m := args.(int)
|
||||
// var n int
|
||||
// for i := 0; i < m; i++ {
|
||||
// n += i
|
||||
// }
|
||||
// return nil
|
||||
//m := args.(int)
|
||||
//var n int
|
||||
//for i := 0; i < m; i++ {
|
||||
// n += i
|
||||
//}
|
||||
//return nil
|
||||
n := args.(int)
|
||||
time.Sleep(time.Duration(n) * time.Millisecond)
|
||||
return nil
|
||||
}
|
||||
|
||||
func BenchmarkGoroutine(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
var wg sync.WaitGroup
|
||||
for j := 0; j < RunTimes; j++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
demoFunc()
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
func BenchmarkGoroutineWithFunc(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
var wg sync.WaitGroup
|
||||
b.ResetTimer()
|
||||
for j := 0; j < RunTimes; j++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
|
@ -104,9 +72,7 @@ func BenchmarkGoroutineWithFunc(b *testing.B) {
|
|||
}
|
||||
wg.Wait()
|
||||
}
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
||||
|
@ -117,14 +83,32 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
|||
wg.Done()
|
||||
return nil
|
||||
})
|
||||
b.ResetTimer()
|
||||
for j := 0; j < RunTimes; j++ {
|
||||
wg.Add(1)
|
||||
p.Serve(loop)
|
||||
}
|
||||
wg.Wait()
|
||||
//b.Logf("running goroutines: %d", p.Running())
|
||||
}
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
func BenchmarkGoroutine(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := 0; j < RunTimes; j++ {
|
||||
go demoFunc()
|
||||
}
|
||||
}
|
||||
|
||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
func BenchmarkAntsPool(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
for j := 0; j < RunTimes; j++ {
|
||||
ants.Push(demoFunc)
|
||||
}
|
||||
b.Logf("running goroutines: %d", ants.Running())
|
||||
}
|
||||
//b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
|
66
ants_test.go
66
ants_test.go
|
@ -24,7 +24,6 @@ package ants_test
|
|||
|
||||
import (
|
||||
"runtime"
|
||||
"time"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
|
@ -33,29 +32,6 @@ import (
|
|||
|
||||
var n = 10000000
|
||||
|
||||
//func demoFunc() {
|
||||
// var n int
|
||||
// for i := 0; i < 1000000; i++ {
|
||||
// n += i
|
||||
// }
|
||||
//}
|
||||
|
||||
//func demoFunc() {
|
||||
// var n int
|
||||
// for i := 0; i < 10000; i++ {
|
||||
// n += i
|
||||
// }
|
||||
// fmt.Printf("finish task with result:%d\n", n)
|
||||
//}
|
||||
|
||||
func demoFunc() {
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
// var n int
|
||||
// for i := 0; i < 1000000; i++ {
|
||||
// n += i
|
||||
// }
|
||||
}
|
||||
|
||||
func TestDefaultPool(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
for i := 0; i < n; i++ {
|
||||
|
@ -73,7 +49,7 @@ func TestDefaultPool(t *testing.T) {
|
|||
t.Logf("running workers number:%d", ants.Running())
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
t.Logf("memory usage:%d", mem.TotalAlloc/MiB)
|
||||
t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
func TestNoPool(t *testing.T) {
|
||||
|
@ -89,30 +65,30 @@ func TestNoPool(t *testing.T) {
|
|||
wg.Wait()
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
t.Logf("memory usage:%d", mem.TotalAlloc/MiB)
|
||||
t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||
}
|
||||
|
||||
func TestAntsPoolWithFunc(t *testing.T) {
|
||||
var wg sync.WaitGroup
|
||||
p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
|
||||
demoPoolFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
})
|
||||
for i := 0; i < n; i++ {
|
||||
wg.Add(1)
|
||||
p.Serve(n)
|
||||
}
|
||||
wg.Wait()
|
||||
// func TestAntsPoolWithFunc(t *testing.T) {
|
||||
// var wg sync.WaitGroup
|
||||
// p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
|
||||
// demoPoolFunc(i)
|
||||
// wg.Done()
|
||||
// return nil
|
||||
// })
|
||||
// for i := 0; i < n; i++ {
|
||||
// wg.Add(1)
|
||||
// p.Serve(n)
|
||||
// }
|
||||
// wg.Wait()
|
||||
|
||||
//t.Logf("pool capacity:%d", ants.Cap())
|
||||
//t.Logf("free workers number:%d", ants.Free())
|
||||
// //t.Logf("pool capacity:%d", ants.Cap())
|
||||
// //t.Logf("free workers number:%d", ants.Free())
|
||||
|
||||
t.Logf("running workers number:%d", p.Running())
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
t.Logf("memory usage:%d", mem.TotalAlloc/GiB)
|
||||
}
|
||||
// t.Logf("running workers number:%d", p.Running())
|
||||
// mem := runtime.MemStats{}
|
||||
// runtime.ReadMemStats(&mem)
|
||||
// t.Logf("memory usage:%d", mem.TotalAlloc/GiB)
|
||||
// }
|
||||
|
||||
// func TestNoPool(t *testing.T) {
|
||||
// var wg sync.WaitGroup
|
||||
|
|
|
@ -58,7 +58,7 @@ func main() {
|
|||
// set 100 the size of goroutine pool
|
||||
|
||||
var wg sync.WaitGroup
|
||||
p, _ := ants.NewPoolWithFunc(100, func(i interface{}) error {
|
||||
p, _ := ants.NewPoolWithFunc(10, func(i interface{}) error {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
|
|
Loading…
Reference in New Issue