-
Notifications
You must be signed in to change notification settings - Fork 5
/
bench_test.go
79 lines (70 loc) · 2.04 KB
/
bench_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package console
import (
"context"
"errors"
"io"
"log/slog"
"testing"
"time"
)
type DummyHandler struct{}
func (*DummyHandler) Enabled(context.Context, slog.Level) bool { return true }
func (*DummyHandler) Handle(context.Context, slog.Record) error { return nil }
func (h *DummyHandler) WithAttrs(attrs []slog.Attr) slog.Handler { return h }
func (h *DummyHandler) WithGroup(name string) slog.Handler { return h }
var handlers = []struct {
name string
hdl slog.Handler
}{
{"dummy", &DummyHandler{}},
{"console", NewHandler(io.Discard, &HandlerOptions{Level: slog.LevelDebug, AddSource: false})},
{"std-text", slog.NewTextHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: false})},
{"std-json", slog.NewJSONHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelDebug, AddSource: false})},
}
var attrs = []slog.Attr{
slog.String("foo", "bar"),
slog.Int("int", 12),
slog.Duration("dur", 3*time.Second),
slog.Bool("bool", true),
slog.Float64("float", 23.7),
slog.Time("thetime", time.Now()),
slog.Any("err", errors.New("yo")),
slog.Group("empty"),
slog.Group("group", slog.String("bar", "baz")),
}
var attrsAny = func() (a []any) {
for _, attr := range attrs {
a = append(a, attr)
}
return
}()
func BenchmarkHandlers(b *testing.B) {
ctx := context.Background()
rec := slog.NewRecord(time.Now(), slog.LevelInfo, "hello", 0)
rec.AddAttrs(attrs...)
for _, tc := range handlers {
b.Run(tc.name, func(b *testing.B) {
l := tc.hdl.WithAttrs(attrs).WithGroup("test").WithAttrs(attrs)
// Warm-up
_ = l.Handle(ctx, rec)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = l.Handle(ctx, rec)
}
})
}
}
func BenchmarkLoggers(b *testing.B) {
for _, tc := range handlers {
ctx := context.Background()
b.Run(tc.name, func(b *testing.B) {
l := slog.New(tc.hdl).With(attrsAny...).WithGroup("test").With(attrsAny...)
// Warm-up
l.LogAttrs(ctx, slog.LevelInfo, "hello", attrs...)
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(ctx, slog.LevelInfo, "hello", attrs...)
}
})
}
}