test: concurrent.Pool 增加测试用例
This commit is contained in:
parent
3877b28baa
commit
8f4e65219e
|
@ -0,0 +1,26 @@
|
||||||
|
package concurrent_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/kercylan98/minotaur/utils/concurrent"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func BenchmarkPool_Get2Put(b *testing.B) {
|
||||||
|
var pool = concurrent.NewPool[map[string]int](func() *map[string]int {
|
||||||
|
return &map[string]int{}
|
||||||
|
}, func(data *map[string]int) {
|
||||||
|
for k := range *data {
|
||||||
|
delete(*data, k)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
b.ResetTimer()
|
||||||
|
b.RunParallel(func(pb *testing.PB) {
|
||||||
|
for pb.Next() {
|
||||||
|
msg := pool.Get()
|
||||||
|
pool.Release(msg)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
b.StopTimer()
|
||||||
|
b.ReportAllocs()
|
||||||
|
}
|
|
@ -1,32 +1 @@
|
||||||
package concurrent_test
|
package concurrent_test
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/kercylan98/minotaur/utils/concurrent"
|
|
||||||
"github.com/kercylan98/minotaur/utils/times"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestPool_EAC(t *testing.T) {
|
|
||||||
var p = concurrent.NewPool[int](10, func() int {
|
|
||||||
return 0
|
|
||||||
}, func(data int) {
|
|
||||||
})
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
for i := 0; i < 1000; i++ {
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
p.Release(p.Get())
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
time.Sleep(2 * time.Second)
|
|
||||||
p.EAC(2048)
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(100 * times.Day)
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue