-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathlockfree_test.go
104 lines (99 loc) · 1.88 KB
/
lockfree_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* 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()
}