map_test.go 936 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package proto_test
  2. import (
  3. "fmt"
  4. "testing"
  5. "github.com/golang/protobuf/proto"
  6. ppb "github.com/golang/protobuf/proto/proto3_proto"
  7. )
  8. func marshalled() []byte {
  9. m := &ppb.IntMaps{}
  10. for i := 0; i < 1000; i++ {
  11. m.Maps = append(m.Maps, &ppb.IntMap{
  12. Rtt: map[int32]int32{1: 2},
  13. })
  14. }
  15. b, err := proto.Marshal(m)
  16. if err != nil {
  17. panic(fmt.Sprintf("Can't marshal %+v: %v", m, err))
  18. }
  19. return b
  20. }
  21. func BenchmarkConcurrentMapUnmarshal(b *testing.B) {
  22. in := marshalled()
  23. b.RunParallel(func(pb *testing.PB) {
  24. for pb.Next() {
  25. var out ppb.IntMaps
  26. if err := proto.Unmarshal(in, &out); err != nil {
  27. b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
  28. }
  29. }
  30. })
  31. }
  32. func BenchmarkSequentialMapUnmarshal(b *testing.B) {
  33. in := marshalled()
  34. b.ResetTimer()
  35. for i := 0; i < b.N; i++ {
  36. var out ppb.IntMaps
  37. if err := proto.Unmarshal(in, &out); err != nil {
  38. b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
  39. }
  40. }
  41. }