From 7cc61659a454f9929032ad76a8a7589df6abfd83 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Tue, 16 Apr 2024 11:30:08 +0800 Subject: [PATCH] fix: fix benchmark bug --- benchmark_json_test.go | 26 +++++++++++++++++++++----- benchmark_text_test.go | 26 +++++++++++++++++++++----- 2 files changed, 42 insertions(+), 10 deletions(-) diff --git a/benchmark_json_test.go b/benchmark_json_test.go index 23f603a..20abfaa 100644 --- a/benchmark_json_test.go +++ b/benchmark_json_test.go @@ -108,6 +108,22 @@ func Benchmark_Json_NativeSugar(b *testing.B) { } func Benchmark_Json_SugarKeyValuePair(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatJson) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.InfowContext(ctx, + "success", + "name", "jack", + "age", 18, + dfltCtx(ctx), + ) + } +} + +func Benchmark_Json_SugarKeyValuePairFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -123,7 +139,7 @@ func Benchmark_Json_SugarKeyValuePair(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePair_Use_Hook(b *testing.B) { +func Benchmark_Json_SugarKeyValuePairFields_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -139,7 +155,7 @@ func Benchmark_Json_SugarKeyValuePair_Use_Hook(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePair_Use_WithFields(b *testing.B) { +func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -154,7 +170,7 @@ func Benchmark_Json_SugarKeyValuePair_Use_WithFields(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { +func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -169,7 +185,7 @@ func Benchmark_Json_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePair_Use_WithValuer(b *testing.B) { +func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -184,7 +200,7 @@ func Benchmark_Json_SugarKeyValuePair_Use_WithValuer(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePair_Use_WithValuer_Hook(b *testing.B) { +func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) diff --git a/benchmark_text_test.go b/benchmark_text_test.go index 64a547c..37fa2a3 100644 --- a/benchmark_text_test.go +++ b/benchmark_text_test.go @@ -83,6 +83,22 @@ func Benchmark_Text_NativeSugar(b *testing.B) { } func Benchmark_Text_SugarKeyValuePair(b *testing.B) { + b.ReportAllocs() + b.StopTimer() + logger := newDiscardLogger(log.FormatConsole) + ctx := context.Background() + b.StartTimer() + for i := 0; i < b.N; i++ { + logger.InfowContext(ctx, + "success", + "name", "jack", + "age", 18, + dfltCtx(ctx), + ) + } +} + +func Benchmark_Text_SugarKeyValuePairFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -98,7 +114,7 @@ func Benchmark_Text_SugarKeyValuePair(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePair_Use_Hook(b *testing.B) { +func Benchmark_Text_SugarKeyValuePairFields_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -114,7 +130,7 @@ func Benchmark_Text_SugarKeyValuePair_Use_Hook(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePair_Use_WithFields(b *testing.B) { +func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -129,7 +145,7 @@ func Benchmark_Text_SugarKeyValuePair_Use_WithFields(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { +func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -144,7 +160,7 @@ func Benchmark_Text_SugarKeyValuePair_Use_WithFields_Hook(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePair_Use_WithValuer(b *testing.B) { +func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -159,7 +175,7 @@ func Benchmark_Text_SugarKeyValuePair_Use_WithValuer(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePair_Use_WithValuer_Hook(b *testing.B) { +func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole)