OSDN Git Service

new repo
[bytom/vapor.git] / vendor / google.golang.org / grpc / benchmark / primitives / primitives_test.go
1 /*
2  *
3  * Copyright 2017 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 // Package primitives_test contains benchmarks for various synchronization primitives
20 // available in Go.
21 package primitives_test
22
23 import (
24         "sync"
25         "sync/atomic"
26         "testing"
27 )
28
29 func BenchmarkSelectClosed(b *testing.B) {
30         c := make(chan struct{})
31         close(c)
32         x := 0
33         b.ResetTimer()
34         for i := 0; i < b.N; i++ {
35                 select {
36                 case <-c:
37                         x++
38                 default:
39                 }
40         }
41         b.StopTimer()
42         if x != b.N {
43                 b.Fatal("error")
44         }
45 }
46
47 func BenchmarkSelectOpen(b *testing.B) {
48         c := make(chan struct{})
49         x := 0
50         b.ResetTimer()
51         for i := 0; i < b.N; i++ {
52                 select {
53                 case <-c:
54                 default:
55                         x++
56                 }
57         }
58         b.StopTimer()
59         if x != b.N {
60                 b.Fatal("error")
61         }
62 }
63
64 func BenchmarkAtomicBool(b *testing.B) {
65         c := int32(0)
66         x := 0
67         b.ResetTimer()
68         for i := 0; i < b.N; i++ {
69                 if atomic.LoadInt32(&c) == 0 {
70                         x++
71                 }
72         }
73         b.StopTimer()
74         if x != b.N {
75                 b.Fatal("error")
76         }
77 }
78
79 func BenchmarkAtomicValue(b *testing.B) {
80         c := atomic.Value{}
81         c.Store(0)
82         x := 0
83         b.ResetTimer()
84         for i := 0; i < b.N; i++ {
85                 if c.Load().(int) == 0 {
86                         x++
87                 }
88         }
89         b.StopTimer()
90         if x != b.N {
91                 b.Fatal("error")
92         }
93 }
94
95 func BenchmarkMutex(b *testing.B) {
96         c := sync.Mutex{}
97         x := 0
98         b.ResetTimer()
99         for i := 0; i < b.N; i++ {
100                 c.Lock()
101                 x++
102                 c.Unlock()
103         }
104         b.StopTimer()
105         if x != b.N {
106                 b.Fatal("error")
107         }
108 }
109
110 func BenchmarkRWMutex(b *testing.B) {
111         c := sync.RWMutex{}
112         x := 0
113         b.ResetTimer()
114         for i := 0; i < b.N; i++ {
115                 c.RLock()
116                 x++
117                 c.RUnlock()
118         }
119         b.StopTimer()
120         if x != b.N {
121                 b.Fatal("error")
122         }
123 }
124
125 func BenchmarkRWMutexW(b *testing.B) {
126         c := sync.RWMutex{}
127         x := 0
128         b.ResetTimer()
129         for i := 0; i < b.N; i++ {
130                 c.Lock()
131                 x++
132                 c.Unlock()
133         }
134         b.StopTimer()
135         if x != b.N {
136                 b.Fatal("error")
137         }
138 }
139
140 func BenchmarkMutexWithDefer(b *testing.B) {
141         c := sync.Mutex{}
142         x := 0
143         b.ResetTimer()
144         for i := 0; i < b.N; i++ {
145                 func() {
146                         c.Lock()
147                         defer c.Unlock()
148                         x++
149                 }()
150         }
151         b.StopTimer()
152         if x != b.N {
153                 b.Fatal("error")
154         }
155 }
156
157 func BenchmarkMutexWithClosureDefer(b *testing.B) {
158         c := sync.Mutex{}
159         x := 0
160         b.ResetTimer()
161         for i := 0; i < b.N; i++ {
162                 func() {
163                         c.Lock()
164                         defer func() { c.Unlock() }()
165                         x++
166                 }()
167         }
168         b.StopTimer()
169         if x != b.N {
170                 b.Fatal("error")
171         }
172 }
173
174 func BenchmarkMutexWithoutDefer(b *testing.B) {
175         c := sync.Mutex{}
176         x := 0
177         b.ResetTimer()
178         for i := 0; i < b.N; i++ {
179                 func() {
180                         c.Lock()
181                         x++
182                         c.Unlock()
183                 }()
184         }
185         b.StopTimer()
186         if x != b.N {
187                 b.Fatal("error")
188         }
189 }
190
191 type myFooer struct{}
192
193 func (myFooer) Foo() {}
194
195 type fooer interface {
196         Foo()
197 }
198
199 func BenchmarkInterfaceTypeAssertion(b *testing.B) {
200         // Call a separate function to avoid compiler optimizations.
201         runInterfaceTypeAssertion(b, myFooer{})
202 }
203
204 func runInterfaceTypeAssertion(b *testing.B, fer interface{}) {
205         x := 0
206         b.ResetTimer()
207         for i := 0; i < b.N; i++ {
208                 if _, ok := fer.(fooer); ok {
209                         x++
210                 }
211         }
212         b.StopTimer()
213         if x != b.N {
214                 b.Fatal("error")
215         }
216 }
217
218 func BenchmarkStructTypeAssertion(b *testing.B) {
219         // Call a separate function to avoid compiler optimizations.
220         runStructTypeAssertion(b, myFooer{})
221 }
222
223 func runStructTypeAssertion(b *testing.B, fer interface{}) {
224         x := 0
225         b.ResetTimer()
226         for i := 0; i < b.N; i++ {
227                 if _, ok := fer.(myFooer); ok {
228                         x++
229                 }
230         }
231         b.StopTimer()
232         if x != b.N {
233                 b.Fatal("error")
234         }
235 }