github.com/chrislusf/greenpack@v3.7.1-0.20170911073826-ad5bd10b7c47+incompatible/msgp/json_test.go (about) 1 package msgp 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "reflect" 7 "testing" 8 ) 9 10 func TestCopyJSON(t *testing.T) { 11 var buf bytes.Buffer 12 enc := NewWriter(&buf) 13 enc.WriteMapHeader(5) 14 15 enc.WriteString("thing_1") 16 enc.WriteString("a string object") 17 18 enc.WriteString("a_map") 19 enc.WriteMapHeader(2) 20 enc.WriteString("float_a") 21 enc.WriteFloat32(1.0) 22 enc.WriteString("int_b") 23 enc.WriteInt64(-100) 24 25 enc.WriteString("some bytes") 26 enc.WriteBytes([]byte("here are some bytes")) 27 enc.WriteString("a bool") 28 enc.WriteBool(true) 29 30 enc.WriteString("a map") 31 enc.WriteMapStrStr(map[string]string{ 32 "internal_one": "blah", 33 "internal_two": "blahhh...", 34 }) 35 enc.Flush() 36 37 var js bytes.Buffer 38 _, err := CopyToJSON(&js, &buf) 39 if err != nil { 40 t.Fatal(err) 41 } 42 mp := make(map[string]interface{}) 43 err = json.Unmarshal(js.Bytes(), &mp) 44 if err != nil { 45 t.Log(js.String()) 46 t.Fatalf("Error unmarshaling: %s", err) 47 } 48 49 if len(mp) != 5 { 50 t.Errorf("map length should be %d, not %d", 4, len(mp)) 51 } 52 53 so, ok := mp["thing_1"] 54 if !ok || so != "a string object" { 55 t.Errorf("expected %q; got %q", "a string object", so) 56 } 57 58 in, ok := mp["a map"] 59 if !ok { 60 t.Error("no key 'a map'") 61 } 62 if inm, ok := in.(map[string]interface{}); !ok { 63 t.Error("inner map not type-assertable to map[string]interface{}") 64 } else { 65 inm1, ok := inm["internal_one"] 66 if !ok || !reflect.DeepEqual(inm1, "blah") { 67 t.Errorf("inner map field %q should be %q, not %q", "internal_one", "blah", inm1) 68 } 69 } 70 } 71 72 func BenchmarkCopyToJSON(b *testing.B) { 73 var buf bytes.Buffer 74 enc := NewWriter(&buf) 75 enc.WriteMapHeader(4) 76 77 enc.WriteString("thing_1") 78 enc.WriteString("a string object") 79 80 enc.WriteString("a_first_map") 81 enc.WriteMapHeader(2) 82 enc.WriteString("float_a") 83 enc.WriteFloat32(1.0) 84 enc.WriteString("int_b") 85 enc.WriteInt64(-100) 86 87 enc.WriteString("an array") 88 enc.WriteArrayHeader(2) 89 enc.WriteBool(true) 90 enc.WriteUint(2089) 91 92 enc.WriteString("a_second_map") 93 enc.WriteMapStrStr(map[string]string{ 94 "internal_one": "blah", 95 "internal_two": "blahhh...", 96 }) 97 enc.Flush() 98 99 var js bytes.Buffer 100 bts := buf.Bytes() 101 _, err := CopyToJSON(&js, &buf) 102 if err != nil { 103 b.Fatal(err) 104 } 105 b.SetBytes(int64(len(js.Bytes()))) 106 b.ResetTimer() 107 b.ReportAllocs() 108 for i := 0; i < b.N; i++ { 109 js.Reset() 110 CopyToJSON(&js, bytes.NewReader(bts)) 111 } 112 } 113 114 func BenchmarkStdlibJSON(b *testing.B) { 115 obj := map[string]interface{}{ 116 "thing_1": "a string object", 117 "a_first_map": map[string]interface{}{ 118 "float_a": float32(1.0), 119 "float_b": -100, 120 }, 121 "an array": []interface{}{ 122 "part_A", 123 "part_B", 124 }, 125 "a_second_map": map[string]interface{}{ 126 "internal_one": "blah", 127 "internal_two": "blahhh...", 128 }, 129 } 130 var js bytes.Buffer 131 err := json.NewEncoder(&js).Encode(&obj) 132 if err != nil { 133 b.Fatal(err) 134 } 135 b.SetBytes(int64(len(js.Bytes()))) 136 b.ResetTimer() 137 b.ReportAllocs() 138 for i := 0; i < b.N; i++ { 139 js.Reset() 140 json.NewEncoder(&js).Encode(&obj) 141 } 142 }