Files
kopia/internal/testlogging/printf.go
Jarek Kowalski 34da3a2415 feat(general): implemented custom log encoder for ZAP (#2116)
Turns out standard ConsoleEncoder is not optimized at all and we're
emitting up to 8 log entries per file. This change avoids massive
amount of allocations and brings in some latency reduction.

Backing up 100k files in a flat directory:

duration: current:7.8 baseline:7.9 change:-1.0 %
repo_size: current:1019954521.4 baseline:1019976318.9 change:-0.0 %
num_files: current:58.0 baseline:58.0 change:0%
avg_heap_objects: current:6682141.0 baseline:7508631.3 change:-11.0 %
avg_heap_bytes: current:845404672.0 baseline:867325413.9 change:-2.5 %
avg_ram: current:156.5 baseline:159.1 change:-1.7 %
max_ram: current:278.3 baseline:287.2 change:-3.1 %
avg_cpu: current:153.8 baseline:156.4 change:-1.7 %
max_cpu: current:298.4 baseline:297.1 change:+0.4 %

Backing up Linux 5.18.4:

duration: current:3.6 baseline:4.2 change:-14.2 %
repo_size: current:1081624213.7 baseline:1081635886.8 change:-0.0 %
num_files: current:60.0 baseline:60.0 change:0%
avg_heap_objects: current:5180192.3 baseline:5831270.7 change:-11.2 %
avg_heap_bytes: current:783468754.2 baseline:804350042.1 change:-2.6 %
avg_ram: current:239.0 baseline:240.6 change:-0.6 %
max_ram: current:384.8 baseline:368.0 change:+4.6 %
avg_cpu: current:259.8 baseline:230.8 change:+12.6 %
max_cpu: current:321.6 baseline:303.9 change:+5.9 %
2022-07-02 13:55:01 +00:00

54 lines
1.4 KiB
Go

package testlogging
import (
"bytes"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"github.com/kopia/kopia/internal/zaplogutil"
"github.com/kopia/kopia/repo/logging"
)
// Printf returns a logger that uses given printf-style function to print log output.
func Printf(printf func(msg string, args ...interface{}), prefix string) *zap.SugaredLogger {
return PrintfLevel(printf, prefix, zapcore.DebugLevel)
}
// PrintfLevel returns a logger that uses given printf-style function to print log output for logs of a given level or above.
func PrintfLevel(printf func(msg string, args ...interface{}), prefix string, level zapcore.Level) *zap.SugaredLogger {
writer := printfWriter{printf, prefix}
return zap.New(
zapcore.NewCore(
zaplogutil.NewStdConsoleEncoder(zaplogutil.StdConsoleEncoderConfig{}),
writer,
level,
),
).Sugar()
}
// PrintfFactory returns LoggerForModuleFunc that uses given printf-style function to print log output.
func PrintfFactory(printf func(msg string, args ...interface{})) logging.LoggerFactory {
return func(module string) *zap.SugaredLogger {
return Printf(printf, "["+module+"] ")
}
}
type printfWriter struct {
printf func(msg string, args ...interface{})
prefix string
}
func (w printfWriter) Write(p []byte) (int, error) {
n := len(p)
w.printf("%s%s", w.prefix, bytes.TrimRight(p, "\n"))
return n, nil
}
func (w printfWriter) Sync() error {
return nil
}