-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
control_test.go
194 lines (169 loc) · 2.79 KB
/
control_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gocostmodel
import (
"math"
"testing"
)
var (
x int
val = 1
rng = []int{1}
blockedCh = make(chan bool)
closedCh = make(chan bool)
fullBufCh = make(chan bool, 1)
)
func init() {
close(closedCh)
fullBufCh <- true
}
func BenchmarkSwitch(b *testing.B) {
for i := 0; i < b.N; i++ {
switch val {
case 0:
x++
case 1:
x--
default:
x += 2
}
}
}
func BenchmarkIfEquals(b *testing.B) {
for i := 0; i < b.N; i++ {
if val == 1 {
x++
}
}
}
func BenchmarkIfNotEquals(b *testing.B) {
for i := 0; i < b.N; i++ {
if val != 5 {
x++
}
}
}
func BenchmarkFor1(b *testing.B) {
for i := 0; i < b.N; i++ {
for val < 0 {
x++
}
}
}
func BenchmarkFor3(b *testing.B) {
for i := 0; i < b.N; i++ {
// loop only once, the goal is to measure the loop construct
for j := 0; j < val; j++ {
x++
}
}
}
func BenchmarkForRange(b *testing.B) {
for i := 0; i < b.N; i++ {
// do not use empty range, would force go1.4
for _ = range rng {
x++
}
}
}
func BenchmarkForRangeClosedChan(b *testing.B) {
for i := 0; i < b.N; i++ {
// do not use empty range, would force go1.4
for _ = range closedCh {
x++
}
}
}
func BenchmarkSelectBlockedDefault(b *testing.B) {
for i := 0; i < b.N; i++ {
select {
case <-blockedCh:
x--
default:
x++
}
}
}
func BenchmarkSelectBlockedClosed(b *testing.B) {
for i := 0; i < b.N; i++ {
select {
case <-blockedCh:
x--
case <-closedCh:
x++
}
}
}
func BenchmarkSelectTrySend(b *testing.B) {
for i := 0; i < b.N; i++ {
select {
case blockedCh <- false:
default:
x++
}
}
}
func BenchmarkPanicRecover(b *testing.B) {
for i := 0; i < b.N; i++ {
panicRecover()
}
}
func panicRecover() {
defer func() {
// a simple call to recover would be enough, but the usual way
// this pattern is used is to do something conditionnally if there
// was a panic.
if e := recover(); e != nil {
x++
}
}()
panic("zomg!")
}
func BenchmarkSelectTrySendBuf(b *testing.B) {
for i := 0; i < b.N; i++ {
select {
case fullBufCh <- false:
default:
x++
}
}
}
func BenchmarkFunc1(b *testing.B) {
for i := 0; i < b.N; i++ {
x = sum1(i1)
}
}
func BenchmarkFunc2(b *testing.B) {
for i := 0; i < b.N; i++ {
x = sum2(i1, i2)
}
}
func BenchmarkFunc3(b *testing.B) {
for i := 0; i < b.N; i++ {
x = sum3(i1, i2, i3)
}
}
func sum1(n int) int {
if n > 0 {
return n
}
// instructions just to avoid inlining
math.Pow(float64(n), 1)
panic("unreachable")
}
func sum2(n1, n2 int) int {
if n1 > 0 {
x := n1 + n2
return x
}
// instructions just to avoid inlining
math.Pow(float64(n1), 1)
panic("unreachable")
}
func sum3(n1, n2, n3 int) int {
if n1 > 0 {
x := n1 + n2 + n3
return x
}
// instructions just to avoid inlining
math.Pow(float64(n1), 1)
panic("unreachable")
}