Skip to content
This repository has been archived by the owner on May 8, 2024. It is now read-only.

Commit

Permalink
fix: fix the code
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkgos committed Apr 17, 2024
1 parent eaa0d36 commit c8e1126
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 60 deletions.
54 changes: 24 additions & 30 deletions benchmark_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,13 @@ func Benchmark_Json_Logger(b *testing.B) {
ctx := context.Background()
b.StartTimer()
for i := 0; i < b.N; i++ {
logger.
InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
dfltCtx(ctx),
)
logger.InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
dfltCtx(ctx),
)
}
}

Expand All @@ -81,13 +80,12 @@ func Benchmark_Json_Logger_Use_Hook(b *testing.B) {
ctx := context.Background()
b.StartTimer()
for i := 0; i < b.N; i++ {
logger.
InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
)
logger.InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
)
}
}

Expand All @@ -111,7 +109,7 @@ func Benchmark_Json_NativeSugar(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePair(b *testing.B) {
func Benchmark_Json_KeyValuePair(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -127,7 +125,7 @@ func Benchmark_Json_SugarKeyValuePair(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePairFields(b *testing.B) {
func Benchmark_Json_KeyValuePairFields(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -143,7 +141,7 @@ func Benchmark_Json_SugarKeyValuePairFields(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePairFields_Use_Hook(b *testing.B) {
func Benchmark_Json_KeyValuePairFields_Use_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -159,7 +157,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_Hook(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields(b *testing.B) {
func Benchmark_Json_KeyValuePairFields_Use_WithFields(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -174,7 +172,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) {
func Benchmark_Json_KeyValuePairFields_Use_WithFields_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -189,7 +187,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) {
func Benchmark_Json_KeyValuePairFields_Use_WithValuer(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -204,7 +202,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) {
}
}

func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) {
func Benchmark_Json_KeyValuePairFields_Use_WithValuer_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -219,7 +217,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) {
}
}

func Benchmark_Json_SugarFormat(b *testing.B) {
func Benchmark_Json_Format(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -234,13 +232,11 @@ func Benchmark_Json_SugarFormat(b *testing.B) {
return log.Int("age", 18)
},
dfltCtx,
).InfofContext(ctx,
"success",
)
).InfofContext(ctx, "success")
}
}

func Benchmark_Json_SugarFormat_Use_Hook(b *testing.B) {
func Benchmark_Json_Format_Use_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatJson)
Expand All @@ -251,8 +247,6 @@ func Benchmark_Json_SugarFormat_Use_Hook(b *testing.B) {
logger.WithValuer(
log.ImmutString("name", "jack"),
log.ImmutInt("age", 18),
).InfofContext(ctx,
"success",
)
).InfofContext(ctx, "success")
}
}
54 changes: 24 additions & 30 deletions benchmark_text_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,13 @@ func Benchmark_Text_Logger(b *testing.B) {
ctx := context.Background()
b.StartTimer()
for i := 0; i < b.N; i++ {
logger.
InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
dfltCtx(ctx),
)
logger.InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
dfltCtx(ctx),
)
}
}

Expand All @@ -56,13 +55,12 @@ func Benchmark_Text_Logger_Use_Hook(b *testing.B) {
ctx := context.Background()
b.StartTimer()
for i := 0; i < b.N; i++ {
logger.
InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
)
logger.InfoxContext(
ctx,
"success",
log.String("name", "jack"),
log.Int("age", 18),
)
}
}

Expand All @@ -86,7 +84,7 @@ func Benchmark_Text_NativeSugar(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePair(b *testing.B) {
func Benchmark_Text_KeyValuePair(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -102,7 +100,7 @@ func Benchmark_Text_SugarKeyValuePair(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePairFields(b *testing.B) {
func Benchmark_Text_KeyValuePairFields(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -118,7 +116,7 @@ func Benchmark_Text_SugarKeyValuePairFields(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePairFields_Use_Hook(b *testing.B) {
func Benchmark_Text_KeyValuePairFields_Use_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -134,7 +132,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_Hook(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields(b *testing.B) {
func Benchmark_Text_KeyValuePairFields_Use_WithFields(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -149,7 +147,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) {
func Benchmark_Text_KeyValuePairFields_Use_WithFields_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -164,7 +162,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) {
func Benchmark_Text_KeyValuePairFields_Use_WithValuer(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -179,7 +177,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) {
}
}

func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) {
func Benchmark_Text_KeyValuePairFields_Use_WithValuer_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -194,7 +192,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) {
}
}

func Benchmark_Text_SugarFormat(b *testing.B) {
func Benchmark_Text_Format(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -209,13 +207,11 @@ func Benchmark_Text_SugarFormat(b *testing.B) {
return log.Int("age", 18)
},
dfltCtx,
).InfofContext(ctx,
"success",
)
).InfofContext(ctx, "success")
}
}

func Benchmark_Text_SugarFormat_Use_Hook(b *testing.B) {
func Benchmark_Text_Format_Use_Hook(b *testing.B) {
b.ReportAllocs()
b.StopTimer()
logger := newDiscardLogger(log.FormatConsole)
Expand All @@ -226,8 +222,6 @@ func Benchmark_Text_SugarFormat_Use_Hook(b *testing.B) {
logger.WithValuer(
log.ImmutString("name", "jack"),
log.ImmutInt("age", 18),
).InfofContext(ctx,
"success",
)
).InfofContext(ctx, "success")
}
}
1 change: 1 addition & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ func formatMessage(template string, fmtArgs []any) string {
return fmt.Sprint(fmtArgs...)
}

// copy from zap(sugar.go)
func (l *Log) appendSweetenFields(fields []Field, args []any) []Field {
if len(args) == 0 {
return nil
Expand Down

0 comments on commit c8e1126

Please sign in to comment.