Skip to content

Commit

Permalink
update -
Browse files Browse the repository at this point in the history
  • Loading branch information
panjf2000 committed May 23, 2018
1 parent 0453f88 commit 19e4147
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 36 deletions.
39 changes: 31 additions & 8 deletions ants_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}
}

Expand All @@ -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
Expand All @@ -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)
}
}
62 changes: 34 additions & 28 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!")
//}

0 comments on commit 19e4147

Please sign in to comment.