Files
loggerx/loggerx_features_bench_test.go
2026-09-14 00:14:30 +08:00

76 lines
1.8 KiB
Go

package loggerx_test
import (
"context"
"testing"
"time"
"github.com/yuninks/loggerx"
)
// 按大小切割开启后,每条日志会多一次 Stat(判断是否该滚动)的开销
func BenchmarkWriteWithSizeSplit(b *testing.B) {
l := benchLogger(b, loggerx.SetSizeSplit(8<<20))
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Infof(ctx, "hello %d", i)
}
}
// 不开大小切割的对照
func BenchmarkWriteNoSizeSplit(b *testing.B) {
l := benchLogger(b)
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Infof(ctx, "hello %d", i)
}
}
// 定时刷盘开启后(间隔取大值,避免刷盘本身干扰测量)
func BenchmarkWriteWithFlushInterval(b *testing.B) {
l := benchLogger(b, loggerx.SetFlushInterval(time.Hour))
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Infof(ctx, "hello %d", i)
}
}
// 定时刷盘:短间隔(1ms)下的稳态吞吐,含刷盘协程竞争
func BenchmarkWriteShortFlushInterval(b *testing.B) {
l := benchLogger(b, loggerx.SetFlushInterval(time.Millisecond))
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Infof(ctx, "hello %d", i)
}
}
// text 格式(不走 encoding/json,应该比默认 JSON 便宜)
func BenchmarkTextFormat(b *testing.B) {
l := benchLogger(b, loggerx.SetFormat(loggerx.FormatText))
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Infof(ctx, "hello %d", i)
}
}
// 带前缀的 JSON(每行多一次拼接)
func BenchmarkJSONWithPrefix(b *testing.B) {
l := benchLogger(b, loggerx.SetPrefix("[svc] "))
ctx := context.Background()
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
l.Infof(ctx, "hello %d", i)
}
}