logger_bench_test.go 2.1 KB

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