...
1
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
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
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
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
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 }
126
View as plain text