map_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package proto_test
  2. import (
  3. "fmt"
  4. "reflect"
  5. "testing"
  6. "github.com/golang/protobuf/proto"
  7. ppb "github.com/golang/protobuf/proto/proto3_proto"
  8. )
  9. func TestMap(t *testing.T) {
  10. var b []byte
  11. fmt.Sscanf("a2010c0a044b657931120456616c31a201130a044b657932120556616c3261120456616c32a201240a044b6579330d05000000120556616c33621a0556616c3361120456616c331505000000a20100a201260a044b657934130a07536f6d6555524c1209536f6d655469746c651a08536e69707065743114", "%x", &b)
  12. var m ppb.Message
  13. if err := proto.Unmarshal(b, &m); err != nil {
  14. t.Fatalf("proto.Unmarshal error: %v", err)
  15. }
  16. got := m.StringMap
  17. want := map[string]string{
  18. "": "",
  19. "Key1": "Val1",
  20. "Key2": "Val2",
  21. "Key3": "Val3",
  22. "Key4": "",
  23. }
  24. if !reflect.DeepEqual(got, want) {
  25. t.Errorf("maps differ:\ngot %#v\nwant %#v", got, want)
  26. }
  27. }
  28. func marshalled() []byte {
  29. m := &ppb.IntMaps{}
  30. for i := 0; i < 1000; i++ {
  31. m.Maps = append(m.Maps, &ppb.IntMap{
  32. Rtt: map[int32]int32{1: 2},
  33. })
  34. }
  35. b, err := proto.Marshal(m)
  36. if err != nil {
  37. panic(fmt.Sprintf("Can't marshal %+v: %v", m, err))
  38. }
  39. return b
  40. }
  41. func BenchmarkConcurrentMapUnmarshal(b *testing.B) {
  42. in := marshalled()
  43. b.RunParallel(func(pb *testing.PB) {
  44. for pb.Next() {
  45. var out ppb.IntMaps
  46. if err := proto.Unmarshal(in, &out); err != nil {
  47. b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
  48. }
  49. }
  50. })
  51. }
  52. func BenchmarkSequentialMapUnmarshal(b *testing.B) {
  53. in := marshalled()
  54. b.ResetTimer()
  55. for i := 0; i < b.N; i++ {
  56. var out ppb.IntMaps
  57. if err := proto.Unmarshal(in, &out); err != nil {
  58. b.Errorf("Can't unmarshal ppb.IntMaps: %v", err)
  59. }
  60. }
  61. }