Merge branch 'develop'

This commit is contained in:
Andy Pan 2018-07-15 22:52:51 +08:00
commit f8bfed2da6
6 changed files with 87 additions and 45 deletions

View File

@ -6,6 +6,12 @@ go:
- "1.10.x" - "1.10.x"
- master - master
before_install:
- go get -t -v ./...
script: script:
- go test -v ./... - 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)

View File

@ -6,9 +6,10 @@
[![Build Status][1]][2] [![Build Status][1]][2]
[![godoc for panjf2000/ants][3]][4] [![codecov][3]][4]
[![goreportcard for panjf2000/ants][5]][6] [![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) [中文项目说明](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=master [1]: https://travis-ci.com/panjf2000/ants.svg?branch=master
[2]: https://travis-ci.com/panjf2000/ants [2]: https://travis-ci.com/panjf2000/ants
[3]: https://godoc.org/github.com/panjf2000/ants?status.svg [3]: https://codecov.io/gh/panjf2000/ants/branch/develop/graph/badge.svg
[4]: https://godoc.org/github.com/panjf2000/ants [4]: https://codecov.io/gh/panjf2000/ants
[5]: https://goreportcard.com/badge/github.com/panjf2000/ants [5]: https://goreportcard.com/badge/github.com/panjf2000/ants
[6]: https://goreportcard.com/report/github.com/panjf2000/ants [6]: https://goreportcard.com/report/github.com/panjf2000/ants
[7]: https://badges.frapsoft.com/os/mit/mit.svg?v=103 [7]: https://godoc.org/github.com/panjf2000/ants?status.svg
[8]: https://opensource.org/licenses/mit-license.php [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

View File

@ -42,9 +42,9 @@ const (
YiB // 1208925819614629174706176 YiB // 1208925819614629174706176
) )
const ( const (
RunTimes = 1000000 RunTimes = 10000000
Param = 10 Param = 10
AntsSize = 100000 AntsSize = 50000
) )
func demoFunc() error { func demoFunc() error {

View File

@ -32,7 +32,27 @@ import (
var n = 1000000 var n = 1000000
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(Param)
}
wg.Wait()
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
t.Logf("memory usage:%d", mem.TotalAlloc/GiB)
}
func TestAntsPool(t *testing.T) {
defer ants.Release() defer ants.Release()
var wg sync.WaitGroup var wg sync.WaitGroup
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -45,10 +65,10 @@ func TestDefaultPool(t *testing.T) {
} }
wg.Wait() wg.Wait()
//t.Logf("pool capacity:%d", ants.Cap()) t.Logf("pool, capacity:%d", ants.Cap())
//t.Logf("free workers number:%d", ants.Free()) t.Logf("pool, running workers number:%d", ants.Running())
t.Logf("pool, free workers number:%d", ants.Free())
t.Logf("running workers number:%d", ants.Running())
mem := runtime.MemStats{} mem := runtime.MemStats{}
runtime.ReadMemStats(&mem) runtime.ReadMemStats(&mem)
t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB)
@ -70,27 +90,42 @@ func TestNoPool(t *testing.T) {
t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB) t.Logf("memory usage:%d MB", mem.TotalAlloc/MiB)
} }
// func TestAntsPoolWithFunc(t *testing.T) { func TestCodeCov(t *testing.T) {
// var wg sync.WaitGroup _, err := ants.NewTimingPool(-1, -1)
// p, _ := ants.NewPoolWithFunc(50000, func(i interface{}) error { t.Log(err)
// demoPoolFunc(i) _, err = ants.NewTimingPool(1, -1)
// wg.Done() t.Log(err)
// return nil _, err = ants.NewTimingPoolWithFunc(-1, -1, demoPoolFunc)
// }) t.Log(err)
// for i := 0; i < n; i++ { _, err = ants.NewTimingPoolWithFunc(1, -1, demoPoolFunc)
// wg.Add(1) t.Log(err)
// p.Serve(n)
// }
// wg.Wait()
// //t.Logf("pool capacity:%d", ants.Cap()) p0, _ := ants.NewPool(AntsSize)
// //t.Logf("free workers number:%d", ants.Free()) defer p0.Submit(demoFunc)
defer p0.Release()
for i := 0; i < n; i++ {
p0.Submit(demoFunc)
}
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())
// t.Logf("running workers number:%d", p.Running()) p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
// mem := runtime.MemStats{} defer p.Serve(Param)
// runtime.ReadMemStats(&mem) defer p.Release()
// t.Logf("memory usage:%d", mem.TotalAlloc/GiB) for i := 0; i < n; i++ {
// } p.Serve(Param)
}
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())
}
// func TestNoPool(t *testing.T) { // func TestNoPool(t *testing.T) {
// var wg sync.WaitGroup // var wg sync.WaitGroup

11
pool.go
View File

@ -76,7 +76,7 @@ func (p *Pool) monitorAndClear() {
n = i n = i
w.task <- nil w.task <- nil
idleWorkers[i] = nil idleWorkers[i] = nil
p.running-- atomic.AddInt32(&p.running, 1)
} }
if n > 0 { if n > 0 {
n++ n++
@ -145,10 +145,9 @@ func (p *Pool) Release() error {
for i := 0; i < running; i++ { for i := 0; i < running; i++ {
p.getWorker().task <- nil p.getWorker().task <- nil
} }
for i := range p.workers { p.lock.Lock()
p.workers[i] = nil
}
p.workers = nil p.workers = nil
p.lock.Unlock()
}) })
return nil return nil
} }
@ -177,10 +176,10 @@ func (p *Pool) getWorker() *Worker {
workers := p.workers workers := p.workers
n := len(workers) - 1 n := len(workers) - 1
if n < 0 { if n < 0 {
if p.running >= p.capacity { if p.Running() >= p.Cap() {
waiting = true waiting = true
} else { } else {
p.running++ atomic.AddInt32(&p.running, 1)
} }
} else { } else {
<-p.freeSignal <-p.freeSignal

View File

@ -77,7 +77,7 @@ func (p *PoolWithFunc) monitorAndClear() {
n = i n = i
w.args <- nil w.args <- nil
idleWorkers[i] = nil idleWorkers[i] = nil
p.running-- atomic.AddInt32(&p.running, 1)
} }
if n > 0 { if n > 0 {
n++ n++
@ -150,10 +150,9 @@ func (p *PoolWithFunc) Release() error {
for i := 0; i < running; i++ { for i := 0; i < running; i++ {
p.getWorker().args <- nil p.getWorker().args <- nil
} }
for i := range p.workers { p.lock.Lock()
p.workers[i] = nil
}
p.workers = nil p.workers = nil
p.lock.Unlock()
}) })
return nil return nil
} }
@ -182,10 +181,10 @@ func (p *PoolWithFunc) getWorker() *WorkerWithFunc {
workers := p.workers workers := p.workers
n := len(workers) - 1 n := len(workers) - 1
if n < 0 { if n < 0 {
if p.running >= p.capacity { if p.Running() >= p.Cap() {
waiting = true waiting = true
} else { } else {
p.running++ atomic.AddInt32(&p.running, 1)
} }
} else { } else {
<-p.freeSignal <-p.freeSignal