Files
bl/common/utils/lockfree-1.1.3/lockfree_test.go
昔念 269256a861 feat(common): 添加无锁并发工具包依赖
新增 lockfree-1.1.3 工具包到 go.work 文件中,为项目提供无锁并发数据结构支持,
提升高并发场景下的性能表现。
2025-12-05 00:36:28 +08:00

104 lines
1.9 KiB
Go

/*
* Copyright (C) THL A29 Limited, a Tencent company. All rights reserved.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
package lockfree
import (
"sync"
"sync/atomic"
"testing"
"time"
)
func BenchmarkLockFree(b *testing.B) {
var (
counter = uint64(0)
)
eh := &longEventHandler[uint64]{}
disruptor := NewLockfree[uint64](1024*1024, eh, &SleepBlockStrategy{
t: time.Microsecond,
})
disruptor.Start()
producer := disruptor.Producer()
var wg sync.WaitGroup
wg.Add(b.N)
b.ResetTimer()
for i := 0; i < b.N; i++ {
go func() {
for j := 0; j < SchPerGo; j++ {
x := atomic.AddUint64(&counter, 1)
err := producer.Write(x)
if err != nil {
panic(err)
}
}
wg.Done()
}()
}
wg.Wait()
b.StopTimer()
time.Sleep(time.Second * 1)
disruptor.Close()
}
func TestChanBlockStrategy(t *testing.T) {
var (
counter = uint64(0)
goS = 1000
perGo = 100
)
eh := &longEventHandler[uint64]{}
disruptor := NewLockfree[uint64](2, eh, NewChanBlockStrategy())
disruptor.Start()
producer := disruptor.Producer()
var wg sync.WaitGroup
wg.Add(goS)
for i := 0; i < goS; i++ {
go func() {
for j := 0; j < perGo; j++ {
x := atomic.AddUint64(&counter, 1)
err := producer.Write(x)
if err != nil {
panic(err)
}
}
wg.Done()
}()
}
wg.Wait()
time.Sleep(time.Second * 1)
disruptor.Close()
}
func TestCondBlockStrategy(t *testing.T) {
var (
counter = uint64(0)
goS = 1000
perGo = 100
)
eh := &longEventHandler[uint64]{}
disruptor := NewLockfree[uint64](2, eh, NewConditionBlockStrategy())
disruptor.Start()
producer := disruptor.Producer()
var wg sync.WaitGroup
wg.Add(goS)
for i := 0; i < goS; i++ {
go func() {
for j := 0; j < perGo; j++ {
x := atomic.AddUint64(&counter, 1)
err := producer.Write(x)
if err != nil {
panic(err)
}
}
wg.Done()
}()
}
wg.Wait()
time.Sleep(time.Second * 1)
disruptor.Close()
}