From 130de6ecbcff17d6574d787a95fdd923391fb078 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Tue, 8 Aug 2023 15:11:35 -0700 Subject: [PATCH 1/5] Adding (initial) fortio.org/log entry --- README.md | 53 +++++++++++---------- benchmarks/fortio_test.go | 78 +++++++++++++++++++++++++++++++ benchmarks/go.mod | 6 +++ benchmarks/go.sum | 2 + benchmarks/scenario_bench_test.go | 58 +++++++++++++++++++++-- benchmarks/zap_test.go | 1 + internal/readme/readme.go | 1 + 7 files changed, 170 insertions(+), 29 deletions(-) create mode 100644 benchmarks/fortio_test.go diff --git a/README.md b/README.md index 9de08927b..dc669f288 100644 --- a/README.md +++ b/README.md @@ -66,41 +66,44 @@ Log a message and 10 fields: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 1744 ns/op | +0% | 5 allocs/op -| :zap: zap (sugared) | 2483 ns/op | +42% | 10 allocs/op -| zerolog | 918 ns/op | -47% | 1 allocs/op -| go-kit | 5590 ns/op | +221% | 57 allocs/op -| slog | 5640 ns/op | +223% | 40 allocs/op -| apex/log | 21184 ns/op | +1115% | 63 allocs/op -| logrus | 24338 ns/op | +1296% | 79 allocs/op -| log15 | 26054 ns/op | +1394% | 74 allocs/op +| :zap: zap | 654 ns/op | +0% | 5 allocs/op +| :zap: zap (sugared) | 968 ns/op | +48% | 10 allocs/op +| zerolog | 244 ns/op | -63% | 1 allocs/op +| slog | 2193 ns/op | +235% | 40 allocs/op +| go-kit | 2236 ns/op | +242% | 57 allocs/op +| fortio log | 4361 ns/op | +567% | 122 allocs/op +| apex/log | 10018 ns/op | +1432% | 63 allocs/op +| log15 | 11454 ns/op | +1651% | 74 allocs/op +| logrus | 12139 ns/op | +1756% | 79 allocs/op Log a message with a logger that already has 10 fields of context: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 193 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 227 ns/op | +18% | 1 allocs/op -| zerolog | 81 ns/op | -58% | 0 allocs/op -| slog | 322 ns/op | +67% | 0 allocs/op -| go-kit | 5377 ns/op | +2686% | 56 allocs/op -| apex/log | 19518 ns/op | +10013% | 53 allocs/op -| log15 | 19812 ns/op | +10165% | 70 allocs/op -| logrus | 21997 ns/op | +11297% | 68 allocs/op +| :zap: zap | 65 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 87 ns/op | +34% | 1 allocs/op +| zerolog | 22 ns/op | -66% | 0 allocs/op +| slog | 239 ns/op | +268% | 0 allocs/op +| go-kit | 2524 ns/op | +3783% | 56 allocs/op +| fortio log | 4497 ns/op | +6818% | 111 allocs/op +| log15 | 9383 ns/op | +14335% | 70 allocs/op +| apex/log | 9421 ns/op | +14394% | 53 allocs/op +| logrus | 11024 ns/op | +16860% | 68 allocs/op Log a static string, without any context or `printf`-style templating: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 165 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 212 ns/op | +28% | 1 allocs/op -| zerolog | 95 ns/op | -42% | 0 allocs/op -| slog | 296 ns/op | +79% | 0 allocs/op -| go-kit | 415 ns/op | +152% | 9 allocs/op -| standard library | 422 ns/op | +156% | 2 allocs/op -| apex/log | 1601 ns/op | +870% | 5 allocs/op -| logrus | 3017 ns/op | +1728% | 23 allocs/op -| log15 | 3469 ns/op | +2002% | 20 allocs/op +| :zap: zap | 57 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 75 ns/op | +32% | 1 allocs/op +| zerolog | 18 ns/op | -68% | 0 allocs/op +| go-kit | 245 ns/op | +330% | 9 allocs/op +| slog | 247 ns/op | +333% | 0 allocs/op +| standard library | 347 ns/op | +509% | 2 allocs/op +| fortio log | 470 ns/op | +725% | 8 allocs/op +| apex/log | 796 ns/op | +1296% | 5 allocs/op +| logrus | 1601 ns/op | +2709% | 23 allocs/op +| log15 | 2091 ns/op | +3568% | 20 allocs/op ## Development Status: Stable diff --git a/benchmarks/fortio_test.go b/benchmarks/fortio_test.go new file mode 100644 index 000000000..a0de95aa8 --- /dev/null +++ b/benchmarks/fortio_test.go @@ -0,0 +1,78 @@ +// Copyright (c) 2016 Uber Technologies, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +package benchmarks + +import ( + "io" + + "fortio.org/log" +) + +type shmLogger struct { + fields []log.KeyVal +} + +var fLog = &shmLogger{} + +func (l *shmLogger) Infof(msg string, fields ...interface{}) { + log.Infof(msg, fields...) +} + +func (l *shmLogger) InfoS(msg string, fields ...log.KeyVal) { + log.S(log.Info, msg, fields...) +} + +func (l *shmLogger) InfoContext(msg string) { + log.S(log.Info, msg, l.fields...) +} + +func newFortiolog(fields ...log.KeyVal) *shmLogger { + log.Config = log.DefaultConfig() + log.Config.LogFileAndLine = false + log.Config.JSON = true + log.SetOutput(io.Discard) + log.Color = false // should already be but just in case + fLog.fields = fields + log.SetLogLevel(log.Info) + return fLog +} + +func newDisabledFortiolog(fields ...log.KeyVal) *shmLogger { + logger := newFortiolog() + log.SetLogLevel(log.Error) + logger.fields = fields + return logger +} + +func fakeFortiologFields() []log.KeyVal { + return []log.KeyVal{ + log.Attr("int", _tenInts[0]), + log.Attr("ints", _tenInts), + log.Str("string", _tenStrings[0]), + log.Attr("strings", _tenStrings), + log.Attr("time", _tenTimes[0]), + log.Attr("times", _tenTimes), + log.Attr("user1", _oneUser), + log.Attr("user2", _oneUser), + log.Attr("users", _tenUsers), + log.Attr("error", errExample), + } +} diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 8024e66b2..795b3a660 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -5,6 +5,7 @@ go 1.19 replace go.uber.org/zap => ../ require ( + fortio.org/log v1.10.0 github.com/apex/log v1.9.0 github.com/go-kit/log v0.2.1 github.com/rs/zerolog v1.28.0 @@ -15,6 +16,11 @@ require ( gopkg.in/inconshreveable/log15.v2 v2.0.0-20221122034931-555555054819 ) +replace ( +// fortio.org/log => ../../../fortio.org/log + go.ubder.org/zap => ../ +) + require ( github.com/benbjohnson/clock v1.3.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect diff --git a/benchmarks/go.sum b/benchmarks/go.sum index 4ae6da835..d42596d5c 100644 --- a/benchmarks/go.sum +++ b/benchmarks/go.sum @@ -1,3 +1,5 @@ +fortio.org/log v1.10.0 h1:Id2z9HjGlof0VsIHi2XNI9k+tG7ujODVDnAAvkSgpuE= +fortio.org/log v1.10.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU= github.com/apex/log v1.9.0 h1:FHtw/xuaM8AgmvDDTI9fiwoAL25Sq2cxojnZICUU8l0= github.com/apex/log v1.9.0/go.mod h1:m82fZlWIuiWzWP04XCTXmnX0xRkYYbCdYn8jbJeLBEA= github.com/apex/logs v1.0.0/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo= diff --git a/benchmarks/scenario_bench_test.go b/benchmarks/scenario_bench_test.go index b41b1b50e..56a7ed11d 100644 --- a/benchmarks/scenario_bench_test.go +++ b/benchmarks/scenario_bench_test.go @@ -104,6 +104,15 @@ func BenchmarkDisabledWithoutFields(b *testing.B) { } }) }) + b.Run("fortio.org/log", func(b *testing.B) { + logger := newDisabledFortiolog() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + logger.InfoS(getMessage(0)) + } + }) + }) } @@ -183,7 +192,15 @@ func BenchmarkDisabledAccumulatedContext(b *testing.B) { } }) }) - + b.Run("fortio.org/log", func(b *testing.B) { + logger := newDisabledFortiolog(fakeFortiologFields()...) + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + logger.InfoContext(getMessage(0)) + } + }) + }) } func BenchmarkDisabledAddingFields(b *testing.B) { @@ -253,7 +270,15 @@ func BenchmarkDisabledAddingFields(b *testing.B) { } }) }) - + b.Run("fortio.org/log", func(b *testing.B) { + logger := newDisabledFortiolog() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + logger.InfoS(getMessage(0), fakeFortiologFields()...) + } + }) + }) } func BenchmarkWithoutFields(b *testing.B) { @@ -401,7 +426,15 @@ func BenchmarkWithoutFields(b *testing.B) { } }) }) - + b.Run("fortio.org/log", func(b *testing.B) { + logger := newFortiolog() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + logger.InfoS(getMessage(0)) + } + }) + }) } func BenchmarkAccumulatedContext(b *testing.B) { @@ -531,6 +564,15 @@ func BenchmarkAccumulatedContext(b *testing.B) { } }) }) + b.Run("fortio.org/log", func(b *testing.B) { + logger := newFortiolog(fakeFortiologFields()...) + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + logger.InfoContext(getMessage(0)) + } + }) + }) } @@ -643,5 +685,13 @@ func BenchmarkAddingFields(b *testing.B) { } }) }) - + b.Run("fortio.org/log", func(b *testing.B) { + logger := newFortiolog() + b.ResetTimer() + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + logger.InfoS(getMessage(0), fakeFortiologFields()...) + } + }) + }) } diff --git a/benchmarks/zap_test.go b/benchmarks/zap_test.go index 84784152c..ea4ae10ef 100644 --- a/benchmarks/zap_test.go +++ b/benchmarks/zap_test.go @@ -110,6 +110,7 @@ func newZapLogger(lvl zapcore.Level) *zap.Logger { enc := zapcore.NewJSONEncoder(ec) return zap.New(zapcore.NewCore( enc, + // os.Stderr, // for real/checking the output &ztest.Discarder{}, lvl, )) diff --git a/internal/readme/readme.go b/internal/readme/readme.go index 148765908..7be150ce9 100644 --- a/internal/readme/readme.go +++ b/internal/readme/readme.go @@ -46,6 +46,7 @@ var ( "apex/log": "apex/log", "rs/zerolog": "zerolog", "slog": "slog", + "fortio.org/log": "fortio log", } ) From 8788569d6f24037d9c5fda22636f09bd3bbc2831 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 10 Aug 2023 14:31:44 -0700 Subject: [PATCH 2/5] Use numbers whose alloc isn't optimized away by go --- README.md | 56 +++++++++++++++++++++--------------------- benchmarks/zap_test.go | 25 ++++++++++--------- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/README.md b/README.md index dc669f288..327b0a2d0 100644 --- a/README.md +++ b/README.md @@ -66,44 +66,44 @@ Log a message and 10 fields: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 654 ns/op | +0% | 5 allocs/op -| :zap: zap (sugared) | 968 ns/op | +48% | 10 allocs/op -| zerolog | 244 ns/op | -63% | 1 allocs/op -| slog | 2193 ns/op | +235% | 40 allocs/op -| go-kit | 2236 ns/op | +242% | 57 allocs/op -| fortio log | 4361 ns/op | +567% | 122 allocs/op -| apex/log | 10018 ns/op | +1432% | 63 allocs/op -| log15 | 11454 ns/op | +1651% | 74 allocs/op -| logrus | 12139 ns/op | +1756% | 79 allocs/op +| :zap: zap | 645 ns/op | +0% | 5 allocs/op +| :zap: zap (sugared) | 978 ns/op | +52% | 11 allocs/op +| zerolog | 237 ns/op | -63% | 1 allocs/op +| slog | 2215 ns/op | +243% | 40 allocs/op +| go-kit | 2389 ns/op | +270% | 58 allocs/op +| fortio log | 4798 ns/op | +644% | 120 allocs/op +| apex/log | 9909 ns/op | +1436% | 64 allocs/op +| log15 | 11405 ns/op | +1668% | 75 allocs/op +| logrus | 11966 ns/op | +1755% | 80 allocs/op Log a message with a logger that already has 10 fields of context: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 65 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 87 ns/op | +34% | 1 allocs/op -| zerolog | 22 ns/op | -66% | 0 allocs/op -| slog | 239 ns/op | +268% | 0 allocs/op -| go-kit | 2524 ns/op | +3783% | 56 allocs/op -| fortio log | 4497 ns/op | +6818% | 111 allocs/op -| log15 | 9383 ns/op | +14335% | 70 allocs/op -| apex/log | 9421 ns/op | +14394% | 53 allocs/op -| logrus | 11024 ns/op | +16860% | 68 allocs/op +| :zap: zap | 63 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 80 ns/op | +27% | 1 allocs/op +| zerolog | 26 ns/op | -59% | 0 allocs/op +| slog | 244 ns/op | +287% | 0 allocs/op +| go-kit | 2517 ns/op | +3895% | 56 allocs/op +| fortio log | 4430 ns/op | +6932% | 112 allocs/op +| log15 | 9307 ns/op | +14673% | 70 allocs/op +| apex/log | 9341 ns/op | +14727% | 53 allocs/op +| logrus | 10952 ns/op | +17284% | 68 allocs/op Log a static string, without any context or `printf`-style templating: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 57 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 75 ns/op | +32% | 1 allocs/op -| zerolog | 18 ns/op | -68% | 0 allocs/op -| go-kit | 245 ns/op | +330% | 9 allocs/op -| slog | 247 ns/op | +333% | 0 allocs/op -| standard library | 347 ns/op | +509% | 2 allocs/op -| fortio log | 470 ns/op | +725% | 8 allocs/op -| apex/log | 796 ns/op | +1296% | 5 allocs/op -| logrus | 1601 ns/op | +2709% | 23 allocs/op -| log15 | 2091 ns/op | +3568% | 20 allocs/op +| :zap: zap | 62 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 78 ns/op | +26% | 1 allocs/op +| zerolog | 23 ns/op | -63% | 0 allocs/op +| slog | 233 ns/op | +276% | 0 allocs/op +| go-kit | 247 ns/op | +298% | 9 allocs/op +| standard library | 293 ns/op | +373% | 2 allocs/op +| fortio log | 430 ns/op | +594% | 8 allocs/op +| apex/log | 786 ns/op | +1168% | 5 allocs/op +| logrus | 1477 ns/op | +2282% | 23 allocs/op +| log15 | 2029 ns/op | +3173% | 20 allocs/op ## Development Status: Stable diff --git a/benchmarks/zap_test.go b/benchmarks/zap_test.go index ea4ae10ef..314ede4c3 100644 --- a/benchmarks/zap_test.go +++ b/benchmarks/zap_test.go @@ -34,20 +34,21 @@ import ( var ( errExample = errors.New("fail") - _messages = fakeMessages(1000) - _tenInts = []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0} + _messages = fakeMessages(1000) + // Go optimizes away allocations for numbers below 256 + _tenInts = []int{1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009, 1010} _tenStrings = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"} _tenTimes = []time.Time{ - time.Unix(0, 0), - time.Unix(1, 0), - time.Unix(2, 0), - time.Unix(3, 0), - time.Unix(4, 0), - time.Unix(5, 0), - time.Unix(6, 0), - time.Unix(7, 0), - time.Unix(8, 0), - time.Unix(9, 0), + time.Unix(1000, 0), + time.Unix(1001, 0), + time.Unix(1002, 0), + time.Unix(1003, 0), + time.Unix(1004, 0), + time.Unix(1005, 0), + time.Unix(1006, 0), + time.Unix(1007, 0), + time.Unix(1008, 0), + time.Unix(1009, 0), } _oneUser = &user{ Name: "Jane Doe", From b113ea6a0194e4838a6fae34acf14f7d6d38b970 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 17 Aug 2023 16:57:24 -0700 Subject: [PATCH 3/5] rerun and small fix to slog in case it starts doing like log. io.Discard == checks --- README.md | 56 +++++++++++++++---------------- benchmarks/fortio_test.go | 5 +-- benchmarks/go.mod | 4 +-- benchmarks/go.sum | 2 -- benchmarks/scenario_bench_test.go | 2 +- benchmarks/slog_test.go | 7 ++-- 6 files changed, 37 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index 327b0a2d0..a3573f4f6 100644 --- a/README.md +++ b/README.md @@ -66,44 +66,44 @@ Log a message and 10 fields: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 645 ns/op | +0% | 5 allocs/op -| :zap: zap (sugared) | 978 ns/op | +52% | 11 allocs/op -| zerolog | 237 ns/op | -63% | 1 allocs/op -| slog | 2215 ns/op | +243% | 40 allocs/op -| go-kit | 2389 ns/op | +270% | 58 allocs/op -| fortio log | 4798 ns/op | +644% | 120 allocs/op -| apex/log | 9909 ns/op | +1436% | 64 allocs/op -| log15 | 11405 ns/op | +1668% | 75 allocs/op -| logrus | 11966 ns/op | +1755% | 80 allocs/op +| :zap: zap | 621 ns/op | +0% | 5 allocs/op +| :zap: zap (sugared) | 986 ns/op | +59% | 11 allocs/op +| zerolog | 242 ns/op | -61% | 1 allocs/op +| go-kit | 2162 ns/op | +248% | 58 allocs/op +| slog | 2198 ns/op | +254% | 40 allocs/op +| fortio log | 5112 ns/op | +723% | 120 allocs/op +| apex/log | 9994 ns/op | +1509% | 64 allocs/op +| log15 | 11379 ns/op | +1732% | 75 allocs/op +| logrus | 11970 ns/op | +1828% | 80 allocs/op Log a message with a logger that already has 10 fields of context: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 63 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 80 ns/op | +27% | 1 allocs/op -| zerolog | 26 ns/op | -59% | 0 allocs/op -| slog | 244 ns/op | +287% | 0 allocs/op -| go-kit | 2517 ns/op | +3895% | 56 allocs/op -| fortio log | 4430 ns/op | +6932% | 112 allocs/op -| log15 | 9307 ns/op | +14673% | 70 allocs/op -| apex/log | 9341 ns/op | +14727% | 53 allocs/op -| logrus | 10952 ns/op | +17284% | 68 allocs/op +| :zap: zap | 59 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 82 ns/op | +39% | 1 allocs/op +| zerolog | 18 ns/op | -69% | 0 allocs/op +| slog | 236 ns/op | +300% | 0 allocs/op +| go-kit | 2357 ns/op | +3895% | 56 allocs/op +| fortio log | 4612 ns/op | +7717% | 112 allocs/op +| log15 | 9315 ns/op | +15688% | 70 allocs/op +| apex/log | 9354 ns/op | +15754% | 53 allocs/op +| logrus | 10983 ns/op | +18515% | 68 allocs/op Log a static string, without any context or `printf`-style templating: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 62 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 78 ns/op | +26% | 1 allocs/op -| zerolog | 23 ns/op | -63% | 0 allocs/op -| slog | 233 ns/op | +276% | 0 allocs/op -| go-kit | 247 ns/op | +298% | 9 allocs/op -| standard library | 293 ns/op | +373% | 2 allocs/op -| fortio log | 430 ns/op | +594% | 8 allocs/op -| apex/log | 786 ns/op | +1168% | 5 allocs/op -| logrus | 1477 ns/op | +2282% | 23 allocs/op -| log15 | 2029 ns/op | +3173% | 20 allocs/op +| :zap: zap | 55 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 79 ns/op | +44% | 1 allocs/op +| zerolog | 19 ns/op | -65% | 0 allocs/op +| go-kit | 241 ns/op | +338% | 9 allocs/op +| slog | 242 ns/op | +340% | 0 allocs/op +| standard library | 255 ns/op | +364% | 2 allocs/op +| apex/log | 784 ns/op | +1325% | 5 allocs/op +| fortio log | 828 ns/op | +1405% | 2 allocs/op +| logrus | 1495 ns/op | +2618% | 23 allocs/op +| log15 | 2027 ns/op | +3585% | 20 allocs/op ## Development Status: Stable diff --git a/benchmarks/fortio_test.go b/benchmarks/fortio_test.go index a0de95aa8..e09708d72 100644 --- a/benchmarks/fortio_test.go +++ b/benchmarks/fortio_test.go @@ -46,8 +46,9 @@ func (l *shmLogger) InfoContext(msg string) { func newFortiolog(fields ...log.KeyVal) *shmLogger { log.Config = log.DefaultConfig() - log.Config.LogFileAndLine = false - log.Config.JSON = true + log.Config.LogFileAndLine = false // file/line extraction implies bad perf + log.Config.JSON = true // structured logging comparison + log.Config.GoroutineID = false // other loggers don't have that log.SetOutput(io.Discard) log.Color = false // should already be but just in case fLog.fields = fields diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 795b3a660..7637bbf77 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -17,8 +17,8 @@ require ( ) replace ( -// fortio.org/log => ../../../fortio.org/log - go.ubder.org/zap => ../ +// fortio.org/log => ../../../fortio.org/log + go.ubder.org/zap => ../ ) require ( diff --git a/benchmarks/go.sum b/benchmarks/go.sum index d42596d5c..4ae6da835 100644 --- a/benchmarks/go.sum +++ b/benchmarks/go.sum @@ -1,5 +1,3 @@ -fortio.org/log v1.10.0 h1:Id2z9HjGlof0VsIHi2XNI9k+tG7ujODVDnAAvkSgpuE= -fortio.org/log v1.10.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU= github.com/apex/log v1.9.0 h1:FHtw/xuaM8AgmvDDTI9fiwoAL25Sq2cxojnZICUU8l0= github.com/apex/log v1.9.0/go.mod h1:m82fZlWIuiWzWP04XCTXmnX0xRkYYbCdYn8jbJeLBEA= github.com/apex/logs v1.0.0/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo= diff --git a/benchmarks/scenario_bench_test.go b/benchmarks/scenario_bench_test.go index 56a7ed11d..61f84d4a3 100644 --- a/benchmarks/scenario_bench_test.go +++ b/benchmarks/scenario_bench_test.go @@ -431,7 +431,7 @@ func BenchmarkWithoutFields(b *testing.B) { b.ResetTimer() b.RunParallel(func(pb *testing.PB) { for pb.Next() { - logger.InfoS(getMessage(0)) + logger.Infof(getMessage(0)) } }) }) diff --git a/benchmarks/slog_test.go b/benchmarks/slog_test.go index 36a5a6077..f059e6ab3 100644 --- a/benchmarks/slog_test.go +++ b/benchmarks/slog_test.go @@ -21,17 +21,16 @@ package benchmarks import ( - "io" - + "go.uber.org/zap/internal/ztest" "golang.org/x/exp/slog" ) func newSlog(fields ...slog.Attr) *slog.Logger { - return slog.New(slog.NewJSONHandler(io.Discard).WithAttrs(fields)) + return slog.New(slog.NewJSONHandler(&ztest.Discarder{}).WithAttrs(fields)) } func newDisabledSlog(fields ...slog.Attr) *slog.Logger { - return slog.New(slog.HandlerOptions{Level: slog.LevelError}.NewJSONHandler(io.Discard).WithAttrs(fields)) + return slog.New(slog.HandlerOptions{Level: slog.LevelError}.NewJSONHandler(&ztest.Discarder{}).WithAttrs(fields)) } func fakeSlogFields() []slog.Attr { From 1e075db47b7b3c03d7b06490e2a5b77449843cdd Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Thu, 17 Aug 2023 20:58:43 -0700 Subject: [PATCH 4/5] using release version of fortio.org/log --- README.md | 56 +++++++++++++++++++++++------------------------ benchmarks/go.mod | 6 ++--- benchmarks/go.sum | 2 ++ 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/README.md b/README.md index a3573f4f6..eed14c598 100644 --- a/README.md +++ b/README.md @@ -66,44 +66,44 @@ Log a message and 10 fields: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 621 ns/op | +0% | 5 allocs/op -| :zap: zap (sugared) | 986 ns/op | +59% | 11 allocs/op -| zerolog | 242 ns/op | -61% | 1 allocs/op -| go-kit | 2162 ns/op | +248% | 58 allocs/op -| slog | 2198 ns/op | +254% | 40 allocs/op -| fortio log | 5112 ns/op | +723% | 120 allocs/op -| apex/log | 9994 ns/op | +1509% | 64 allocs/op -| log15 | 11379 ns/op | +1732% | 75 allocs/op -| logrus | 11970 ns/op | +1828% | 80 allocs/op +| :zap: zap | 626 ns/op | +0% | 5 allocs/op +| :zap: zap (sugared) | 982 ns/op | +57% | 11 allocs/op +| zerolog | 236 ns/op | -62% | 1 allocs/op +| go-kit | 2174 ns/op | +247% | 58 allocs/op +| slog | 2175 ns/op | +247% | 40 allocs/op +| fortio log | 5659 ns/op | +804% | 140 allocs/op +| apex/log | 9934 ns/op | +1487% | 64 allocs/op +| log15 | 11288 ns/op | +1703% | 75 allocs/op +| logrus | 12118 ns/op | +1836% | 80 allocs/op Log a message with a logger that already has 10 fields of context: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 59 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 82 ns/op | +39% | 1 allocs/op -| zerolog | 18 ns/op | -69% | 0 allocs/op -| slog | 236 ns/op | +300% | 0 allocs/op -| go-kit | 2357 ns/op | +3895% | 56 allocs/op -| fortio log | 4612 ns/op | +7717% | 112 allocs/op -| log15 | 9315 ns/op | +15688% | 70 allocs/op -| apex/log | 9354 ns/op | +15754% | 53 allocs/op -| logrus | 10983 ns/op | +18515% | 68 allocs/op +| :zap: zap | 63 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 82 ns/op | +30% | 1 allocs/op +| zerolog | 18 ns/op | -71% | 0 allocs/op +| slog | 242 ns/op | +284% | 0 allocs/op +| go-kit | 2341 ns/op | +3616% | 56 allocs/op +| fortio log | 5162 ns/op | +8094% | 131 allocs/op +| log15 | 9321 ns/op | +14695% | 70 allocs/op +| apex/log | 9421 ns/op | +14854% | 53 allocs/op +| logrus | 10950 ns/op | +17281% | 68 allocs/op Log a static string, without any context or `printf`-style templating: | Package | Time | Time % to zap | Objects Allocated | | :------ | :--: | :-----------: | :---------------: | -| :zap: zap | 55 ns/op | +0% | 0 allocs/op -| :zap: zap (sugared) | 79 ns/op | +44% | 1 allocs/op -| zerolog | 19 ns/op | -65% | 0 allocs/op -| go-kit | 241 ns/op | +338% | 9 allocs/op -| slog | 242 ns/op | +340% | 0 allocs/op -| standard library | 255 ns/op | +364% | 2 allocs/op -| apex/log | 784 ns/op | +1325% | 5 allocs/op -| fortio log | 828 ns/op | +1405% | 2 allocs/op -| logrus | 1495 ns/op | +2618% | 23 allocs/op -| log15 | 2027 ns/op | +3585% | 20 allocs/op +| :zap: zap | 52 ns/op | +0% | 0 allocs/op +| :zap: zap (sugared) | 80 ns/op | +54% | 1 allocs/op +| zerolog | 17 ns/op | -67% | 0 allocs/op +| slog | 230 ns/op | +342% | 0 allocs/op +| go-kit | 243 ns/op | +367% | 9 allocs/op +| standard library | 309 ns/op | +494% | 2 allocs/op +| apex/log | 787 ns/op | +1413% | 5 allocs/op +| fortio log | 827 ns/op | +1490% | 2 allocs/op +| logrus | 1495 ns/op | +2775% | 23 allocs/op +| log15 | 2030 ns/op | +3804% | 20 allocs/op ## Development Status: Stable diff --git a/benchmarks/go.mod b/benchmarks/go.mod index 7637bbf77..dbcd57b0c 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -5,7 +5,7 @@ go 1.19 replace go.uber.org/zap => ../ require ( - fortio.org/log v1.10.0 + fortio.org/log v1.11.0 github.com/apex/log v1.9.0 github.com/go-kit/log v0.2.1 github.com/rs/zerolog v1.28.0 @@ -16,10 +16,8 @@ require ( gopkg.in/inconshreveable/log15.v2 v2.0.0-20221122034931-555555054819 ) -replace ( // fortio.org/log => ../../../fortio.org/log - go.ubder.org/zap => ../ -) +replace go.ubder.org/zap => ../ require ( github.com/benbjohnson/clock v1.3.0 // indirect diff --git a/benchmarks/go.sum b/benchmarks/go.sum index 4ae6da835..1c0c0e1fd 100644 --- a/benchmarks/go.sum +++ b/benchmarks/go.sum @@ -1,3 +1,5 @@ +fortio.org/log v1.11.0 h1:w7ueGPGbXz0A3+ApMz/5Q9gwEMrwSo/ohTlLo2Um6dU= +fortio.org/log v1.11.0/go.mod h1:u/8/2lyczXq52aT5Nw6reD+3cR6m/EbS2jBiIYhgiTU= github.com/apex/log v1.9.0 h1:FHtw/xuaM8AgmvDDTI9fiwoAL25Sq2cxojnZICUU8l0= github.com/apex/log v1.9.0/go.mod h1:m82fZlWIuiWzWP04XCTXmnX0xRkYYbCdYn8jbJeLBEA= github.com/apex/logs v1.0.0/go.mod h1:XzxuLZ5myVHDy9SAmYpamKKRNApGj54PfYLcFrXqDwo= From 812df0a53e725f6323801242eb99b92dae7cc665 Mon Sep 17 00:00:00 2001 From: Laurent Demailly Date: Fri, 18 Aug 2023 15:58:44 -0700 Subject: [PATCH 5/5] remove dup replace go.uber.org/zap => ../ --- benchmarks/go.mod | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/benchmarks/go.mod b/benchmarks/go.mod index dbcd57b0c..95d9dac48 100644 --- a/benchmarks/go.mod +++ b/benchmarks/go.mod @@ -4,6 +4,8 @@ go 1.19 replace go.uber.org/zap => ../ +// replace fortio.org/log => ../../../fortio.org/log // remove this before any merging + require ( fortio.org/log v1.11.0 github.com/apex/log v1.9.0 @@ -16,9 +18,6 @@ require ( gopkg.in/inconshreveable/log15.v2 v2.0.0-20221122034931-555555054819 ) -// fortio.org/log => ../../../fortio.org/log -replace go.ubder.org/zap => ../ - require ( github.com/benbjohnson/clock v1.3.0 // indirect github.com/go-logfmt/logfmt v0.5.1 // indirect