You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

99 lines
2.8 KiB

  1. /*
  2. *
  3. * Copyright 2014 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 proto
  19. import (
  20. "fmt"
  21. "testing"
  22. "github.com/golang/protobuf/proto"
  23. "google.golang.org/grpc/encoding"
  24. "google.golang.org/grpc/test/codec_perf"
  25. )
  26. func setupBenchmarkProtoCodecInputs(payloadBaseSize uint32) []proto.Message {
  27. payloadBase := make([]byte, payloadBaseSize)
  28. // arbitrary byte slices
  29. payloadSuffixes := [][]byte{
  30. []byte("one"),
  31. []byte("two"),
  32. []byte("three"),
  33. []byte("four"),
  34. []byte("five"),
  35. }
  36. protoStructs := make([]proto.Message, 0)
  37. for _, p := range payloadSuffixes {
  38. ps := &codec_perf.Buffer{}
  39. ps.Body = append(payloadBase, p...)
  40. protoStructs = append(protoStructs, ps)
  41. }
  42. return protoStructs
  43. }
  44. // The possible use of certain protobuf APIs like the proto.Buffer API potentially involves caching
  45. // on our side. This can add checks around memory allocations and possible contention.
  46. // Example run: go test -v -run=^$ -bench=BenchmarkProtoCodec -benchmem
  47. func BenchmarkProtoCodec(b *testing.B) {
  48. // range of message sizes
  49. payloadBaseSizes := make([]uint32, 0)
  50. for i := uint32(0); i <= 12; i += 4 {
  51. payloadBaseSizes = append(payloadBaseSizes, 1<<i)
  52. }
  53. // range of SetParallelism
  54. parallelisms := make([]int, 0)
  55. for i := uint32(0); i <= 16; i += 4 {
  56. parallelisms = append(parallelisms, int(1<<i))
  57. }
  58. for _, s := range payloadBaseSizes {
  59. for _, p := range parallelisms {
  60. protoStructs := setupBenchmarkProtoCodecInputs(s)
  61. name := fmt.Sprintf("MinPayloadSize:%v/SetParallelism(%v)", s, p)
  62. b.Run(name, func(b *testing.B) {
  63. codec := &codec{}
  64. b.SetParallelism(p)
  65. b.RunParallel(func(pb *testing.PB) {
  66. benchmarkProtoCodec(codec, protoStructs, pb, b)
  67. })
  68. })
  69. }
  70. }
  71. }
  72. func benchmarkProtoCodec(codec *codec, protoStructs []proto.Message, pb *testing.PB, b *testing.B) {
  73. counter := 0
  74. for pb.Next() {
  75. counter++
  76. ps := protoStructs[counter%len(protoStructs)]
  77. fastMarshalAndUnmarshal(codec, ps, b)
  78. }
  79. }
  80. func fastMarshalAndUnmarshal(codec encoding.Codec, protoStruct proto.Message, b *testing.B) {
  81. marshaledBytes, err := codec.Marshal(protoStruct)
  82. if err != nil {
  83. b.Errorf("codec.Marshal(_) returned an error")
  84. }
  85. res := codec_perf.Buffer{}
  86. if err := codec.Unmarshal(marshaledBytes, &res); err != nil {
  87. b.Errorf("codec.Unmarshal(_) returned an error")
  88. }
  89. }