项目作者: hnlq715

项目描述 :
A simple goroutine pool which can create and release goroutine dynamically, inspired by fasthttp.
高级语言: Go
项目地址: git://github.com/hnlq715/goroutine-pool.git
创建时间: 2018-01-19T01:56:58Z
项目社区:https://github.com/hnlq715/goroutine-pool

开源协议:MIT License

下载


goroutine-pool

Go
Coverage

A simple goroutine pool which can create and release goroutine dynamically, inspired by fasthttp.

install

  1. go get -u -v github.com/sysulq/goroutine-pool

example

  1. package main
  2. import (
  3. "fmt"
  4. pool "github.com/sysulq/goroutine-pool"
  5. "sync"
  6. )
  7. func main() {
  8. wg := sync.WaitGroup{}
  9. for i := 0; i < 5; i++ {
  10. wg.Add(1)
  11. pool.Go(func() {
  12. fmt.Println(i)
  13. wg.Done()
  14. })
  15. }
  16. wg.Wait()
  17. fmt.Println()
  18. for i := 0; i < 5; i++ {
  19. wg.Add(1)
  20. n := i
  21. pool.Go(func() {
  22. fmt.Println(n)
  23. wg.Done()
  24. })
  25. }
  26. wg.Wait()
  27. }
  1. $ go run main.go
  2. 5
  3. 5
  4. 5
  5. 5
  6. 5
  7. 4
  8. 1
  9. 0
  10. 2
  11. 3

benchmarks

With Wait

  1. Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkGoroutine$
  2. goos: windows
  3. goarch: amd64
  4. pkg: workerpool
  5. BenchmarkGoroutine-4 1000 1634621 ns/op 65 B/op 1 allocs/op
  6. PASS
  7. ok workerpool 2.047s
  8. Success: Benchmarks passed.
  1. Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkPool$
  2. goos: windows
  3. goarch: amd64
  4. pkg: workerpool
  5. BenchmarkPool-4 2000 1146818 ns/op 17 B/op 1 allocs/op
  6. PASS
  7. ok workerpool 2.702s
  8. Success: Benchmarks passed.

Without Wait

cpu run at 100%

  1. Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkGoroutineWithoutWait$
  2. goos: windows
  3. goarch: amd64
  4. pkg: workerpool
  5. BenchmarkGoroutineWithoutWait-4 1000000 4556 ns/op 517 B/op 1 allocs/op
  6. PASS
  7. ok workerpool 5.649s
  8. Success: Benchmarks passed.

cpu relatively low

  1. Running tool: D:\Go\bin\go.exe test -benchmem -run=^$ workerpool -bench ^BenchmarkPoolWithoutWait$
  2. goos: windows
  3. goarch: amd64
  4. pkg: workerpool
  5. BenchmarkPoolWithoutWait-4 10000000 144 ns/op 3 B/op 0 allocs/op
  6. PASS
  7. ok workerpool 4.812s
  8. Success: Benchmarks passed.