v0.1.8 tests converted to standard library from testify

This commit is contained in:
2026-07-24 13:48:21 -04:00
parent 5553aeaec4
commit 24b7deebdb
19 changed files with 2399 additions and 1795 deletions
+54 -24
View File
@@ -4,40 +4,55 @@ import (
"testing"
)
// BenchmarkLoggerInfo benchmarks the performance of standard Info logging
func BenchmarkLoggerInfo(b *testing.B) {
logger, _ := createTestLogger(&testing.T{})
defer logger.Shutdown()
// 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.
b.ResetTimer()
for i := 0; i < b.N; i++ {
// 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)
}
}
// BenchmarkLoggerJSON benchmarks the performance of JSON formatted logging
// BenchmarkLoggerTxt measures the txt path, which includes quote analysis.
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, _ := createTestLogger(&testing.T{})
defer logger.Shutdown()
logger, _ := newTestLogger(b)
cfg := logger.GetConfig()
cfg.Format = "json"
logger.ApplyConfig(cfg)
mustNoErr(b, logger.ApplyConfig(cfg), "ApplyConfig")
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.ReportAllocs()
for i := 0; b.Loop(); i++ {
logger.Info("benchmark message", i, "key", "value")
}
}
// BenchmarkLoggerStructured benchmarks the performance of structured JSON logging
// BenchmarkLoggerStructured measures the json.Marshal path for field maps.
func BenchmarkLoggerStructured(b *testing.B) {
logger, _ := createTestLogger(&testing.T{})
defer logger.Shutdown()
logger, _ := newTestLogger(b)
cfg := logger.GetConfig()
cfg.Format = "json"
logger.ApplyConfig(cfg)
mustNoErr(b, logger.ApplyConfig(cfg), "ApplyConfig")
fields := map[string]any{
"user_id": 123,
@@ -45,18 +60,33 @@ func BenchmarkLoggerStructured(b *testing.B) {
"value": 42.5,
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
b.ReportAllocs()
for b.Loop() {
logger.LogStructured(LevelInfo, "benchmark", fields)
}
}
// BenchmarkConcurrentLogging benchmarks the logger's performance under concurrent load
func BenchmarkConcurrentLogging(b *testing.B) {
logger, _ := createTestLogger(&testing.T{})
defer logger.Shutdown()
// BenchmarkLoggerSanitized measures PolicyTxt overhead on control-free input,
// where the sanitizer takes its no-allocation fast path.
func BenchmarkLoggerSanitized(b *testing.B) {
logger, _ := newTestLogger(b)
b.ResetTimer()
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() {
@@ -64,4 +94,4 @@ func BenchmarkConcurrentLogging(b *testing.B) {
i++
}
})
}
}