package log import ( "testing" ) // These benchmarks measure the producer path: format selection, channel send, // and drop accounting. File writes complete asynchronously in the processor and // are not attributed to the measured iterations. // BenchmarkLoggerInfo measures the default raw-format path. func BenchmarkLoggerInfo(b *testing.B) { logger, _ := newTestLogger(b) b.ReportAllocs() for i := 0; b.Loop(); i++ { logger.Info("benchmark message", i) } } // BenchmarkLoggerTxt measures enqueueing with txt output selected. func BenchmarkLoggerTxt(b *testing.B) { logger, _ := newTestLogger(b) cfg := logger.GetConfig() cfg.Format = "txt" mustNoErr(b, logger.ApplyConfig(cfg), "ApplyConfig") b.ReportAllocs() for i := 0; b.Loop(); i++ { logger.Info("benchmark message", i) } } // BenchmarkLoggerJSON measures the json path with key/value arguments. func BenchmarkLoggerJSON(b *testing.B) { logger, _ := newTestLogger(b) cfg := logger.GetConfig() cfg.Format = "json" mustNoErr(b, logger.ApplyConfig(cfg), "ApplyConfig") b.ReportAllocs() for i := 0; b.Loop(); i++ { logger.Info("benchmark message", i, "key", "value") } } // BenchmarkLoggerStructured measures enqueueing and snapshotting structured maps. func BenchmarkLoggerStructured(b *testing.B) { logger, _ := newTestLogger(b) cfg := logger.GetConfig() cfg.Format = "json" mustNoErr(b, logger.ApplyConfig(cfg), "ApplyConfig") fields := map[string]any{ "user_id": 123, "action": "benchmark", "value": 42.5, } b.ReportAllocs() for b.Loop() { logger.LogStructured(LevelInfo, "benchmark", fields) } } // BenchmarkLoggerSanitized measures enqueueing with PolicyTxt selected; // sanitizer CPU work runs asynchronously and is benchmarked in its own package. func BenchmarkLoggerSanitized(b *testing.B) { logger, _ := newTestLogger(b) cfg := logger.GetConfig() cfg.Format = "txt" cfg.Sanitization = PolicyTxt mustNoErr(b, logger.ApplyConfig(cfg), "ApplyConfig") b.ReportAllocs() for i := 0; b.Loop(); i++ { logger.Info("benchmark message", i) } } // BenchmarkConcurrentLogging measures contention on the shared channel. func BenchmarkConcurrentLogging(b *testing.B) { logger, _ := newTestLogger(b) b.ReportAllocs() b.RunParallel(func(pb *testing.PB) { i := 0 for pb.Next() { logger.Info("concurrent", i) i++ } }) }