3 * Copyright 2017 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 // Package primitives_test contains benchmarks for various synchronization primitives
21 package primitives_test
29 func BenchmarkSelectClosed(b *testing.B) {
30 c := make(chan struct{})
34 for i := 0; i < b.N; i++ {
47 func BenchmarkSelectOpen(b *testing.B) {
48 c := make(chan struct{})
51 for i := 0; i < b.N; i++ {
64 func BenchmarkAtomicBool(b *testing.B) {
68 for i := 0; i < b.N; i++ {
69 if atomic.LoadInt32(&c) == 0 {
79 func BenchmarkAtomicValue(b *testing.B) {
84 for i := 0; i < b.N; i++ {
85 if c.Load().(int) == 0 {
95 func BenchmarkMutex(b *testing.B) {
99 for i := 0; i < b.N; i++ {
110 func BenchmarkRWMutex(b *testing.B) {
114 for i := 0; i < b.N; i++ {
125 func BenchmarkRWMutexW(b *testing.B) {
129 for i := 0; i < b.N; i++ {
140 func BenchmarkMutexWithDefer(b *testing.B) {
144 for i := 0; i < b.N; i++ {
157 func BenchmarkMutexWithClosureDefer(b *testing.B) {
161 for i := 0; i < b.N; i++ {
164 defer func() { c.Unlock() }()
174 func BenchmarkMutexWithoutDefer(b *testing.B) {
178 for i := 0; i < b.N; i++ {
191 type myFooer struct{}
193 func (myFooer) Foo() {}
195 type fooer interface {
199 func BenchmarkInterfaceTypeAssertion(b *testing.B) {
200 // Call a separate function to avoid compiler optimizations.
201 runInterfaceTypeAssertion(b, myFooer{})
204 func runInterfaceTypeAssertion(b *testing.B, fer interface{}) {
207 for i := 0; i < b.N; i++ {
208 if _, ok := fer.(fooer); ok {
218 func BenchmarkStructTypeAssertion(b *testing.B) {
219 // Call a separate function to avoid compiler optimizations.
220 runStructTypeAssertion(b, myFooer{})
223 func runStructTypeAssertion(b *testing.B, fer interface{}) {
226 for i := 0; i < b.N; i++ {
227 if _, ok := fer.(myFooer); ok {