frostfs-node/pkg/util/logger/logger_test.go
Anton Nikiforov 86264e4e20
All checks were successful
Vulncheck / Vulncheck (push) Successful in 1m13s
Pre-commit hooks / Pre-commit (push) Successful in 1m29s
Build / Build Components (push) Successful in 1m50s
Tests and linters / gopls check (push) Successful in 3m46s
Tests and linters / Run gofumpt (push) Successful in 4m7s
OCI image / Build container images (push) Successful in 4m27s
Tests and linters / Lint (push) Successful in 4m48s
Tests and linters / Staticcheck (push) Successful in 4m50s
Tests and linters / Tests (push) Successful in 5m24s
Tests and linters / Tests with -race (push) Successful in 6m29s
[#1619] logger: Add benchmark
Change-Id: I49e90e8a3689a755755afd0638b327a6b1884795
Signed-off-by: Anton Nikiforov <an.nikiforov@yadro.com>
2025-04-16 13:07:51 +03:00

118 lines
3 KiB
Go

package logger
import (
"context"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)
func BenchmarkLogger(b *testing.B) {
ctx := context.Background()
m := map[string]Prm{}
prm := Prm{}
require.NoError(b, prm.SetLevelString("debug"))
m["logging enabled"] = prm
prm = Prm{}
require.NoError(b, prm.SetLevelString("error"))
m["logging disabled"] = prm
prm = Prm{}
require.NoError(b, prm.SetLevelString("error"))
require.NoError(b, prm.SetTags([][]string{{"main", "debug"}, {"morph", "debug"}}))
m["logging enabled via tags"] = prm
prm = Prm{}
require.NoError(b, prm.SetLevelString("debug"))
require.NoError(b, prm.SetTags([][]string{{"main", "error"}, {"morph", "debug"}}))
m["logging disabled via tags"] = prm
for k, v := range m {
b.Run(k, func(b *testing.B) {
logger, err := createLogger(v)
require.NoError(b, err)
UpdateLevelForTags(v)
b.ResetTimer()
b.ReportAllocs()
for range b.N {
logger.Info(ctx, "test info")
}
})
}
}
type testCore struct {
core zapcore.Core
}
func (c *testCore) Enabled(lvl zapcore.Level) bool {
return c.core.Enabled(lvl)
}
func (c *testCore) With(fields []zapcore.Field) zapcore.Core {
c.core = c.core.With(fields)
return c
}
func (c *testCore) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
return ce.AddCore(e, c)
}
func (c *testCore) Write(zapcore.Entry, []zapcore.Field) error {
return nil
}
func (c *testCore) Sync() error {
return c.core.Sync()
}
func createLogger(prm Prm) (*Logger, error) {
prm.Options = []zap.Option{zap.WrapCore(func(core zapcore.Core) zapcore.Core {
tc := testCore{core: core}
return &tc
})}
return NewLogger(prm)
}
func TestLoggerOutput(t *testing.T) {
obs, logs := observer.New(zap.NewAtomicLevelAt(zap.DebugLevel))
prm := Prm{}
require.NoError(t, prm.SetLevelString("debug"))
prm.Options = []zap.Option{zap.WrapCore(func(zapcore.Core) zapcore.Core {
return obs
})}
loggerMain, err := NewLogger(prm)
require.NoError(t, err)
UpdateLevelForTags(prm)
loggerMainWith := loggerMain.With(zap.String("key", "value"))
require.Panics(t, func() {
loggerMainWith.WithTag(TagShard)
})
loggerShard := loggerMain.WithTag(TagShard)
loggerShard = loggerShard.With(zap.String("key1", "value1"))
loggerMorph := loggerMain.WithTag(TagMorph)
loggerMorph = loggerMorph.With(zap.String("key2", "value2"))
ctx := context.Background()
loggerMain.Debug(ctx, "main")
loggerMainWith.Debug(ctx, "main with")
loggerShard.Debug(ctx, "shard")
loggerMorph.Debug(ctx, "morph")
require.Len(t, logs.All(), 4)
require.Len(t, logs.FilterFieldKey("key").All(), 1)
require.Len(t, logs.FilterFieldKey("key1").All(), 1)
require.Len(t, logs.FilterFieldKey("key2").All(), 1)
require.Len(t, logs.FilterField(zap.String("tag", TagMain.String())).All(), 2)
require.Len(t, logs.FilterField(zap.String("tag", TagShard.String())).All(), 1)
require.Len(t, logs.FilterField(zap.String("tag", TagMorph.String())).All(), 1)
}