mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
groutine性能测试
This commit is contained in:
@ -15,8 +15,9 @@ import (
|
||||
|
||||
// goroutine池对象
|
||||
type Pool struct {
|
||||
queue *glist.SafeList // 空闲任务队列*PoolJob)
|
||||
pjobs *gset.InterfaceSet // 当前任务对象(*PoolJob)
|
||||
jobs *gset.InterfaceSet // 当前任务对象(*PoolJob)
|
||||
queue *glist.SafeList // 空闲任务队列(*PoolJob)
|
||||
funcs []chan func() // 待处理任务操作
|
||||
}
|
||||
|
||||
// goroutine任务
|
||||
@ -26,6 +27,19 @@ type PoolJob struct {
|
||||
pool *Pool // 所属池
|
||||
}
|
||||
|
||||
// 任务分配循环
|
||||
func (p *Pool) loop() {
|
||||
go func() {
|
||||
for {
|
||||
if f := <- p.funcs; f != nil {
|
||||
p.getJob().setJob(f)
|
||||
} else {
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// 创建一个空的任务对象
|
||||
func (p *Pool) newJob() *PoolJob {
|
||||
j := &PoolJob {
|
||||
@ -33,7 +47,7 @@ func (p *Pool) newJob() *PoolJob {
|
||||
pool : p,
|
||||
}
|
||||
j.start()
|
||||
p.pjobs.Add(j)
|
||||
p.jobs.Add(j)
|
||||
return j
|
||||
}
|
||||
|
||||
|
||||
@ -13,20 +13,24 @@ import (
|
||||
|
||||
// 创建goroutine池管理对象
|
||||
func New() *Pool {
|
||||
return &Pool {
|
||||
p := &Pool {
|
||||
jobs : gset.NewInterfaceSet(),
|
||||
queue : glist.NewSafeList(),
|
||||
pjobs : gset.NewInterfaceSet(),
|
||||
funcs : make(chan func(), 1000000),
|
||||
}
|
||||
p.loop()
|
||||
return p
|
||||
}
|
||||
|
||||
// 添加异步任务
|
||||
func (p *Pool) Add(f func()) {
|
||||
p.getJob().setJob(f)
|
||||
p.funcs <- f
|
||||
}
|
||||
|
||||
// 关闭池,所有的任务将会停止,此后继续添加的任务将不会被执行
|
||||
func (p *Pool) Close() {
|
||||
p.pjobs.Iterator(func(v interface{}){
|
||||
p.funcs <- nil
|
||||
p.jobs.Iterator(func(v interface{}){
|
||||
v.(*PoolJob).stop()
|
||||
})
|
||||
}
|
||||
@ -13,6 +13,8 @@ func (j *PoolJob) start() {
|
||||
if f := <- j.job; f != nil {
|
||||
// 执行任务
|
||||
f()
|
||||
// 清空任务(GC可回收f对应资源)
|
||||
j.job = nil
|
||||
// 执行完毕后添加到空闲队列
|
||||
j.pool.addJob(j)
|
||||
} else {
|
||||
|
||||
31
g/os/groutine/groutine_test.go
Normal file
31
g/os/groutine/groutine_test.go
Normal file
@ -0,0 +1,31 @@
|
||||
package groutine_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"gitee.com/johng/gf/g/os/groutine"
|
||||
)
|
||||
|
||||
func test() {
|
||||
num := 0
|
||||
for i := 0; i < 1000000; i++ {
|
||||
num += i
|
||||
}
|
||||
}
|
||||
|
||||
var pool = groutine.New()
|
||||
|
||||
func BenchmarkGroutine(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
pool.Add(test)
|
||||
}
|
||||
//pool.Close()
|
||||
}
|
||||
|
||||
//func BenchmarkGoRoutine(b *testing.B) {
|
||||
// t := gtime.Microsecond()
|
||||
// b.N = 100000
|
||||
// for i := 0; i < b.N; i++ {
|
||||
// go test()
|
||||
// }
|
||||
// fmt.Println("BenchmarkGoRoutine costs:", gtime.Microsecond() - t)
|
||||
//}
|
||||
@ -1,9 +1,20 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gitee.com/johng/gf/g/os/gtime"
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/container/glist"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println(len(make(chan int, 10)))
|
||||
|
||||
t1 := gtime.Microsecond()
|
||||
c := make(chan func(), 10)
|
||||
c <- func(){}
|
||||
fmt.Println(gtime.Microsecond() - t1)
|
||||
|
||||
t2 := gtime.Microsecond()
|
||||
l := glist.NewSafeList()
|
||||
l.PushBack(func() {})
|
||||
fmt.Println(gtime.Microsecond() - t2)
|
||||
}
|
||||
Reference in New Issue
Block a user