完成grpool性能测试

This commit is contained in:
John
2018-01-16 17:07:46 +08:00
parent c68e051a1b
commit 12fe0f2a90
2 changed files with 35 additions and 17 deletions

View File

@ -16,7 +16,7 @@ import (
const (
gDEFAULT_EXPIRE_TIME = 60 // 默认goroutine过期时间
gDEFAULT_CLEAR_INTERVAL = 3 // 定期检查任务过期时间间隔
gDEFAULT_CLEAR_INTERVAL = 60 // 定期检查任务过期时间间隔
)
// goroutine池对象
@ -41,17 +41,12 @@ type PoolJob struct {
var defaultPool = New(gDEFAULT_EXPIRE_TIME)
// 创建goroutine池管理对象给定过期时间(秒)
// 第二个参数设置允许同时执行的最大任务数,用户可以限制最大并行任务数(非必需参数,默认为不限制)
func New(expire int, sizes...int) *Pool {
size := math.MaxUint32
if len(sizes) > 0 {
size = sizes[0]
}
func New(expire int) *Pool {
p := &Pool {
expire : int32(expire),
queue : glist.NewSafeList(),
funcs : glist.NewSafeList(),
funcEvents : make(chan struct{}, size),
funcEvents : make(chan struct{}, math.MaxUint32),
stopEvents : make(chan struct{}, 1),
}
p.startWorkLoop()

View File

@ -10,21 +10,44 @@ package grpool_test
import (
"testing"
"gitee.com/johng/gf/g/os/grpool"
"runtime"
"fmt"
)
var n = 5000000
func increment() {
for i := 0; i < 1000000; i++ {}
}
func BenchmarkGroutine(b *testing.B) {
for i := 0; i < b.N; i++ {
grpool.Add(increment)
}
}
//func Test_GrpoolMemUsage(t *testing.T) {
// for i := 0; i < n; i++ {
// grpool.Add(increment)
// }
// mem := runtime.MemStats{}
// runtime.ReadMemStats(&mem)
// fmt.Println("mem usage:", mem.TotalAlloc/1024)
//}
func BenchmarkGoRoutine(b *testing.B) {
for i := 0; i < b.N; i++ {
func Test_GroroutineMemUsage(t *testing.T) {
for i := 0; i < n; i++ {
go increment()
}
}
mem := runtime.MemStats{}
runtime.ReadMemStats(&mem)
fmt.Println("mem usage:", mem.TotalAlloc/1024)
}
//func BenchmarkGrpool(b *testing.B) {
// b.N = n
// for i := 0; i < b.N; i++ {
// grpool.Add(increment)
// }
//}
//func BenchmarkGoroutine(b *testing.B) {
// b.N = n
// for i := 0; i < b.N; i++ {
// go increment()
// }
//}