From ee0c2307350c077f500df5f05d994bafd7b67744 Mon Sep 17 00:00:00 2001 From: Phus Lu Date: Tue, 23 Apr 2024 10:22:06 +0800 Subject: [PATCH] Update benchmark.yml --- .github/workflows/benchmark.yml | 22 +++++---- README.md | 82 ++++++++++++++++----------------- 2 files changed, 53 insertions(+), 51 deletions(-) diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml index 7da11cbf..ad21ed69 100644 --- a/.github/workflows/benchmark.yml +++ b/.github/workflows/benchmark.yml @@ -15,14 +15,16 @@ jobs: with: go-version: '1.22' check-latest: true - - name: Benchmark + - uses: actions/checkout@v4 + - name: go mod tidy run: | - curl -sL https://github.com/${GITHUB_REPOSITORY}/raw/master/README.md | \ - awk '{if($0 ~ "// go test -v"){a=1;b=1};if($0 ~ "```" && b=1){b=0};if (a&&b) {print}}' | \ - tee log_test.go - export GOPROXY=direct - go mod init main - go get github.com/${GITHUB_REPOSITORY}@master - go get github.com/rs/zerolog - go get go.uber.org/zap - head -1 log_test.go | cut -b3- | sed -E 's#\r##' | bash -xe + mkdir bench + cd bench + awk '{if($0 ~ "// go test -v"){a=1;b=1};if($0 ~ "```" && b=1){b=0};if (a&&b) {print}}' ../README.md | tee bench_test.go + go mod init bench + go mod tidy + go mod edit -replace github.com/phuslu/log=../ + - name: Throughput benchmarks + working-directory: ./bench + run: | + go test -v -cpu=4 -run=none -bench=. -benchtime=10s -benchmem diff --git a/README.md b/README.md index b7394cc4..301cf994 100644 --- a/README.md +++ b/README.md @@ -879,31 +879,6 @@ func BenchmarkNormalPhusLog(b *testing.B) { } } -func BenchmarkInterfaceZap(b *testing.B) { - logger := zap.New(zapcore.NewCore( - zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), - zapcore.AddSync(io.Discard), - zapcore.InfoLevel, - )).Sugar() - for i := 0; i < b.N; i++ { - logger.Infow(msg, "object", &obj) - } -} - -func BenchmarkInterfaceZeroLog(b *testing.B) { - logger := zerolog.New(io.Discard).With().Timestamp().Logger() - for i := 0; i < b.N; i++ { - logger.Info().Interface("object", &obj).Msg(msg) - } -} - -func BenchmarkInterfacePhusLog(b *testing.B) { - logger := log.Logger{Writer: log.IOWriter{io.Discard}} - for i := 0; i < b.N; i++ { - logger.Info().Interface("object", &obj).Msg(msg) - } -} - func BenchmarkPrintfZap(b *testing.B) { logger := zap.New(zapcore.NewCore( zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), @@ -954,6 +929,31 @@ func BenchmarkCallerPhusLog(b *testing.B) { logger.Info().Str("rate", "15").Int("low", 16).Float32("high", 123.2).Msg(msg) } } + +func BenchmarkInterfaceZap(b *testing.B) { + logger := zap.New(zapcore.NewCore( + zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()), + zapcore.AddSync(io.Discard), + zapcore.InfoLevel, + )).Sugar() + for i := 0; i < b.N; i++ { + logger.Infow(msg, "object", &obj) + } +} + +func BenchmarkInterfaceZeroLog(b *testing.B) { + logger := zerolog.New(io.Discard).With().Timestamp().Logger() + for i := 0; i < b.N; i++ { + logger.Info().Interface("object", &obj).Msg(msg) + } +} + +func BenchmarkInterfacePhusLog(b *testing.B) { + logger := log.Logger{Writer: log.IOWriter{io.Discard}} + for i := 0; i < b.N; i++ { + logger.Info().Interface("object", &obj).Msg(msg) + } +} ``` A Performance result as below, for daily benchmark results see [github actions][benchmark] ``` @@ -961,28 +961,28 @@ goos: linux goarch: amd64 cpu: AMD EPYC 7763 64-Core Processor -BenchmarkDisableZap-4 190298439 62.62 ns/op 192 B/op 1 allocs/op -BenchmarkDisableZeroLog-4 1000000000 9.014 ns/op 0 B/op 0 allocs/op -BenchmarkDisablePhusLog-4 1000000000 9.604 ns/op 0 B/op 0 allocs/op +BenchmarkDisableZap-4 185592676 64.84 ns/op 192 B/op 1 allocs/op +BenchmarkDisableZeroLog-4 1000000000 9.938 ns/op 0 B/op 0 allocs/op +BenchmarkDisablePhusLog-4 1000000000 9.605 ns/op 0 B/op 0 allocs/op -BenchmarkNormalZap-4 16307428 738.0 ns/op 192 B/op 1 allocs/op -BenchmarkNormalZeroLog-4 33277992 359.5 ns/op 0 B/op 0 allocs/op -BenchmarkNormalPhusLog-4 44305356 269.8 ns/op 0 B/op 0 allocs/op +BenchmarkNormalZap-4 16546404 730.1 ns/op 192 B/op 1 allocs/op +BenchmarkNormalZeroLog-4 34346268 347.4 ns/op 0 B/op 0 allocs/op +BenchmarkNormalPhusLog-4 53834917 227.8 ns/op 0 B/op 0 allocs/op -BenchmarkInterfaceZap-4 10783807 1096 ns/op 224 B/op 2 allocs/op -BenchmarkInterfaceZeroLog-4 19654053 604.0 ns/op 48 B/op 1 allocs/op -BenchmarkInterfacePhusLog-4 21652580 552.4 ns/op 0 B/op 0 allocs/op +BenchmarkPrintfZap-4 13039936 931.5 ns/op 80 B/op 1 allocs/op +BenchmarkPrintfZeroLog-4 18802732 641.6 ns/op 80 B/op 1 allocs/op +BenchmarkPrintfPhusLog-4 24678733 488.3 ns/op 0 B/op 0 allocs/op -BenchmarkPrintfZap-4 13087916 938.2 ns/op 80 B/op 1 allocs/op -BenchmarkPrintfZeroLog-4 18128668 651.0 ns/op 80 B/op 1 allocs/op -BenchmarkPrintfPhusLog-4 23203374 517.9 ns/op 0 B/op 0 allocs/op +BenchmarkCallerZap-4 5967156 2016 ns/op 440 B/op 3 allocs/op +BenchmarkCallerZeroLog-4 9310912 1286 ns/op 304 B/op 4 allocs/op +BenchmarkCallerPhusLog-4 17120493 705.3 ns/op 232 B/op 2 allocs/op -BenchmarkCallerZap-4 5881387 2017 ns/op 440 B/op 3 allocs/op -BenchmarkCallerZeroLog-4 9414148 1271 ns/op 288 B/op 4 allocs/op -BenchmarkCallerPhusLog-4 16249498 737.2 ns/op 232 B/op 2 allocs/op +BenchmarkInterfaceZap-4 10968961 1093 ns/op 224 B/op 2 allocs/op +BenchmarkInterfaceZeroLog-4 19989060 598.5 ns/op 48 B/op 1 allocs/op +BenchmarkInterfacePhusLog-4 23181860 519.4 ns/op 0 B/op 0 allocs/op PASS -ok command-line-arguments 192.224s +ok command-line-arguments 194.383s ``` This library uses the following special techniques to achieve better performance, 1. handwriting time formatting