Debian Codegraph @

logging/logger_bench_test.go

raw
1 package logging
2
3 import (
4 "context"
5 "io"
6 "log/slog"
7 "testing"
8 )
9
10 // BenchmarkLogger_* compare the handler returned by NewLogger against a
11 // stock slog text handler. The key thing we care about is the per-log
12 // cost on a logger that has been derived via .With(), because that is the
13 // shape subsystems store on their structs (HostInfo.logger(),
14 // lh.l.With("subsystem", ...), etc.) and call from hot paths.
15
16 func BenchmarkLogger_Stock_RootInfo(b *testing.B) {
17 l := slog.New(slog.DiscardHandler)
18 b.ReportAllocs()
19 b.ResetTimer()
20 for i := 0; i < b.N; i++ {
21 l.Info("hello", "i", i)
22 }
23 }
24
25 func BenchmarkLogger_Nebula_RootInfo(b *testing.B) {
26 l := NewLogger(io.Discard)
27 b.ReportAllocs()
28 b.ResetTimer()
29 for i := 0; i < b.N; i++ {
30 l.Info("hello", "i", i)
31 }
32 }
33
34 func BenchmarkLogger_Stock_DerivedInfo(b *testing.B) {
35 l := slog.New(slog.DiscardHandler).With(
36 "subsystem", "bench",
37 "localIndex", 1234,
38 )
39 b.ReportAllocs()
40 b.ResetTimer()
41 for i := 0; i < b.N; i++ {
42 l.Info("hello", "i", i)
43 }
44 }
45
46 func BenchmarkLogger_Nebula_DerivedInfo(b *testing.B) {
47 l := NewLogger(io.Discard).With(
48 "subsystem", "bench",
49 "localIndex", 1234,
50 )
51 b.ReportAllocs()
52 b.ResetTimer()
53 for i := 0; i < b.N; i++ {
54 l.Info("hello", "i", i)
55 }
56 }
57
58 // Gated-off-path benchmarks: mimic the typical hot-path shape
59 // `if l.Enabled(ctx, slog.LevelDebug) { ... }` where the log is gated below
60 // the active level. This is the dominant pattern in inside.go/outside.go and
61 // what we pay on every packet.
62 func BenchmarkLogger_Stock_DerivedEnabledGateMiss(b *testing.B) {
63 l := slog.New(slog.DiscardHandler).With(
64 "subsystem", "bench",
65 "localIndex", 1234,
66 )
67 ctx := context.Background()
68 b.ReportAllocs()
69 b.ResetTimer()
70 for i := 0; i < b.N; i++ {
71 if l.Enabled(ctx, slog.LevelDebug) {
72 l.Debug("hello", "i", i)
73 }
74 }
75 }
76
77 func BenchmarkLogger_Nebula_DerivedEnabledGateMiss(b *testing.B) {
78 l := NewLogger(io.Discard).With(
79 "subsystem", "bench",
80 "localIndex", 1234,
81 )
82 ctx := context.Background()
83 b.ReportAllocs()
84 b.ResetTimer()
85 for i := 0; i < b.N; i++ {
86 if l.Enabled(ctx, slog.LevelDebug) {
87 l.Debug("hello", "i", i)
88 }
89 }
90 }