google.golang.org/grpc@v1.62.1/benchmark/primitives/code_string_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 "strconv" 23 "testing" 24 25 "google.golang.org/grpc/codes" 26 ) 27 28 type codeBench uint32 29 30 const ( 31 OK codeBench = iota 32 Canceled 33 Unknown 34 InvalidArgument 35 DeadlineExceeded 36 NotFound 37 AlreadyExists 38 PermissionDenied 39 ResourceExhausted 40 FailedPrecondition 41 Aborted 42 OutOfRange 43 Unimplemented 44 Internal 45 Unavailable 46 DataLoss 47 Unauthenticated 48 ) 49 50 // The following String() function was generated by stringer. 51 const _Code_name = "OKCanceledUnknownInvalidArgumentDeadlineExceededNotFoundAlreadyExistsPermissionDeniedResourceExhaustedFailedPreconditionAbortedOutOfRangeUnimplementedInternalUnavailableDataLossUnauthenticated" 52 53 var _Code_index = [...]uint8{0, 2, 10, 17, 32, 48, 56, 69, 85, 102, 120, 127, 137, 150, 158, 169, 177, 192} 54 55 func (i codeBench) String() string { 56 if i >= codeBench(len(_Code_index)-1) { 57 return "Code(" + strconv.FormatInt(int64(i), 10) + ")" 58 } 59 return _Code_name[_Code_index[i]:_Code_index[i+1]] 60 } 61 62 var nameMap = map[codeBench]string{ 63 OK: "OK", 64 Canceled: "Canceled", 65 Unknown: "Unknown", 66 InvalidArgument: "InvalidArgument", 67 DeadlineExceeded: "DeadlineExceeded", 68 NotFound: "NotFound", 69 AlreadyExists: "AlreadyExists", 70 PermissionDenied: "PermissionDenied", 71 ResourceExhausted: "ResourceExhausted", 72 FailedPrecondition: "FailedPrecondition", 73 Aborted: "Aborted", 74 OutOfRange: "OutOfRange", 75 Unimplemented: "Unimplemented", 76 Internal: "Internal", 77 Unavailable: "Unavailable", 78 DataLoss: "DataLoss", 79 Unauthenticated: "Unauthenticated", 80 } 81 82 func (i codeBench) StringUsingMap() string { 83 if s, ok := nameMap[i]; ok { 84 return s 85 } 86 return "Code(" + strconv.FormatInt(int64(i), 10) + ")" 87 } 88 89 func BenchmarkCodeStringStringer(b *testing.B) { 90 for i := 0; i < b.N; i++ { 91 c := codeBench(uint32(i % 17)) 92 _ = c.String() 93 } 94 } 95 96 func BenchmarkCodeStringMap(b *testing.B) { 97 for i := 0; i < b.N; i++ { 98 c := codeBench(uint32(i % 17)) 99 _ = c.StringUsingMap() 100 } 101 } 102 103 // codes.Code.String() does a switch. 104 func BenchmarkCodeStringSwitch(b *testing.B) { 105 for i := 0; i < b.N; i++ { 106 c := codes.Code(uint32(i % 17)) 107 _ = c.String() 108 } 109 } 110 111 // Testing all codes (0<=c<=16) and also one overflow (17). 112 func BenchmarkCodeStringStringerWithOverflow(b *testing.B) { 113 for i := 0; i < b.N; i++ { 114 c := codeBench(uint32(i % 18)) 115 _ = c.String() 116 } 117 } 118 119 // Testing all codes (0<=c<=16) and also one overflow (17). 120 func BenchmarkCodeStringSwitchWithOverflow(b *testing.B) { 121 for i := 0; i < b.N; i++ { 122 c := codes.Code(uint32(i % 18)) 123 _ = c.String() 124 } 125 }