This commit is contained in:
andy pan 2018-05-23 11:05:23 +08:00
parent 0453f88168
commit 19e41472a9
2 changed files with 65 additions and 36 deletions

View File

@ -23,21 +23,37 @@
package ants_test package ants_test
import ( import (
"runtime"
"sync" "sync"
"testing" "testing"
"time"
"github.com/panjf2000/ants" "github.com/panjf2000/ants"
) )
const RunTimes = 1000000 const (
const loop = 1000000 _ = 1 << (10 * iota)
KiB // 1024
MiB // 1048576
GiB // 1073741824
TiB // 1099511627776 (超过了int32的范围)
PiB // 1125899906842624
EiB // 1152921504606846976
ZiB // 1180591620717411303424 (超过了int64的范围)
YiB // 1208925819614629174706176
)
const RunTimes = 10000000
const loop = 5
func demoPoolFunc(args interface{}) error { func demoPoolFunc(args interface{}) error {
m := args.(int) // m := args.(int)
var n int // var n int
for i := 0; i < m; i++ { // for i := 0; i < m; i++ {
n += i // n += i
} // }
// return nil
n := args.(int)
time.Sleep(time.Duration(n) * time.Millisecond)
return nil return nil
} }
@ -53,6 +69,9 @@ func BenchmarkGoroutine(b *testing.B) {
}() }()
} }
wg.Wait() wg.Wait()
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB)
} }
} }
@ -73,7 +92,7 @@ func BenchmarkGoroutine(b *testing.B) {
func BenchmarkAntsPoolWithFunc(b *testing.B) { func BenchmarkAntsPoolWithFunc(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {
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
@ -84,5 +103,9 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
p.Serve(loop) p.Serve(loop)
} }
wg.Wait() wg.Wait()
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
b.Logf("GB:%d", GiB)
b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB)
} }
} }

View File

@ -24,44 +24,50 @@ package main
import ( import (
"fmt" "fmt"
"github.com/panjf2000/ants"
"sync" "sync"
"github.com/panjf2000/ants"
) )
func myFunc() { var str = "Hello World!"
fmt.Println("Hello World!")
func myFunc(i interface{}) error {
s := i.(string)
fmt.Println(s)
return nil
} }
func main() { // func main() {
runTimes := 10000
var wg sync.WaitGroup
// submit all your tasks to ants pool
for i := 0; i < runTimes; i++ {
wg.Add(1)
ants.Push(func() {
myFunc()
wg.Done()
})
}
wg.Wait()
fmt.Println("finish all tasks!")
}
//func main() {
// runTimes := 10000 // runTimes := 10000
//
// // set 100 the size of goroutine pool
// p, _ := ants.NewPool(100)
//
// var wg sync.WaitGroup // var wg sync.WaitGroup
// // submit // // submit all your tasks to ants pool
// for i := 0; i < runTimes; i++ { // for i := 0; i < runTimes; i++ {
// wg.Add(1) // wg.Add(1)
// p.Push(func() { // ants.Push(func() {
// myFunc() // myFunc()
// wg.Done() // wg.Done()
// }) // })
// } // }
// wg.Wait() // wg.Wait()
// fmt.Println("finish all tasks!") // fmt.Println("finish all tasks!")
//} // }
func main() {
runTimes := 1000
// set 100 the size of goroutine pool
var wg sync.WaitGroup
p, _ := ants.NewPoolWithFunc(100, func(i interface{}) error {
myFunc(i)
wg.Done()
return nil
})
// submit
for i := 0; i < runTimes; i++ {
wg.Add(1)
p.Serve(str)
}
wg.Wait()
fmt.Println("finish all tasks!")
}