github.com/Laisky/zap@v1.27.0/zapcore/json_encoder_bench_test.go (about) 1 // Copyright (c) 2016 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package zapcore_test 22 23 import ( 24 "encoding/json" 25 "fmt" 26 "testing" 27 "time" 28 29 //revive:disable:dot-imports 30 . "github.com/Laisky/zap/zapcore" 31 ) 32 33 func BenchmarkJSONLogMarshalerFunc(b *testing.B) { 34 for i := 0; i < b.N; i++ { 35 enc := NewJSONEncoder(testEncoderConfig()) 36 err := enc.AddObject("nested", ObjectMarshalerFunc(func(enc ObjectEncoder) error { 37 enc.AddInt64("i", int64(i)) 38 return nil 39 })) 40 if err != nil { 41 b.Fatal(err) 42 } 43 } 44 } 45 46 func BenchmarkZapJSONFloat32AndComplex64(b *testing.B) { 47 b.RunParallel(func(pb *testing.PB) { 48 for pb.Next() { 49 enc := NewJSONEncoder(testEncoderConfig()) 50 enc.AddFloat32("float32", 3.14) 51 enc.AddComplex64("complex64", 2.71+3.14i) 52 } 53 }) 54 } 55 56 const _sliceSize = 5000 57 58 type StringSlice []string 59 60 func (s StringSlice) MarshalLogArray(encoder ArrayEncoder) error { 61 for _, str := range s { 62 encoder.AppendString(str) 63 } 64 return nil 65 } 66 67 func generateStringSlice(n int) StringSlice { 68 output := make(StringSlice, 0, n) 69 for i := 0; i < n; i++ { 70 output = append(output, fmt.Sprint("00000000-0000-0000-0000-0000000000", i)) 71 } 72 return output 73 } 74 75 func BenchmarkZapJSON(b *testing.B) { 76 additional := generateStringSlice(_sliceSize) 77 b.ResetTimer() 78 b.RunParallel(func(pb *testing.PB) { 79 for pb.Next() { 80 enc := NewJSONEncoder(testEncoderConfig()) 81 enc.AddString("str", "foo") 82 enc.AddInt64("int64-1", 1) 83 enc.AddInt64("int64-2", 2) 84 enc.AddFloat64("float64", 1.0) 85 enc.AddString("string1", "\n") 86 enc.AddString("string2", "💩") 87 enc.AddString("string3", "🤔") 88 enc.AddString("string4", "🙊") 89 enc.AddBool("bool", true) 90 _ = enc.AddArray("test", additional) 91 buf, _ := enc.EncodeEntry(Entry{ 92 Message: "fake", 93 Level: DebugLevel, 94 }, nil) 95 buf.Free() 96 } 97 }) 98 } 99 100 func BenchmarkStandardJSON(b *testing.B) { 101 record := struct { 102 Level string `json:"level"` 103 Message string `json:"msg"` 104 Time time.Time `json:"ts"` 105 Fields map[string]interface{} `json:"fields"` 106 Additional StringSlice 107 }{ 108 Level: "debug", 109 Message: "fake", 110 Time: time.Unix(0, 0), 111 Fields: map[string]interface{}{ 112 "str": "foo", 113 "int64-1": int64(1), 114 "int64-2": int64(1), 115 "float64": float64(1.0), 116 "string1": "\n", 117 "string2": "💩", 118 "string3": "🤔", 119 "string4": "🙊", 120 "bool": true, 121 }, 122 Additional: generateStringSlice(_sliceSize), 123 } 124 b.ResetTimer() 125 b.RunParallel(func(pb *testing.PB) { 126 for pb.Next() { 127 if _, err := json.Marshal(record); err != nil { 128 b.Fatal(err) 129 } 130 } 131 }) 132 }