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"
- master
before_install:
- go get -t -v ./...
script:
- go test -v ./...
- go test -bench=. -benchmem=true -run=none ./...
- go test -race -coverprofile=coverage.txt -covermode=atomic
# - 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]
[![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=master
[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/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://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

View File

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

View File

@ -32,7 +32,27 @@ import (
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()
var wg sync.WaitGroup
for i := 0; i < n; i++ {
@ -45,10 +65,10 @@ func TestDefaultPool(t *testing.T) {
}
wg.Wait()
//t.Logf("pool capacity:%d", ants.Cap())
//t.Logf("free workers number:%d", ants.Free())
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("running workers number:%d", ants.Running())
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
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)
}
// 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) {
_, 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)
// //t.Logf("pool capacity:%d", ants.Cap())
// //t.Logf("free workers number:%d", ants.Free())
p0, _ := ants.NewPool(AntsSize)
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())
// mem := runtime.MemStats{}
// runtime.ReadMemStats(&mem)
// t.Logf("memory usage:%d", mem.TotalAlloc/GiB)
// }
p, _ := ants.NewPoolWithFunc(AntsSize, demoPoolFunc)
defer p.Serve(Param)
defer p.Release()
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) {
// var wg sync.WaitGroup

11
pool.go
View File

@ -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++
@ -145,10 +145,9 @@ func (p *Pool) Release() error {
for i := 0; i < running; i++ {
p.getWorker().task <- nil
}
for i := range p.workers {
p.workers[i] = nil
}
p.lock.Lock()
p.workers = nil
p.lock.Unlock()
})
return nil
}
@ -177,10 +176,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

View File

@ -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++
@ -150,10 +150,9 @@ func (p *PoolWithFunc) Release() error {
for i := 0; i < running; i++ {
p.getWorker().args <- nil
}
for i := range p.workers {
p.workers[i] = nil
}
p.lock.Lock()
p.workers = nil
p.lock.Unlock()
})
return nil
}
@ -182,10 +181,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