Skip to content

Commit

Permalink
test: migrate slog benchmark from golang.org/x/exp/slog to log/slog
Browse files Browse the repository at this point in the history
  • Loading branch information
chenyanchen committed Sep 22, 2023
1 parent 4ddf010 commit 68a48fd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 9 deletions.
1 change: 0 additions & 1 deletion benchmarks/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ require (
github.com/sirupsen/logrus v1.9.0
go.uber.org/multierr v1.10.0
go.uber.org/zap v1.23.0
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0
gopkg.in/inconshreveable/log15.v2 v2.0.0-20221122034931-555555054819
)

Expand Down
2 changes: 0 additions & 2 deletions benchmarks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@ go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0 h1:LGJsf5LRplCck6jUCH3dBL2dmycNruWNF5xugkSlfXw=
golang.org/x/exp v0.0.0-20230310171629-522b1b587ee0/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand Down
4 changes: 2 additions & 2 deletions benchmarks/scenario_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func BenchmarkDisabledAddingFields(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info(getMessage(0), fakeSlogFields())
logger.Info(getMessage(0), fakeSlogArgs()...)
}
})
})
Expand Down Expand Up @@ -640,7 +640,7 @@ func BenchmarkAddingFields(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
logger.Info(getMessage(0), fakeSlogFields())
logger.Info(getMessage(0), fakeSlogArgs()...)
}
})
})
Expand Down
22 changes: 18 additions & 4 deletions benchmarks/slog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,15 @@ package benchmarks

import (
"io"

"golang.org/x/exp/slog"
"log/slog"
)

func newSlog(fields ...slog.Attr) *slog.Logger {
return slog.New(slog.NewJSONHandler(io.Discard).WithAttrs(fields))
return slog.New(slog.NewJSONHandler(io.Discard, nil).WithAttrs(fields))
}

func newDisabledSlog(fields ...slog.Attr) *slog.Logger {
return slog.New(slog.HandlerOptions{Level: slog.LevelError}.NewJSONHandler(io.Discard).WithAttrs(fields))
return slog.New(slog.NewJSONHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelError}).WithAttrs(fields))
}

func fakeSlogFields() []slog.Attr {
Expand All @@ -48,3 +47,18 @@ func fakeSlogFields() []slog.Attr {
slog.Any("error", errExample),
}
}

func fakeSlogArgs() []any {
return []any{
"int", _tenInts[0],
"ints", _tenInts,
"string", _tenStrings[0],
"strings", _tenStrings,
"time", _tenTimes[0],
"times", _tenTimes,
"user1", _oneUser,
"user2", _oneUser,
"users", _tenUsers,
"error", errExample,
}
}

0 comments on commit 68a48fd

Please sign in to comment.