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