项目作者: bdxing

项目描述 :
Auto scaling goroutine pool.
高级语言: Go
项目地址: git://github.com/bdxing/workerPool.git
创建时间: 2019-01-23T06:37:55Z
项目社区:https://github.com/bdxing/workerPool

开源协议:

下载


workerPool

" class="reference-link">

workerPool

This is a goroutine pool, which can avoid a large amount of performance consumption of creation and destruction under high concurrency, ensure the stable scheduling of modules, and automatically scale the size of the co-program pool to fit the current business scheduling.

Installation

To install this package, you need to setup your Go workspace. The simplest way to install the library is to run:

  1. go get github.com/bdxing/workerPool

Example

  1. package main
  2. import (
  3. . "github.com/bdxing/workerPool"
  4. "log"
  5. "time"
  6. )
  7. type TestAdd struct {
  8. a int
  9. b int
  10. }
  11. func main() {
  12. workerFunc := func(tmp interface{}) {
  13. ta := tmp.(*TestAdd)
  14. ta.a += ta.b
  15. }
  16. wp := &WorkerPool{
  17. MaxWorkerCount: DefaultConcurrency, // max worker goroutine number, Hot add
  18. MaxIdleWorkerDuration: 5 * time.Second, // worker goroutine max Idle Worker Duration
  19. WorkerFunc: workerFunc, // worker method
  20. }
  21. wp.Start()
  22. nowTime := time.Now()
  23. for i := 0; i < 100000000; i++ {
  24. if !wp.Serve(&TestAdd{
  25. a: i,
  26. b: i + 1,
  27. }) {
  28. log.Printf("wp.Serve(): timeout\n")
  29. }
  30. }
  31. log.Printf("consuming time: %v\n", time.Now().Sub(nowTime))
  32. // shutdown worker pool
  33. //wp.Stop()
  34. }

Benchmark

  1. goos: windows
  2. goarch: amd64
  3. pkg: github.com/bdxing/workerPool
  4. cpu: Intel(R) Core(TM) i5-10400F CPU @ 2.90GHz
  5. BenchmarkWorkerPool_Serve
  6. worker_pool_test.go:80: taskCount: 1, workerCount: 1
  7. worker_pool_test.go:80: taskCount: 100, workerCount: 16
  8. worker_pool_test.go:80: taskCount: 10000, workerCount: 45
  9. worker_pool_test.go:80: taskCount: 1000000, workerCount: 111
  10. worker_pool_test.go:80: taskCount: 2567829, workerCount: 206
  11. BenchmarkWorkerPool_Serve-12 2567829 467.9 ns/op 16 B/op 1 allocs/op
  12. PASS