以下是zap的benchmarks代码所跑出的结果。

goos: linux
goarch: amd64
pkg: go.uber.org/zap/benchmarks

BenchmarkDisabledWithoutFields为打印静态字符串的性能对比,除了sugar和std的Formatting这两个是按printf打印,且输出到/dev/null

BenchmarkDisabledWithoutFields/Zap-8 85337349 12.4 ns/op
BenchmarkDisabledWithoutFields/Zap.Check-8 91927620 11.9 ns/op
BenchmarkDisabledWithoutFields/Zap.Sugar-8 100000000 11.2 ns/op
BenchmarkDisabledWithoutFields/Zap.SugarFormatting-8 267057 4464 ns/op
BenchmarkDisabledWithoutFields/std.Formatting-8 302109 3826 ns/op
BenchmarkDisabledWithoutFields/apex/log-8 536362050 2.16 ns/op
BenchmarkDisabledWithoutFields/sirupsen/logrus-8 134507427 8.91 ns/op
BenchmarkDisabledWithoutFields/rs/zerolog-8 920809478 1.27 ns/op

— BENCH: BenchmarkDisabledWithoutFields

BenchmarkDisabledAccumulatedContext为打印静态字符串,且在logger申请时带上了withField()有9种类型的数据,除了sugar的Formatting还叠加了printf打印

​ scenario_bench_test.go:32: Logging at a disabled level without any structured context.
BenchmarkDisabledAccumulatedContext/Zap-8 86510926 13.5 ns/op
BenchmarkDisabledAccumulatedContext/Zap.Check-8 89121138 13.0 ns/op
BenchmarkDisabledAccumulatedContext/Zap.Sugar-8 100000000 11.7 ns/op
BenchmarkDisabledAccumulatedContext/Zap.SugarFormatting-8 12378014 95.9 ns/op
BenchmarkDisabledAccumulatedContext/apex/log-8 1000000000 0.879 ns/op
BenchmarkDisabledAccumulatedContext/sirupsen/logrus-8 100000000 11.1 ns/op
BenchmarkDisabledAccumulatedContext/rs/zerolog-8 728206543 1.38 ns/op

— BENCH: BenchmarkDisabledAccumulatedContext

BenchmarkDisabledAddingFields为打印静态字符串,且在logger打印时带上了withField()有9种类型的数据

​ scenario_bench_test.go:111: Logging at a disabled level with some accumulated context.
BenchmarkDisabledAddingFields/Zap-8 6410576 180 ns/op
BenchmarkDisabledAddingFields/Zap.Check-8 85936351 13.9 ns/op
BenchmarkDisabledAddingFields/Zap.Sugar-8 13037977 92.3 ns/op
BenchmarkDisabledAddingFields/apex/log-8 2593590 464 ns/op
BenchmarkDisabledAddingFields/sirupsen/logrus-8 1694652 706 ns/op
BenchmarkDisabledAddingFields/rs/zerolog-8 17667589 67.4 ns/op

— BENCH: BenchmarkDisabledAddingFields

BenchmarkWithoutFields 为打印静态字符串的性能对比,跟BenchmarkDisabledWithoutFields的区别是使用了debug日志级别,而不是error日志级别。除了sugar和stdlib.Printf和zerolog这三个的Formatting是按printf打印

​ scenario_bench_test.go:180: Logging at a disabled level, adding context at each log site.
BenchmarkWithoutFields/Zap-8 6590036 184 ns/op
BenchmarkWithoutFields/Zap.Check-8 6278846 198 ns/op
BenchmarkWithoutFields/Zap.CheckSampled-8 21960214 53.5 ns/op
BenchmarkWithoutFields/Zap.Sugar-8 3862297 275 ns/op
BenchmarkWithoutFields/Zap.SugarFormatting-8 231350 4924 ns/op
BenchmarkWithoutFields/apex/log-8 563102 2083 ns/op
BenchmarkWithoutFields/go-kit/kit/log-8 3193994 386 ns/op
BenchmarkWithoutFields/inconshreveable/log15-8 240975 4441 ns/op
BenchmarkWithoutFields/sirupsen/logrus-8 339157 3361 ns/op
BenchmarkWithoutFields/stdlib.Println-8 3062670 392 ns/op
BenchmarkWithoutFields/stdlib.Printf-8 311422 4358 ns/op
BenchmarkWithoutFields/rs/zerolog-8 8673320 137 ns/op
BenchmarkWithoutFields/rs/zerolog.Formatting-8 242422 5823 ns/op
BenchmarkWithoutFields/rs/zerolog.Check-8 6948417 187 ns/op

