-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbench_test.go
73 lines (63 loc) · 1.88 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
package zeroslog
import (
"context"
"io"
"log/slog"
"testing"
"time"
"github.com/rs/zerolog"
)
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 = map[string]slog.Handler{
"std-text": slog.NewTextHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelDebug}),
"std-json": slog.NewJSONHandler(io.Discard, &slog.HandlerOptions{Level: slog.LevelDebug}),
"zerolog": NewJsonHandler(io.Discard, &HandlerOptions{Level: slog.LevelDebug}),
"dummy": &DummyHandler{},
}
loggers = func() map[string]*slog.Logger {
m := make(map[string]*slog.Logger, len(handlers))
for name, hdl := range handlers {
m[name] = slog.New(hdl)
}
return m
}()
)
func BenchmarkZerolog_Raw(b *testing.B) {
l := zerolog.New(io.Discard).Level(zerolog.DebugLevel).With().Timestamp().Logger()
l = l.With().Str("foo", "bar").Logger()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Info().Str("bar", "baz").Msg("hello")
}
}
func BenchmarkHandlers(b *testing.B) {
ctx := context.Background()
for name, h := range handlers {
b.Run(name, func(b *testing.B) {
h = h.WithAttrs([]slog.Attr{slog.String("foo", "bar")})
rec := slog.NewRecord(time.Now(), slog.LevelInfo, "hello", 0)
rec.AddAttrs(slog.String("bar", "baz"))
b.ResetTimer()
for i := 0; i < b.N; i++ {
h.Handle(ctx, rec)
}
})
}
}
func BenchmarkLoggers(b *testing.B) {
ctx := context.Background()
for name, l := range loggers {
b.Run(name, func(b *testing.B) {
l = l.With("foo", "bar")
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(ctx, slog.LevelInfo, "hello", slog.String("bar", "baz"))
}
})
}
}