github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/grpc/benchmark/primitives/context_test.go (about) 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 20 21 import ( 22 "context" 23 "testing" 24 "time" 25 ) 26 27 const defaultTestTimeout = 10 * time.Second 28 29 func BenchmarkCancelContextErrNoErr(b *testing.B) { 30 ctx, cancel := context.WithCancel(context.Background()) 31 for i := 0; i < b.N; i++ { 32 if err := ctx.Err(); err != nil { 33 b.Fatal("error") 34 } 35 } 36 cancel() 37 } 38 39 func BenchmarkCancelContextErrGotErr(b *testing.B) { 40 ctx, cancel := context.WithCancel(context.Background()) 41 cancel() 42 for i := 0; i < b.N; i++ { 43 if err := ctx.Err(); err == nil { 44 b.Fatal("error") 45 } 46 } 47 } 48 49 func BenchmarkCancelContextChannelNoErr(b *testing.B) { 50 ctx, cancel := context.WithCancel(context.Background()) 51 for i := 0; i < b.N; i++ { 52 select { 53 case <-ctx.Done(): 54 b.Fatal("error: ctx.Done():", ctx.Err()) 55 default: 56 } 57 } 58 cancel() 59 } 60 61 func BenchmarkCancelContextChannelGotErr(b *testing.B) { 62 ctx, cancel := context.WithCancel(context.Background()) 63 cancel() 64 for i := 0; i < b.N; i++ { 65 select { 66 case <-ctx.Done(): 67 if err := ctx.Err(); err == nil { 68 b.Fatal("error") 69 } 70 default: 71 b.Fatal("error: !ctx.Done()") 72 } 73 } 74 } 75 76 func BenchmarkTimerContextErrNoErr(b *testing.B) { 77 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 78 for i := 0; i < b.N; i++ { 79 if err := ctx.Err(); err != nil { 80 b.Fatal("error") 81 } 82 } 83 cancel() 84 } 85 86 func BenchmarkTimerContextErrGotErr(b *testing.B) { 87 ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond) 88 cancel() 89 for i := 0; i < b.N; i++ { 90 if err := ctx.Err(); err == nil { 91 b.Fatal("error") 92 } 93 } 94 } 95 96 func BenchmarkTimerContextChannelNoErr(b *testing.B) { 97 ctx, cancel := context.WithTimeout(context.Background(), defaultTestTimeout) 98 for i := 0; i < b.N; i++ { 99 select { 100 case <-ctx.Done(): 101 b.Fatal("error: ctx.Done():", ctx.Err()) 102 default: 103 } 104 } 105 cancel() 106 } 107 108 func BenchmarkTimerContextChannelGotErr(b *testing.B) { 109 ctx, cancel := context.WithTimeout(context.Background(), time.Microsecond) 110 cancel() 111 for i := 0; i < b.N; i++ { 112 select { 113 case <-ctx.Done(): 114 if err := ctx.Err(); err == nil { 115 b.Fatal("error") 116 } 117 default: 118 b.Fatal("error: !ctx.Done()") 119 } 120 } 121 } 122 123 type ctxKey struct{} 124 125 func newContextWithLocalKey(parent context.Context) context.Context { 126 return context.WithValue(parent, ctxKey{}, nil) 127 } 128 129 var ck = ctxKey{} 130 131 func newContextWithGlobalKey(parent context.Context) context.Context { 132 return context.WithValue(parent, ck, nil) 133 } 134 135 func BenchmarkContextWithValue(b *testing.B) { 136 benches := []struct { 137 name string 138 f func(context.Context) context.Context 139 }{ 140 {"newContextWithLocalKey", newContextWithLocalKey}, 141 {"newContextWithGlobalKey", newContextWithGlobalKey}, 142 } 143 144 pCtx := context.Background() 145 for _, bench := range benches { 146 b.Run(bench.name, func(b *testing.B) { 147 for j := 0; j < b.N; j++ { 148 bench.f(pCtx) 149 } 150 }) 151 } 152 }