From 23c80e11581ebac8cebd93c3fb01dd448d15000a Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 20:50:10 +0800 Subject: [PATCH 1/9] add codecov test --- .travis.yml | 10 +++++-- ants_benchmark_test.go | 4 +-- ants_test.go | 66 ++++++++++++++++++++++++++---------------- 3 files changed, 51 insertions(+), 29 deletions(-) diff --git a/.travis.yml b/.travis.yml index c7e9cc6..2e9b7ad 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,6 +6,12 @@ go: - "1.10.x" - master +before_install: + - go get -t -v ./... + script: - - go test -v ./... - - go test -bench=. -benchmem=true -run=none ./... \ No newline at end of file + - go test -race -coverprofile=coverage.txt -covermode=atomic + - go test -bench=. -benchmem=true -run=none ./... + +after_success: + - bash <(curl -s https://codecov.io/bash) \ No newline at end of file diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 7ef3e89..ea22d3d 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -42,9 +42,9 @@ const ( YiB // 1208925819614629174706176 ) const ( - RunTimes = 1000000 + RunTimes = 1000 Param = 10 - AntsSize = 100000 + AntsSize = 100 ) func demoFunc() error { diff --git a/ants_test.go b/ants_test.go index 11059ec..2b8dcb9 100644 --- a/ants_test.go +++ b/ants_test.go @@ -30,9 +30,29 @@ import ( "github.com/panjf2000/ants" ) -var n = 1000000 +var n = 1000 -func TestDefaultPool(t *testing.T) { +func TestAntsPoolWithFunc(t *testing.T) { + var wg sync.WaitGroup + p, _ := ants.NewPoolWithFunc(AntsSize, func(i interface{}) error { + demoPoolFunc(i) + wg.Done() + return nil + }) + defer p.Release() + + for i := 0; i < n; i++ { + wg.Add(1) + p.Serve(n) + } + wg.Wait() + + mem := runtime.MemStats{} + runtime.ReadMemStats(&mem) + t.Logf("memory usage:%d", mem.TotalAlloc/GiB) +} + +func TestAntsPool(t *testing.T) { defer ants.Release() var wg sync.WaitGroup for i := 0; i < n; i++ { @@ -45,10 +65,6 @@ func TestDefaultPool(t *testing.T) { } 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 MB", mem.TotalAlloc/MiB) @@ -70,27 +86,27 @@ func TestNoPool(t *testing.T) { t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) } -// func TestAntsPoolWithFunc(t *testing.T) { -// var wg sync.WaitGroup -// p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { -// demoPoolFunc(i) -// wg.Done() -// return nil -// }) -// for i := 0; i < n; i++ { -// wg.Add(1) -// p.Serve(n) -// } -// wg.Wait() +func TestCodeCov(t *testing.T) { + defer ants.Release() + for i := 0; i < n; i++ { + ants.Submit(demoFunc) + } + t.Logf("pool, capacity:%d", ants.Cap()) + t.Logf("pool, running workers number:%d", ants.Running()) + t.Logf("pool, free workers number:%d", ants.Free()) -// //t.Logf("pool capacity:%d", ants.Cap()) -// //t.Logf("free workers number:%d", ants.Free()) + p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) + defer p.Release() + for i := 0; i < n; i++ { + p.Serve(n) + } + t.Logf("pool with func, capacity:%d", p.Cap()) + t.Logf("pool with func, running workers number:%d", p.Running()) + t.Logf("pool with func, free workers number:%d", p.Free()) + p.ReSize(AntsSize / 2) + t.Logf("pool with func, after resize, capacity:%d", p.Cap()) -// t.Logf("running workers number:%d", p.Running()) -// mem := runtime.MemStats{} -// runtime.ReadMemStats(&mem) -// t.Logf("memory usage:%d", mem.TotalAlloc/GiB) -// } +} // func TestNoPool(t *testing.T) { // var wg sync.WaitGroup From cc81c9573fc2e83201f5eebdfab04fb58c3f485f Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 20:51:14 +0800 Subject: [PATCH 2/9] update --- ants_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ants_test.go b/ants_test.go index 2b8dcb9..44f013e 100644 --- a/ants_test.go +++ b/ants_test.go @@ -43,7 +43,7 @@ func TestAntsPoolWithFunc(t *testing.T) { for i := 0; i < n; i++ { wg.Add(1) - p.Serve(n) + p.Serve(Param) } wg.Wait() @@ -98,7 +98,7 @@ func TestCodeCov(t *testing.T) { p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) defer p.Release() for i := 0; i < n; i++ { - p.Serve(n) + p.Serve(Param) } t.Logf("pool with func, capacity:%d", p.Cap()) t.Logf("pool with func, running workers number:%d", p.Running()) From 4d0682d342e55500533fe5cf241e8abc26e2a4c7 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 21:09:59 +0800 Subject: [PATCH 3/9] fixed race issue --- pool.go | 6 +++--- pool_func.go | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/pool.go b/pool.go index 53471e2..cd10d2a 100644 --- a/pool.go +++ b/pool.go @@ -76,7 +76,7 @@ func (p *Pool) monitorAndClear() { n = i w.task <- nil idleWorkers[i] = nil - p.running-- + atomic.AddInt32(&p.running, 1) } if n > 0 { n++ @@ -177,10 +177,10 @@ func (p *Pool) getWorker() *Worker { workers := p.workers n := len(workers) - 1 if n < 0 { - if p.running >= p.capacity { + if p.Running() >= p.Cap() { waiting = true } else { - p.running++ + atomic.AddInt32(&p.running, 1) } } else { <-p.freeSignal diff --git a/pool_func.go b/pool_func.go index d99269f..bd85f03 100644 --- a/pool_func.go +++ b/pool_func.go @@ -77,7 +77,7 @@ func (p *PoolWithFunc) monitorAndClear() { n = i w.args <- nil idleWorkers[i] = nil - p.running-- + atomic.AddInt32(&p.running, 1) } if n > 0 { n++ @@ -182,10 +182,10 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc { workers := p.workers n := len(workers) - 1 if n < 0 { - if p.running >= p.capacity { + if p.Running() >= p.Cap() { waiting = true } else { - p.running++ + atomic.AddInt32(&p.running, 1) } } else { <-p.freeSignal From 74e49c9f28a291913f6e484f39df603955a316db Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 21:16:27 +0800 Subject: [PATCH 4/9] fixed race issue --- ants_test.go | 1 - pool.go | 2 ++ pool_func.go | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/ants_test.go b/ants_test.go index 44f013e..913a6a6 100644 --- a/ants_test.go +++ b/ants_test.go @@ -105,7 +105,6 @@ func TestCodeCov(t *testing.T) { t.Logf("pool with func, free workers number:%d", p.Free()) p.ReSize(AntsSize / 2) t.Logf("pool with func, after resize, capacity:%d", p.Cap()) - } // func TestNoPool(t *testing.T) { diff --git a/pool.go b/pool.go index cd10d2a..c832c9b 100644 --- a/pool.go +++ b/pool.go @@ -145,10 +145,12 @@ func (p *Pool) Release() error { for i := 0; i < running; i++ { p.getWorker().task <- nil } + p.lock.Lock() for i := range p.workers { p.workers[i] = nil } p.workers = nil + p.lock.Unlock() }) return nil } diff --git a/pool_func.go b/pool_func.go index bd85f03..7040f83 100644 --- a/pool_func.go +++ b/pool_func.go @@ -150,10 +150,12 @@ func (p *PoolWithFunc) Release() error { for i := 0; i < running; i++ { p.getWorker().args <- nil } + p.lock.Lock() for i := range p.workers { p.workers[i] = nil } p.workers = nil + p.lock.Unlock() }) return nil } From 48cb233e4d498e6c02e98c12659433c2a6db93de Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 21:22:20 +0800 Subject: [PATCH 5/9] update --- ants_benchmark_test.go | 4 ++-- ants_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index ea22d3d..c8623aa 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -42,9 +42,9 @@ const ( YiB // 1208925819614629174706176 ) const ( - RunTimes = 1000 + RunTimes = 10000000 Param = 10 - AntsSize = 100 + AntsSize = 100000 ) func demoFunc() error { diff --git a/ants_test.go b/ants_test.go index 913a6a6..f8df4bb 100644 --- a/ants_test.go +++ b/ants_test.go @@ -30,7 +30,7 @@ import ( "github.com/panjf2000/ants" ) -var n = 1000 +var n = 1000000 func TestAntsPoolWithFunc(t *testing.T) { var wg sync.WaitGroup From 4a1439ac906ca4065e4316e3ee24b6a5d92cd773 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 21:27:45 +0800 Subject: [PATCH 6/9] update --- ants_test.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/ants_test.go b/ants_test.go index f8df4bb..18b5ffb 100644 --- a/ants_test.go +++ b/ants_test.go @@ -87,13 +87,16 @@ func TestNoPool(t *testing.T) { } func TestCodeCov(t *testing.T) { - defer ants.Release() + p0, _ := ants.NewPool(AntsSize) + defer p0.Release() for i := 0; i < n; i++ { - ants.Submit(demoFunc) + p0.Submit(demoFunc) } - t.Logf("pool, capacity:%d", ants.Cap()) - t.Logf("pool, running workers number:%d", ants.Running()) - t.Logf("pool, free workers number:%d", ants.Free()) + t.Logf("pool, capacity:%d", p0.Cap()) + t.Logf("pool, running workers number:%d", p0.Running()) + t.Logf("pool, free workers number:%d", p0.Free()) + p0.ReSize(AntsSize / 2) + t.Logf("pool, after resize, capacity:%d", p0.Cap()) p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) defer p.Release() From 9da43e1115d007061f4b260a4f00ec080a2f6239 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 21:47:53 +0800 Subject: [PATCH 7/9] update codecov test --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 2e9b7ad..9c67cfc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +11,7 @@ before_install: script: - go test -race -coverprofile=coverage.txt -covermode=atomic - - go test -bench=. -benchmem=true -run=none ./... +# - go test -bench=. -benchmem=true -run=none ./... after_success: - bash <(curl -s https://codecov.io/bash) \ No newline at end of file From 9b4f9c7b48aab86d5e454515fe060f135661f02f Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 22:45:55 +0800 Subject: [PATCH 8/9] update codecov test --- README.md | 15 +++++++++------ ants_benchmark_test.go | 2 +- ants_test.go | 17 +++++++++++++++++ pool.go | 3 --- pool_func.go | 3 --- 5 files changed, 27 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 85d91fa..4a00b99 100644 --- a/README.md +++ b/README.md @@ -6,9 +6,10 @@ [![Build Status][1]][2] -[![godoc for panjf2000/ants][3]][4] +[![codecov][3]][4] [![goreportcard for panjf2000/ants][5]][6] -[![MIT Licence][7]][8] +[![godoc for panjf2000/ants][7]][8] +[![MIT Licence][9]][10] [中文项目说明](README_ZH.md) | [Project Tutorial](http://blog.taohuawu.club/article/42) @@ -185,9 +186,11 @@ There was only the test of `ants` Pool because my computer was crash when it rea [1]: https://travis-ci.com/panjf2000/ants.svg?branch=develop [2]: https://travis-ci.com/panjf2000/ants -[3]: https://godoc.org/github.com/panjf2000/ants?status.svg -[4]: https://godoc.org/github.com/panjf2000/ants +[3]: https://codecov.io/gh/panjf2000/ants +[4]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg [5]: https://goreportcard.com/badge/github.com/panjf2000/ants [6]: https://goreportcard.com/report/github.com/panjf2000/ants -[7]: https://badges.frapsoft.com/os/mit/mit.svg?v=103 -[8]: https://opensource.org/licenses/mit-license.php +[7]: https://godoc.org/github.com/panjf2000/ants?status.svg +[8]: https://godoc.org/github.com/panjf2000/ants +[9]: https://badges.frapsoft.com/os/mit/mit.svg?v=103 +[10]: https://opensource.org/licenses/mit-license.php diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index c8623aa..440a3c8 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -44,7 +44,7 @@ const ( const ( RunTimes = 10000000 Param = 10 - AntsSize = 100000 + AntsSize = 50000 ) func demoFunc() error { diff --git a/ants_test.go b/ants_test.go index 18b5ffb..72250b4 100644 --- a/ants_test.go +++ b/ants_test.go @@ -65,6 +65,10 @@ func TestAntsPool(t *testing.T) { } wg.Wait() + t.Logf("pool, capacity:%d", ants.Cap()) + t.Logf("pool, running workers number:%d", ants.Running()) + t.Logf("pool, free workers number:%d", ants.Free()) + mem := runtime.MemStats{} runtime.ReadMemStats(&mem) t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) @@ -87,7 +91,17 @@ func TestNoPool(t *testing.T) { } func TestCodeCov(t *testing.T) { + _, err := ants.NewTimingPool(-1, -1) + t.Log(err) + _, err = ants.NewTimingPool(1, -1) + t.Log(err) + _, err = ants.NewTimingPoolWithFunc(-1, -1, demoPoolFunc) + t.Log(err) + _, err = ants.NewTimingPoolWithFunc(1, -1, demoPoolFunc) + t.Log(err) + p0, _ := ants.NewPool(AntsSize) + defer p0.Submit(demoFunc) defer p0.Release() for i := 0; i < n; i++ { p0.Submit(demoFunc) @@ -95,10 +109,12 @@ func TestCodeCov(t *testing.T) { t.Logf("pool, capacity:%d", p0.Cap()) t.Logf("pool, running workers number:%d", p0.Running()) t.Logf("pool, free workers number:%d", p0.Free()) + p0.ReSize(AntsSize) p0.ReSize(AntsSize / 2) t.Logf("pool, after resize, capacity:%d", p0.Cap()) p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc) + defer p.Serve(Param) defer p.Release() for i := 0; i < n; i++ { p.Serve(Param) @@ -106,6 +122,7 @@ func TestCodeCov(t *testing.T) { t.Logf("pool with func, capacity:%d", p.Cap()) t.Logf("pool with func, running workers number:%d", p.Running()) t.Logf("pool with func, free workers number:%d", p.Free()) + p.ReSize(AntsSize) p.ReSize(AntsSize / 2) t.Logf("pool with func, after resize, capacity:%d", p.Cap()) } diff --git a/pool.go b/pool.go index c832c9b..3f988aa 100644 --- a/pool.go +++ b/pool.go @@ -146,9 +146,6 @@ func (p *Pool) Release() error { p.getWorker().task <- nil } p.lock.Lock() - for i := range p.workers { - p.workers[i] = nil - } p.workers = nil p.lock.Unlock() }) diff --git a/pool_func.go b/pool_func.go index 7040f83..c4dea88 100644 --- a/pool_func.go +++ b/pool_func.go @@ -151,9 +151,6 @@ func (p *PoolWithFunc) Release() error { p.getWorker().args <- nil } p.lock.Lock() - for i := range p.workers { - p.workers[i] = nil - } p.workers = nil p.lock.Unlock() }) From 68924c0193aa3d0d90abc7f40c8ba3ba2fdfa169 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Sun, 15 Jul 2018 22:49:47 +0800 Subject: [PATCH 9/9] update - --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 4a00b99..b0ead0d 100644 --- a/README.md +++ b/README.md @@ -186,8 +186,8 @@ There was only the test of `ants` Pool because my computer was crash when it rea [1]: https://travis-ci.com/panjf2000/ants.svg?branch=develop [2]: https://travis-ci.com/panjf2000/ants -[3]: https://codecov.io/gh/panjf2000/ants -[4]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg +[3]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg +[4]: https://codecov.io/gh/panjf2000/ants [5]: https://goreportcard.com/badge/github.com/panjf2000/ants [6]: https://goreportcard.com/report/github.com/panjf2000/ants [7]: https://godoc.org/github.com/panjf2000/ants?status.svg