项目文件夹

文件
wehub-resource-sync 6cf6f95f58
CodeQL / Analyze (push) Has been cancelled
Sync to Gitee / Run (push) Has been cancelled
Go / build & test (1.25.x) (push) Has been cancelled
Go / build & test (1.26.x) (push) Has been cancelled
Lint / resolve module (push) Has been cancelled
Lint / lint module (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:39:43 +08:00

67 行
1.3 KiB
Go

package log
import (
"context"
"log/slog"
"testing"
)
type benchmarkHandler struct {
enabled bool
}
func (h benchmarkHandler) Enabled(context.Context, slog.Level) bool {
return h.enabled
}
func (h benchmarkHandler) Handle(context.Context, slog.Record) error {
return nil
}
func (h benchmarkHandler) WithAttrs([]slog.Attr) slog.Handler {
return h
}
func (h benchmarkHandler) WithGroup(string) slog.Handler {
return h
}
func benchmarkWithDefault(b *testing.B, h slog.Handler) {
b.Helper()
old := Default()
SetDefault(slog.New(h))
b.Cleanup(func() {
SetDefault(old)
})
b.ReportAllocs()
}
func BenchmarkInfoDisabled(b *testing.B) {
benchmarkWithDefault(b, benchmarkHandler{})
for i := 0; i < b.N; i++ {
Info("hello", "kind", "test")
}
}
func BenchmarkInfoEnabled(b *testing.B) {
benchmarkWithDefault(b, benchmarkHandler{enabled: true})
for i := 0; i < b.N; i++ {
Info("hello", "kind", "test")
}
}
func BenchmarkLogAttrsEnabled(b *testing.B) {
benchmarkWithDefault(b, benchmarkHandler{enabled: true})
attr := slog.String("kind", "test")
for i := 0; i < b.N; i++ {
LogAttrs(context.Background(), LevelInfo, "hello", attr)
}
}
func BenchmarkSlogInfoEnabled(b *testing.B) {
benchmarkWithDefault(b, benchmarkHandler{enabled: true})
for i := 0; i < b.N; i++ {
slog.Info("hello", "kind", "test")
}
}