diff --git a/logging/logging.go b/logging/logging.go index fcbe0d124..bd8aa977a 100644 --- a/logging/logging.go +++ b/logging/logging.go @@ -27,6 +27,8 @@ const ( // RecordLogFileName represents the default file name of the record log. RecordLogFileName = "sentinel-record.log" GlobalCallerDepth = 4 + + defaultLogMsgBufferSize = 256 ) var ( @@ -117,6 +119,8 @@ func caller(depth int) (file string, line int) { func AssembleMsg(depth int, logLevel, msg string, err error, keysAndValues ...interface{}) string { sb := strings.Builder{} + sb.Grow(defaultLogMsgBufferSize) + file, line := caller(depth) timeStr := time.Now().Format("2006-01-02 15:04:05.520") caller := fmt.Sprintf("%s:%d", file, line) diff --git a/logging/logging_test.go b/logging/logging_test.go index 818ce7ac1..334e43d7d 100644 --- a/logging/logging_test.go +++ b/logging/logging_test.go @@ -109,3 +109,10 @@ func Benchmark_LoggingDebug_With_Precheck(b *testing.B) { } } } + +func BenchmarkAssembleMsg(b *testing.B) { + b.ReportAllocs() + for i := 0; i < b.N; i++ { + AssembleMsg(1, "INFO", "test msg", nil, "k1", "v1", "k2", "v2") + } +}