gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/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  	"gitee.com/ks-custle/core-gm/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  	b.ResetTimer()
    91  	for i := 0; i < b.N; i++ {
    92  		c := codeBench(uint32(i % 17))
    93  		_ = c.String()
    94  	}
    95  	b.StopTimer()
    96  }
    97  
    98  func BenchmarkCodeStringMap(b *testing.B) {
    99  	b.ResetTimer()
   100  	for i := 0; i < b.N; i++ {
   101  		c := codeBench(uint32(i % 17))
   102  		_ = c.StringUsingMap()
   103  	}
   104  	b.StopTimer()
   105  }
   106  
   107  // codes.Code.String() does a switch.
   108  func BenchmarkCodeStringSwitch(b *testing.B) {
   109  	b.ResetTimer()
   110  	for i := 0; i < b.N; i++ {
   111  		c := codes.Code(uint32(i % 17))
   112  		_ = c.String()
   113  	}
   114  	b.StopTimer()
   115  }
   116  
   117  // Testing all codes (0<=c<=16) and also one overflow (17).
   118  func BenchmarkCodeStringStringerWithOverflow(b *testing.B) {
   119  	b.ResetTimer()
   120  	for i := 0; i < b.N; i++ {
   121  		c := codeBench(uint32(i % 18))
   122  		_ = c.String()
   123  	}
   124  	b.StopTimer()
   125  }
   126  
   127  // Testing all codes (0<=c<=16) and also one overflow (17).
   128  func BenchmarkCodeStringSwitchWithOverflow(b *testing.B) {
   129  	b.ResetTimer()
   130  	for i := 0; i < b.N; i++ {
   131  		c := codes.Code(uint32(i % 18))
   132  		_ = c.String()
   133  	}
   134  	b.StopTimer()
   135  }