github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/internal/encoder/stream_test.go (about) 1 /* 2 * Copyright 2021 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package encoder 18 19 import ( 20 `bytes` 21 `encoding/json` 22 `strings` 23 `testing` 24 25 `github.com/stretchr/testify/require` 26 ) 27 28 func TestEncodeStream(t *testing.T) { 29 var o = map[string]interface{}{ 30 "a": "<>", 31 "b": json.RawMessage(" [ ] "), 32 } 33 var w1 = bytes.NewBuffer(nil) 34 var w2 = bytes.NewBuffer(nil) 35 var enc1 = json.NewEncoder(w1) 36 var enc2 = NewStreamEncoder(w2) 37 enc2.SetEscapeHTML(true) 38 enc2.SortKeys() 39 enc2.SetCompactMarshaler(true) 40 41 require.Nil(t, enc1.Encode(o)) 42 require.Nil(t, enc2.Encode(o)) 43 require.Equal(t, w1.String(), w2.String()) 44 45 enc1.SetEscapeHTML(true) 46 enc2.SetEscapeHTML(true) 47 enc1.SetIndent("你好", "\b") 48 enc2.SetIndent("你好", "\b") 49 require.Nil(t, enc1.Encode(o)) 50 require.Nil(t, enc2.Encode(o)) 51 require.Equal(t, w1.String(), w2.String()) 52 53 enc1.SetEscapeHTML(false) 54 enc2.SetEscapeHTML(false) 55 enc1.SetIndent("", "") 56 enc2.SetIndent("", "") 57 require.Nil(t, enc1.Encode(o)) 58 require.Nil(t, enc2.Encode(o)) 59 require.Equal(t, w1.String(), w2.String()) 60 } 61 62 func BenchmarkEncodeStream_Sonic(b *testing.B) { 63 var o = map[string]interface{}{ 64 "a": `<`+strings.Repeat("1", 1024)+`>`, 65 "b": json.RawMessage(` [ `+strings.Repeat(" ", 1024)+` ] `), 66 } 67 68 b.Run("single", func(b *testing.B){ 69 var w = bytes.NewBuffer(nil) 70 var enc = NewStreamEncoder(w) 71 b.ResetTimer() 72 for i:=0; i<b.N; i++ { 73 _ = enc.Encode(o) 74 w.Reset() 75 } 76 }) 77 78 b.Run("double", func(b *testing.B){ 79 var w = bytes.NewBuffer(nil) 80 var enc = NewStreamEncoder(w) 81 b.ResetTimer() 82 for i:=0; i<b.N; i++ { 83 _ = enc.Encode(o) 84 _ = enc.Encode(o) 85 w.Reset() 86 } 87 }) 88 89 b.Run("compatible", func(b *testing.B){ 90 var w = bytes.NewBuffer(nil) 91 var enc = NewStreamEncoder(w) 92 enc.SetEscapeHTML(true) 93 enc.SortKeys() 94 enc.SetCompactMarshaler(true) 95 b.ResetTimer() 96 for i:=0; i<b.N; i++ { 97 _ = enc.Encode(o) 98 w.Reset() 99 } 100 }) 101 } 102 103 func BenchmarkEncodeStream_Std(b *testing.B) { 104 var o = map[string]interface{}{ 105 "a": `<`+strings.Repeat("1", 1024)+`>`, 106 "b": json.RawMessage(` [ `+strings.Repeat(" ", 1024)+` ] `), 107 } 108 109 b.Run("single", func(b *testing.B){ 110 var w = bytes.NewBuffer(nil) 111 var enc = json.NewEncoder(w) 112 b.ResetTimer() 113 for i:=0; i<b.N; i++ { 114 _ = enc.Encode(o) 115 w.Reset() 116 } 117 }) 118 119 b.Run("double", func(b *testing.B){ 120 var w = bytes.NewBuffer(nil) 121 var enc = json.NewEncoder(w) 122 b.ResetTimer() 123 for i:=0; i<b.N; i++ { 124 _ = enc.Encode(o) 125 _ = enc.Encode(o) 126 w.Reset() 127 } 128 }) 129 130 b.Run("compatible", func(b *testing.B){ 131 var w = bytes.NewBuffer(nil) 132 var enc = json.NewEncoder(w) 133 enc.SetEscapeHTML(true) 134 b.ResetTimer() 135 for i:=0; i<b.N; i++ { 136 _ = enc.Encode(o) 137 w.Reset() 138 } 139 }) 140 }