encode_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2010 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package proto_test
  5. import (
  6. "strconv"
  7. "testing"
  8. "github.com/golang/protobuf/proto"
  9. tpb "github.com/golang/protobuf/proto/proto3_proto"
  10. "github.com/golang/protobuf/ptypes"
  11. )
  12. var (
  13. blackhole []byte
  14. )
  15. // BenchmarkAny creates increasingly large arbitrary Any messages. The type is always the
  16. // same.
  17. func BenchmarkAny(b *testing.B) {
  18. data := make([]byte, 1<<20)
  19. quantum := 1 << 10
  20. for i := uint(0); i <= 10; i++ {
  21. b.Run(strconv.Itoa(quantum<<i), func(b *testing.B) {
  22. for k := 0; k < b.N; k++ {
  23. inner := &tpb.Message{
  24. Data: data[:quantum<<i],
  25. }
  26. outer, err := ptypes.MarshalAny(inner)
  27. if err != nil {
  28. b.Error("wrong encode", err)
  29. }
  30. raw, err := proto.Marshal(&tpb.Message{
  31. Anything: outer,
  32. })
  33. if err != nil {
  34. b.Error("wrong encode", err)
  35. }
  36. blackhole = raw
  37. }
  38. })
  39. }
  40. }
  41. // BenchmarkEmpy measures the overhead of doing the minimal possible encode.
  42. func BenchmarkEmpy(b *testing.B) {
  43. for i := 0; i < b.N; i++ {
  44. raw, err := proto.Marshal(&tpb.Message{})
  45. if err != nil {
  46. b.Error("wrong encode", err)
  47. }
  48. blackhole = raw
  49. }
  50. }