Skip to content

Commit

Permalink
logg/slog: update bench(s)
Browse files Browse the repository at this point in the history
  • Loading branch information
hedzr committed Nov 17, 2024
1 parent 491b652 commit 1d049d8
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 77 deletions.
10 changes: 10 additions & 0 deletions bench/init.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package bench

import (
slogg "github.com/hedzr/logg/slog"
)

func init() {
// no source:
slogg.RemoveFlags(slogg.Lcaller | slogg.LattrsR)
}
92 changes: 92 additions & 0 deletions bench/integrated_single_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package bench

import (
"context"
"testing"
"time"
)

// need optimizing, over 1 allocs (logfmt, json), or 53 allocas (color)
//
// go test -cpuprofile cpu.prof -memprofile mem.prof -benchmem -run=^$ -tags hzstudio,hzwork -bench ^BenchmarkWithoutFieldsSpecial$ github.com/hedzr/logg/bench -v
// go test -cpuprofile cpu.prof -benchmem -run=^$ -tags hzstudio,hzwork -bench ^BenchmarkWithoutFieldsSpecial$ github.com/hedzr/logg/bench -v
// go test -memprofile mem.prof -benchmem -run=^$ -tags hzstudio,hzwork -bench ^BenchmarkWithoutFieldsSpecial$ github.com/hedzr/logg/bench -v
// go tool pprof -http=:6060 cpu.prof
//
// run all benchmarks
//
// go test -benchmem -bench . -run=^$ github.com/hedzr/logg/bench -v
// go test -benchmem -bench . -run=^$ github.com/hedzr/go-zag/benchmarks -v
func BenchmarkWithoutFieldsSpecial(b *testing.B) {
b.Logf("Logging without any structured context. [BenchmarkWithoutFields]")
elapsedTimes := make(map[string]time.Duration)
ctx := context.Background()

b.Run("hedzr/logg/slog", func(b *testing.B) {
logger := newLoggTextMode()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.InfoContext(ctx, getMessage(0))
}
})
elapsedTimes[b.Name()] = b.Elapsed()
})
}

// need optimizing, over 2 allocs (logfmt, json), or 144 allocas (color)
func BenchmarkAccumulatedContextSpecial(b *testing.B) {
b.Logf("Logging with some accumulated context. [BenchmarkAccumulatedContext]")
elapsedTimes := make(map[string]time.Duration)
ctx := context.Background()

b.Run("hedzr/logg/slog TEXT", func(b *testing.B) {
logger := newLoggTextMode().Set(fakeLoggArgs()...)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.InfoContext(ctx, getMessage(0))
}
})
elapsedTimes[b.Name()] = b.Elapsed()
})
}

// need optimizing, over 2 allocs (logfmt, json), or 144 allocas (color)
func BenchmarkAccumulatedContextSpecial1(b *testing.B) {
b.Logf("Logging with some accumulated context. [BenchmarkAccumulatedContextLoggOnly]")
elapsedTimes := make(map[string]time.Duration)
ctx := context.Background()

loggerWarmUp := newLoggTextMode()
_ = loggerWarmUp
loggerWarmUp = newLoggTextMode()
_ = loggerWarmUp

b.Run("hedzr/logg/slog", func(b *testing.B) {
logger := newLoggTextMode().Set(fakeLoggArgs()...)
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.InfoContext(ctx, getMessage(1))
}
})
elapsedTimes[b.Name()] = b.Elapsed()
})
dumpElapsedTimes(b, elapsedTimes)
}

// need optimizing, over 12 allocs (logfmt, json), or 154 allocas (color)
func BenchmarkAddingFieldsSpecial(b *testing.B) {
b.Logf("Logging with additional context at each log site. [BenchmarkAddingFields]")
ctx := context.Background()
b.Run("hedzr/logg/slog", func(b *testing.B) {
logger := newLogg()
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.InfoContext(ctx, getMessage(1), fakeLoggArgs()...)
}
})
})
}
Loading

0 comments on commit 1d049d8

Please sign in to comment.