mirror of
https://gitee.com/johng/gf
synced 2026-06-30 11:05:11 +08:00
完善gqueue基准测试
This commit is contained in:
@ -14,7 +14,8 @@ import (
|
||||
)
|
||||
|
||||
var length = 10000000
|
||||
var q = gqueue.New(length)
|
||||
var q = gqueue.New(length)
|
||||
var c = make(chan int, length)
|
||||
|
||||
func BenchmarkGqueueNew1000W(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
@ -22,6 +23,12 @@ func BenchmarkGqueueNew1000W(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkChannelNew1000W(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c = make(chan int, length)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkGqueuePush(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
q.PushBack(i)
|
||||
@ -35,3 +42,10 @@ func BenchmarkGqueuePushAndPop(b *testing.B) {
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkChannelPushAndPop(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
c <- i
|
||||
<- c
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"gitee.com/johng/gf/g/os/gtime"
|
||||
"gitee.com/johng/gf/g/container/gqueue"
|
||||
"time"
|
||||
)
|
||||
|
||||
func main() {
|
||||
t := gtime.Microsecond()
|
||||
q := gqueue.NewInterfaceQueue()
|
||||
fmt.Println("queue creation costs(μs):", gtime.Microsecond() - t)
|
||||
|
||||
// 每隔2秒异步打印出当前队列的大小
|
||||
gtime.SetInterval(2*time.Second, func() bool {
|
||||
fmt.Println("queue size:", q.Size())
|
||||
return true
|
||||
})
|
||||
|
||||
// push10条数据
|
||||
for i := 0; i < 10; i++ {
|
||||
q.Push(i)
|
||||
fmt.Println("push:", i)
|
||||
}
|
||||
|
||||
// 每隔1秒pop1条数据
|
||||
for {
|
||||
time.Sleep(time.Second)
|
||||
fmt.Println(" pop:", q.Pop())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user