map_test.go 1.7 KB

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