-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpooled_stack_bench_test.go
59 lines (56 loc) · 1.53 KB
/
pooled_stack_bench_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
package stack
import (
"fmt"
"sync/atomic"
"testing"
"unsafe"
)
func BenchmarkImmutableStackPushWithPool(b *testing.B) {
numGoroutines := []int{1, 5, 100, 1000, 5000}
for _, curNum := range numGoroutines {
b.Run(fmt.Sprintf("push with %d g", curNum), func(b *testing.B) {
userEditHistory := NewUserEditHistoryImmutable(pooledStackFactory.NewStack())
b.SetParallelism(curNum)
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
success := false
for !success {
oldVal := userEditHistory.history
newVal := (*(*Stack)(oldVal)).Push(1)
success = atomic.CompareAndSwapPointer(&userEditHistory.history, oldVal, unsafe.Pointer(&newVal))
if !success {
nonEmptyStackPool.Put(newVal)
}
}
}
})
})
}
}
func BenchmarkImmutablePooledStackPushAndTop(b *testing.B) {
numGoroutines := []int{1, 5, 100, 1000, 5000}
var n uint64 = 0
for _, curNum := range numGoroutines {
h := NewUserEditHistoryImmutable(pooledStackFactory.NewStack())
b.SetParallelism(curNum)
b.Run(fmt.Sprintf("push with %d g", curNum), func(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
if atomic.AddUint64(&n, 1)%2 == 0 {
(*(*Stack)(h.history)).Top()
} else {
success := false
for !success {
oldVal := h.history
newVal := (*(*Stack)(oldVal)).Push(1)
success = atomic.CompareAndSwapPointer(&h.history, oldVal, unsafe.Pointer(&newVal))
if !success {
nonEmptyStackPool.Put(newVal)
}
}
}
}
})
})
}
}