1/* 2Copyright 2020 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package logs 18 19import ( 20 "encoding/json" 21 "fmt" 22 "testing" 23 "time" 24) 25 26var record = struct { 27 Error error `json:"err"` 28 Level int `json:"v"` 29 Message string `json:"msg"` 30 Time time.Time `json:"ts"` 31 Fields map[string]interface{} `json:"fields"` 32}{ 33 Error: fmt.Errorf("test for error:%s", "default"), 34 Level: 2, 35 Message: "test", 36 Time: time.Unix(0, 123), 37 Fields: map[string]interface{}{ 38 "str": "foo", 39 "int64-1": int64(1), 40 "int64-2": int64(1), 41 "float64": float64(1.0), 42 "string1": "\n", 43 "string2": "", 44 "string3": "", 45 "string4": "", 46 "bool": true, 47 "request": struct { 48 Method string `json:"method"` 49 Timeout int `json:"timeout"` 50 secret string `json:"secret"` 51 }{ 52 Method: "GET", 53 Timeout: 10, 54 secret: "pony", 55 }, 56 }, 57} 58 59func BenchmarkInfoLoggerInfo(b *testing.B) { 60 b.RunParallel(func(pb *testing.PB) { 61 for pb.Next() { 62 jLogger := NewJSONLogger(nil) 63 jLogger.Info("test", 64 "str", "foo", 65 "int64-1", int64(1), 66 "int64-2", int64(1), 67 "float64", float64(1.0), 68 "string1", "\n", 69 "string2", "", 70 "string3", "", 71 "string4", "", 72 "bool", true, 73 "request", struct { 74 Method string `json:"method"` 75 Timeout int `json:"timeout"` 76 secret string `json:"secret"` 77 }{ 78 Method: "GET", 79 Timeout: 10, 80 secret: "pony", 81 }, 82 ) 83 } 84 }) 85} 86 87func BenchmarkInfoLoggerInfoStandardJSON(b *testing.B) { 88 b.ResetTimer() 89 b.RunParallel(func(pb *testing.PB) { 90 for pb.Next() { 91 json.Marshal(record) 92 } 93 }) 94} 95 96func BenchmarkZapLoggerError(b *testing.B) { 97 b.RunParallel(func(pb *testing.PB) { 98 for pb.Next() { 99 jLogger := NewJSONLogger(nil) 100 jLogger.Error(fmt.Errorf("test for error:%s", "default"), 101 "test", 102 "str", "foo", 103 "int64-1", int64(1), 104 "int64-2", int64(1), 105 "float64", float64(1.0), 106 "string1", "\n", 107 "string2", "", 108 "string3", "", 109 "string4", "", 110 "bool", true, 111 "request", struct { 112 Method string `json:"method"` 113 Timeout int `json:"timeout"` 114 secret string `json:"secret"` 115 }{ 116 Method: "GET", 117 Timeout: 10, 118 secret: "pony", 119 }, 120 ) 121 } 122 }) 123} 124func BenchmarkZapLoggerErrorStandardJSON(b *testing.B) { 125 b.ResetTimer() 126 b.RunParallel(func(pb *testing.PB) { 127 for pb.Next() { 128 json.Marshal(record) 129 } 130 }) 131} 132 133func BenchmarkZapLoggerV(b *testing.B) { 134 b.RunParallel(func(pb *testing.PB) { 135 for pb.Next() { 136 jLogger := NewJSONLogger(nil) 137 jLogger.V(1).Info("test", 138 "str", "foo", 139 "int64-1", int64(1), 140 "int64-2", int64(1), 141 "float64", float64(1.0), 142 "string1", "\n", 143 "string2", "", 144 "string3", "", 145 "string4", "", 146 "bool", true, 147 "request", struct { 148 Method string `json:"method"` 149 Timeout int `json:"timeout"` 150 secret string `json:"secret"` 151 }{ 152 Method: "GET", 153 Timeout: 10, 154 secret: "pony", 155 }, 156 ) 157 } 158 }) 159} 160