mirror of https://github.com/panjf2000/ants.git
update -
This commit is contained in:
parent
0453f88168
commit
19e41472a9
|
@ -23,21 +23,37 @@
|
|||
package ants_test
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/panjf2000/ants"
|
||||
)
|
||||
|
||||
const RunTimes = 1000000
|
||||
const loop = 1000000
|
||||
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
|
||||
const loop = 5
|
||||
|
||||
func demoPoolFunc(args interface{}) error {
|
||||
m := args.(int)
|
||||
var n int
|
||||
for i := 0; i < m; i++ {
|
||||
n += i
|
||||
}
|
||||
// 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
|
||||
}
|
||||
|
||||
|
@ -53,6 +69,9 @@ func BenchmarkGoroutine(b *testing.B) {
|
|||
}()
|
||||
}
|
||||
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) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
var wg sync.WaitGroup
|
||||
p, _ := ants.NewPoolWithFunc(100000, func(i interface{}) error {
|
||||
p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error {
|
||||
demoPoolFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
|
@ -84,5 +103,9 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
|||
p.Serve(loop)
|
||||
}
|
||||
wg.Wait()
|
||||
mem := runtime.MemStats{}
|
||||
runtime.ReadMemStats(&mem)
|
||||
b.Logf("GB:%d", GiB)
|
||||
b.Logf("total memory usage:%dG", mem.TotalAlloc/GiB)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,44 +24,50 @@ package main
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/panjf2000/ants"
|
||||
"sync"
|
||||
|
||||
"github.com/panjf2000/ants"
|
||||
)
|
||||
|
||||
func myFunc() {
|
||||
fmt.Println("Hello World!")
|
||||
var str = "Hello World!"
|
||||
|
||||
func myFunc(i interface{}) error {
|
||||
s := i.(string)
|
||||
fmt.Println(s)
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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 := 1000
|
||||
|
||||
// set 100 the size of goroutine pool
|
||||
|
||||
var wg sync.WaitGroup
|
||||
// submit all your tasks to ants pool
|
||||
p, _ := ants.NewPoolWithFunc(100, func(i interface{}) error {
|
||||
myFunc(i)
|
||||
wg.Done()
|
||||
return nil
|
||||
})
|
||||
// submit
|
||||
for i := 0; i < runTimes; i++ {
|
||||
wg.Add(1)
|
||||
ants.Push(func() {
|
||||
myFunc()
|
||||
wg.Done()
|
||||
})
|
||||
p.Serve(str)
|
||||
}
|
||||
wg.Wait()
|
||||
fmt.Println("finish all tasks!")
|
||||
}
|
||||
|
||||
//func main() {
|
||||
// runTimes := 10000
|
||||
//
|
||||
// // set 100 the size of goroutine pool
|
||||
// p, _ := ants.NewPool(100)
|
||||
//
|
||||
// var wg sync.WaitGroup
|
||||
// // submit
|
||||
// for i := 0; i < runTimes; i++ {
|
||||
// wg.Add(1)
|
||||
// p.Push(func() {
|
||||
// myFunc()
|
||||
// wg.Done()
|
||||
// })
|
||||
// }
|
||||
// wg.Wait()
|
||||
// fmt.Println("finish all tasks!")
|
||||
//}
|
||||
|
|
Loading…
Reference in New Issue