forked from mirror/ants
Merge branch 'develop'
This commit is contained in:
commit
8fb25cc0c4
|
@ -23,25 +23,75 @@
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
func BenchmarkGoroutine(b *testing.B) {
|
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++ {
|
for i := 0; i < b.N; i++ {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
b.ResetTimer()
|
b.ResetTimer()
|
||||||
|
@ -54,26 +104,15 @@ func BenchmarkGoroutine(b *testing.B) {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
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()
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
|
|
||||||
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
|
||||||
|
@ -85,4 +124,7 @@ func BenchmarkAntsPoolWithFunc(b *testing.B) {
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
}
|
}
|
||||||
|
mem := runtime.MemStats{}
|
||||||
|
runtime.ReadMemStats(&mem)
|
||||||
|
b.Logf("total memory usage:%d MB", mem.TotalAlloc/MiB)
|
||||||
}
|
}
|
||||||
|
|
111
ants_test.go
111
ants_test.go
|
@ -24,13 +24,14 @@ package ants_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"time"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/panjf2000/ants"
|
"github.com/panjf2000/ants"
|
||||||
)
|
)
|
||||||
|
|
||||||
var n = 100000
|
var n = 10000000
|
||||||
|
|
||||||
//func demoFunc() {
|
//func demoFunc() {
|
||||||
// var n int
|
// var n int
|
||||||
|
@ -48,48 +49,48 @@ var n = 100000
|
||||||
//}
|
//}
|
||||||
|
|
||||||
func demoFunc() {
|
func demoFunc() {
|
||||||
//time.Sleep(time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
var n int
|
// var n int
|
||||||
for i := 0; i < 1000000; i++ {
|
// for i := 0; i < 1000000; i++ {
|
||||||
n += i
|
// n += i
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
//func TestDefaultPool(t *testing.T) {
|
func TestDefaultPool(t *testing.T) {
|
||||||
// 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)
|
||||||
// ants.Push(func() {
|
ants.Push(func() {
|
||||||
// demoFunc()
|
demoFunc()
|
||||||
// wg.Done()
|
wg.Done()
|
||||||
// })
|
})
|
||||||
// }
|
}
|
||||||
// wg.Wait()
|
wg.Wait()
|
||||||
//
|
|
||||||
// //t.Logf("pool capacity:%d", ants.Cap())
|
|
||||||
// //t.Logf("free workers number:%d", ants.Free())
|
|
||||||
//
|
|
||||||
// t.Logf("running workers number:%d", ants.Running())
|
|
||||||
// mem := runtime.MemStats{}
|
|
||||||
// runtime.ReadMemStats(&mem)
|
|
||||||
// t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
|
||||||
//}
|
|
||||||
|
|
||||||
//func TestNoPool(t *testing.T) {
|
//t.Logf("pool capacity:%d", ants.Cap())
|
||||||
// var wg sync.WaitGroup
|
//t.Logf("free workers number:%d", ants.Free())
|
||||||
// for i := 0; i < n; i++ {
|
|
||||||
// wg.Add(1)
|
t.Logf("running workers number:%d", ants.Running())
|
||||||
// go func() {
|
mem := runtime.MemStats{}
|
||||||
// demoFunc()
|
runtime.ReadMemStats(&mem)
|
||||||
// wg.Done()
|
t.Logf("memory usage:%d", mem.TotalAlloc/MiB)
|
||||||
// }()
|
}
|
||||||
// }
|
|
||||||
//
|
func TestNoPool(t *testing.T) {
|
||||||
// wg.Wait()
|
var wg sync.WaitGroup
|
||||||
// mem := runtime.MemStats{}
|
for i := 0; i < n; i++ {
|
||||||
// runtime.ReadMemStats(&mem)
|
wg.Add(1)
|
||||||
// t.Logf("memory usage:%d", mem.TotalAlloc/1024)
|
go func() {
|
||||||
//}
|
demoFunc()
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
}
|
||||||
|
|
||||||
|
wg.Wait()
|
||||||
|
mem := runtime.MemStats{}
|
||||||
|
runtime.ReadMemStats(&mem)
|
||||||
|
t.Logf("memory usage:%d", mem.TotalAlloc/MiB)
|
||||||
|
}
|
||||||
|
|
||||||
func TestAntsPoolWithFunc(t *testing.T) {
|
func TestAntsPoolWithFunc(t *testing.T) {
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
|
@ -110,24 +111,24 @@ func TestAntsPoolWithFunc(t *testing.T) {
|
||||||
t.Logf("running workers number:%d", p.Running())
|
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/GiB)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestNoPool(t *testing.T) {
|
// func TestNoPool(t *testing.T) {
|
||||||
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)
|
||||||
go func() {
|
// go func() {
|
||||||
demoPoolFunc(n)
|
// demoPoolFunc(n)
|
||||||
wg.Done()
|
// wg.Done()
|
||||||
}()
|
// }()
|
||||||
}
|
// }
|
||||||
|
|
||||||
wg.Wait()
|
// wg.Wait()
|
||||||
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/GiB)
|
||||||
}
|
// }
|
||||||
|
|
||||||
//func TestCustomPool(t *testing.T) {
|
//func TestCustomPool(t *testing.T) {
|
||||||
// p, _ := ants.NewPool(30000)
|
// p, _ := ants.NewPool(30000)
|
||||||
|
|
|
@ -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() {
|
||||||
|
// 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() {
|
func main() {
|
||||||
runTimes := 10000
|
runTimes := 1000
|
||||||
|
|
||||||
|
// set 100 the size of goroutine pool
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
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++ {
|
for i := 0; i < runTimes; i++ {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
ants.Push(func() {
|
p.Serve(str)
|
||||||
myFunc()
|
|
||||||
wg.Done()
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
wg.Wait()
|
wg.Wait()
|
||||||
fmt.Println("finish all tasks!")
|
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!")
|
|
||||||
//}
|
|
||||||
|
|
2
pool.go
2
pool.go
|
@ -42,7 +42,7 @@ type Pool struct {
|
||||||
// running is the number of the currently running goroutines.
|
// running is the number of the currently running goroutines.
|
||||||
running int32
|
running int32
|
||||||
|
|
||||||
// signal is used to notice pool there are available
|
// freeSignal is used to notice pool there are available
|
||||||
// workers which can be sent to work.
|
// workers which can be sent to work.
|
||||||
freeSignal chan sig
|
freeSignal chan sig
|
||||||
|
|
||||||
|
|
|
@ -40,7 +40,7 @@ type PoolWithFunc struct {
|
||||||
// running is the number of the currently running goroutines.
|
// running is the number of the currently running goroutines.
|
||||||
running int32
|
running int32
|
||||||
|
|
||||||
// signal is used to notice pool there are available
|
// freeSignal is used to notice pool there are available
|
||||||
// workers which can be sent to work.
|
// workers which can be sent to work.
|
||||||
freeSignal chan sig
|
freeSignal chan sig
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue