-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathatomiccontext_test.go
160 lines (129 loc) · 3.38 KB
/
atomiccontext_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package safetyfast
import (
"fmt"
"math/rand"
"runtime"
"sync"
"testing"
"time"
"github.com/intel-go/cpuid"
)
func TestLockedContext(t *testing.T) {
run := func(t *testing.T, lock sync.Locker) {
const numConcurGoRoutines = 8
const arrayLength = 100
const numIterations = 5000000
oldmaxprocs := runtime.GOMAXPROCS(numConcurGoRoutines)
var wg sync.WaitGroup
var arr = make([]int, arrayLength)
var c = NewLockedContext(lock)
routine := func() {
randsrc := rand.NewSource(int64(time.Now().Second()))
r := rand.New(randsrc)
for i := 0; i < numIterations; i++ {
index := r.Int() % len(arr)
c.Atomic(func() {
arr[index]++
})
}
wg.Done()
}
wg.Add(numConcurGoRoutines)
for i := 0; i < numConcurGoRoutines; i++ {
go routine()
}
wg.Wait()
var sum int
for _, v := range arr {
sum += v
}
expected := numIterations * numConcurGoRoutines
t.Logf("Array Length=%d | NumberGoRoutines=%d | NumIterations=%d", len(arr), numConcurGoRoutines, numIterations)
t.Logf("ArraySum=%d | Expected=%d", sum, expected)
if sum != expected {
t.Fatalf("Sum result is %d, but we expected %d", sum, expected)
}
runtime.GOMAXPROCS(oldmaxprocs)
}
t.Run("SpinHLEMutex", func(t *testing.T) {
run(t, new(SpinHLEMutex))
})
t.Run("sync.Mutex", func(t *testing.T) {
run(t, new(sync.Mutex))
})
t.Run("SpinMutex", func(t *testing.T) {
run(t, new(SpinMutex))
})
t.Run("SpinMutexASM", func(t *testing.T) {
run(t, new(SpinMutexASM))
})
t.Run("SpinMutexBasic", func(t *testing.T) {
run(t, new(SpinMutexBasic))
})
}
func TestRTMContext(t *testing.T) {
run := func(t *testing.T, lock sync.Locker) {
const numConcurGoRoutines = 8
const arrayLength = 100
const numIterations = 5000000
if !cpuid.HasExtendedFeature(cpuid.RTM) {
// Let's not fail for Travis-CI
fmt.Println("The CPU does not support Intel RTM - Skipping RTM Test!")
t.Log("The CPU does not support Intel RTM - Skipping RTM Test!")
// t.Fatal("The CPU does not support Intel RTM")
return
}
oldmaxprocs := runtime.GOMAXPROCS(numConcurGoRoutines)
var wg sync.WaitGroup
var arr = make([]int, arrayLength)
var c *RTMContext
if lock == nil {
c = NewRTMContexDefault()
} else {
c = NewRTMContex(lock)
}
routine := func() {
randsrc := rand.NewSource(int64(time.Now().Second()))
r := rand.New(randsrc)
for i := 0; i < numIterations; i++ {
index := r.Int() % len(arr)
c.Atomic(func() {
arr[index]++
})
}
wg.Done()
}
wg.Add(numConcurGoRoutines)
for i := 0; i < numConcurGoRoutines; i++ {
go routine()
}
wg.Wait()
var sum int
for _, v := range arr {
sum += v
}
expected := numIterations * numConcurGoRoutines
t.Logf("Array Length=%d | NumberGoRoutines=%d | NumIterations=%d", len(arr), numConcurGoRoutines, numIterations)
t.Logf("ArraySum=%d | Expected=%d", sum, expected)
t.Logf("CapacityAborts=%d", c.CapacityAborts())
if sum != expected {
t.Fatalf("Sum result is %d, but we expected %d", sum, expected)
}
runtime.GOMAXPROCS(oldmaxprocs)
}
t.Run("Default", func(t *testing.T) {
run(t, nil)
})
t.Run("sync.Mutex", func(t *testing.T) {
run(t, new(sync.Mutex))
})
t.Run("SpinMutex", func(t *testing.T) {
run(t, new(SpinMutex))
})
t.Run("SpinMutexASM", func(t *testing.T) {
run(t, new(SpinMutexASM))
})
t.Run("SpinMutexBasic", func(t *testing.T) {
run(t, new(SpinMutexBasic))
})
}