🐳 Add test cases for sync.Pool

This commit is contained in:
Andy Pan 2019-01-27 05:03:26 +08:00
parent 5c666692e9
commit 840634ae51
1 changed files with 34 additions and 22 deletions

View File

@ -75,25 +75,39 @@ func TestAntsPoolWithFuncWaitToGetWorker(t *testing.T) {
} }
// TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool. // TestAntsPoolGetWorkerFromCache is used to test getting worker from sync.Pool.
//func TestAntsPoolGetWorkerFromCache(t *testing.T) { func TestAntsPoolGetWorkerFromCache(t *testing.T) {
// var wg sync.WaitGroup p, _ := ants.NewPool(TestSize)
// p, _ := ants.NewPool(AntsSize) defer p.Release()
// defer p.Release()
// for i := 0; i < AntsSize; i++ {
// for i := 0; i < n; i++ { p.Submit(demoFunc)
// wg.Add(1) }
// p.Submit(func() { time.Sleep(ants.DefaultCleanIntervalTime * time.Second)
// demoPoolFunc(Param) p.Submit(demoFunc)
// wg.Done() t.Logf("pool, running workers number:%d", p.Running())
// }) mem := runtime.MemStats{}
// } runtime.ReadMemStats(&mem)
// wg.Wait() curMem = mem.TotalAlloc/MiB - curMem
// t.Logf("pool, running workers number:%d", p.Running()) t.Logf("memory usage:%d MB", curMem)
// mem := runtime.MemStats{} }
// runtime.ReadMemStats(&mem)
// curMem = mem.TotalAlloc/MiB - curMem // TestAntsPoolWithFuncGetWorkerFromCache is used to test getting worker from sync.Pool.
// t.Logf("memory usage:%d MB", curMem) func TestAntsPoolWithFuncGetWorkerFromCache(t *testing.T) {
//} dur := 10
p, _ := ants.NewPoolWithFunc(TestSize, demoPoolFunc)
defer p.Release()
for i := 0; i < AntsSize; i++ {
p.Serve(dur)
}
time.Sleep(ants.DefaultCleanIntervalTime * time.Second)
p.Serve(dur)
t.Logf("pool, running workers number:%d", p.Running())
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
curMem = mem.TotalAlloc/MiB - curMem
t.Logf("memory usage:%d MB", curMem)
}
//------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------
// Contrast between goroutines without a pool and goroutines with ants pool. // Contrast between goroutines without a pool and goroutines with ants pool.
@ -246,9 +260,7 @@ func TestRestCodeCoverage(t *testing.T) {
defer p0.Submit(demoFunc) defer p0.Submit(demoFunc)
defer p0.Release() defer p0.Release()
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
p0.Submit(func() { p0.Submit(demoFunc)
demoPoolFunc(Param)
})
} }
t.Logf("pool, capacity:%d", p0.Cap()) t.Logf("pool, capacity:%d", p0.Cap())
t.Logf("pool, running workers number:%d", p0.Running()) t.Logf("pool, running workers number:%d", p0.Running())