-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench_test.go
More file actions
73 lines (66 loc) · 1.67 KB
/
Copy pathbench_test.go
File metadata and controls
73 lines (66 loc) · 1.67 KB
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 logger
import (
"context"
"io"
"log/slog"
"testing"
)
// Allocations/op is the load-bearing number. These run the typed hot path,
// the slog-bridge path (the honest "through-bridge" cost), and stdlib slog
// for reference — all writing to io.Discard so we measure the logger, not I/O.
func benchLogger() *Logger {
return New(
WithSink(NewWriterSink(io.Discard, NewJSONEncoder(), LevelTrace)),
WithLevel(LevelInfo),
)
}
func BenchmarkTypedHotPath(b *testing.B) {
l := benchLogger()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Info("request handled",
String("method", "GET"),
String("path", "/v1/orders"),
Int("status", 200),
Int("bytes", 4096),
Bool("cached", true),
)
}
}
func BenchmarkDisabledLevel(b *testing.B) {
l := benchLogger()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Debug("should be cheap", String("k", "v")) // below Info
}
}
func BenchmarkThroughSlogBridge(b *testing.B) {
l := slog.New(benchLogger().Handler())
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(context.Background(), slog.LevelInfo, "request handled",
slog.String("method", "GET"),
slog.String("path", "/v1/orders"),
slog.Int("status", 200),
slog.Int("bytes", 4096),
slog.Bool("cached", true),
)
}
}
func BenchmarkStdlibSlogJSON(b *testing.B) {
l := slog.New(slog.NewJSONHandler(io.Discard, nil))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.LogAttrs(context.Background(), slog.LevelInfo, "request handled",
slog.String("method", "GET"),
slog.String("path", "/v1/orders"),
slog.Int("status", 200),
slog.Int("bytes", 4096),
slog.Bool("cached", true),
)
}
}