Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

136 linhas
3.3 KiB

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