Skip to content

Commit

Permalink
Add benchmarks for chained Withs (#1326)
Browse files Browse the repository at this point in the history
Add benchmarks to show the performance of chaining child loggers via
'With' and whether the logger is used or not used.

This is intended to establish a baseline for a proposed optimisation in
PR #1319.
  • Loading branch information
jquirke authored Aug 18, 2023
1 parent c94c2bb commit c50301e
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions logger_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ package zap
import (
"errors"
"runtime"
"strconv"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -198,6 +199,37 @@ func BenchmarkAddCallerAndStacktrace(b *testing.B) {
}
})
}
func Benchmark5WithsUsed(b *testing.B) {
benchmarkWithUsed(b, 5, true)
}

// This benchmark will be used in future as a
// baseline for improving
func Benchmark5WithsNotUsed(b *testing.B) {
benchmarkWithUsed(b, 5, false)
}

func benchmarkWithUsed(b *testing.B, N int, use bool) {
keys := make([]string, N)
values := make([]string, N)
for i := 0; i < N; i++ {
keys[i] = "k" + strconv.Itoa(i)
values[i] = "v" + strconv.Itoa(i)
}

b.ResetTimer()

withBenchedLogger(b, func(log *Logger) {
for i := 0; i < N; i++ {
log = log.With(String(keys[i], values[i]))
}
if use {
log.Info("used")
return
}
runtime.KeepAlive(log)
})
}

func Benchmark10Fields(b *testing.B) {
withBenchedLogger(b, func(log *Logger) {
Expand Down

0 comments on commit c50301e

Please sign in to comment.