— BENCH: BenchmarkWithoutFields

BenchmarkAccumulatedContext 为打印静态字符串的性能对比,且在logger申请时带上了withField()有9种类型的数据,使用了debug日志级别,而不是error日志级别。除了sugar和zerolog的Formatting是按printf打印

​ scenario_bench_test.go:240: Logging without any structured context.
BenchmarkAccumulatedContext/Zap-8 3980222 255 ns/op
BenchmarkAccumulatedContext/Zap.Check-8 5550046 209 ns/op
BenchmarkAccumulatedContext/Zap.CheckSampled-8 20133012 57.5 ns/op
BenchmarkAccumulatedContext/Zap.Sugar-8 4194475 295 ns/op
BenchmarkAccumulatedContext/Zap.SugarFormatting-8 229069 4973 ns/op
BenchmarkAccumulatedContext/apex/log-8 33429 30532 ns/op
BenchmarkAccumulatedContext/go-kit/kit/log-8 154054 8585 ns/op
BenchmarkAccumulatedContext/inconshreveable/log15-8 53823 21170 ns/op
BenchmarkAccumulatedContext/sirupsen/logrus-8 38761 31527 ns/op
BenchmarkAccumulatedContext/rs/zerolog-8 8759184 150 ns/op
BenchmarkAccumulatedContext/rs/zerolog.Check-8 7921603 151 ns/op
BenchmarkAccumulatedContext/rs/zerolog.Formatting-8 270817 4438 ns/op

— BENCH: BenchmarkAccumulatedContext

BenchmarkAddingFields 且在logger打印时带上了withField()有9种类型的数据,使用了debug日志级别,而不是error日志级别。

​ scenario_bench_test.go:378: Logging with some accumulated context.
BenchmarkAddingFields/Zap-8 644416 1690 ns/op
BenchmarkAddingFields/Zap.Check-8 639225 1690 ns/op
BenchmarkAddingFields/Zap.CheckSampled-8 4912309 245 ns/op
BenchmarkAddingFields/Zap.Sugar-8 515523 2038 ns/op
BenchmarkAddingFields/apex/log-8 38698 30643 ns/op
BenchmarkAddingFields/go-kit/kit/log-8 154527 8435 ns/op
BenchmarkAddingFields/inconshreveable/log15-8 36103 33431 ns/op
BenchmarkAddingFields/sirupsen/logrus-8 34834 33870 ns/op
BenchmarkAddingFields/rs/zerolog-8 184165 7042 ns/op
BenchmarkAddingFields/rs/zerolog.Check-8 160432 7150 ns/op

— BENCH: BenchmarkAddingFields

BenchmarkCompStdAddingFields不在zap的代码库里,目的是跟标准库做最直接的性能对比,输出到/dev/null

BenchmarkCompStdAddingFields/Zap-8 772062 1501 ns/op
BenchmarkCompStdAddingFields/Zap.Check-8 769766 1541 ns/op
BenchmarkCompStdAddingFields/Zap.Sugar-8 587250 1829 ns/op
BenchmarkCompStdAddingFields/stdlib.Printf-8 285757 3962 ns/op
BenchmarkCompStdAddingFields/apex/log-8 39156 30363 ns/op
BenchmarkCompStdAddingFields/sirupsen/logrus-8 35476 33387 ns/op
BenchmarkCompStdAddingFields/rs/zerolog-8 162192 6737 ns/op

— BENCH: BenchmarkCompStdAddingFields

结论:

如果使用printf(“%s”, str)这种方式打印日志的话,标准库是性能最好的,zap和zerolog性能会略微差些。

如果是withfield()的方式,则zap的性能可以超过标准库printf方式的性能。

withfield使用举例:

1
2
3
4
5
6
7
logger, _ := zap.NewProduction()
defer logger.Sync()
logger.Info("failed to fetch URL",
zap.String("url", url),
zap.Int("attempt", 3),
zap.Duration("backoff", time.Second),
)