Skip to content

Commit

Permalink
✅ test: update benchmark tests and update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 14, 2023
1 parent 0c3609b commit 2eac8d4
Show file tree
Hide file tree
Showing 13 changed files with 184 additions and 103 deletions.
33 changes: 20 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -646,39 +646,46 @@ go test ./...

### Benchmarks

Benchmark code at [_example/bench_loglibs_test.go](_example/bench_loglibs_test.go)

```bash
make test-bench
```

> record ad 2022.11.08
Benchmarks for `slog` and other log packages:

> **Note**: test and record ad 2023.04.13
```shell
% make test-bench
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
BenchmarkZapNegative
BenchmarkZapNegative-4 123297997 110.4 ns/op 192 B/op 1 allocs/op
BenchmarkZapNegative-4 8381674 1429 ns/op 216 B/op 3 allocs/op
BenchmarkZapSugarNegative
BenchmarkZapSugarNegative-4 8655980 1383 ns/op 104 B/op 4 allocs/op
BenchmarkZeroLogNegative
BenchmarkZeroLogNegative-4 891508806 13.36 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogNegative-4 14173719 849.8 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogNegative
BenchmarkPhusLogNegative-4 811990076 14.74 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogNegative-4 27456256 451.2 ns/op 0 B/op 0 allocs/op
BenchmarkLogrusNegative
BenchmarkLogrusNegative-4 242633541 49.40 ns/op 16 B/op 1 allocs/op
BenchmarkLogrusNegative-4 2550771 4784 ns/op 608 B/op 17 allocs/op
BenchmarkGookitSlogNegative
>>> BenchmarkGookitSlogNegative-4 29102253 422.8 ns/op 125 B/op 4 allocs/op
>>>> BenchmarkGookitSlogNegative-4 8798220 1375 ns/op 120 B/op 3 allocs/op
BenchmarkZapPositive
BenchmarkZapPositive-4 9772791 1194 ns/op 192 B/op 1 allocs/op
BenchmarkZapPositive-4 10302483 1167 ns/op 192 B/op 1 allocs/op
BenchmarkZapSugarPositive
BenchmarkZapSugarPositive-4 3833311 3154 ns/op 344 B/op 7 allocs/op
BenchmarkZeroLogPositive
BenchmarkZeroLogPositive-4 13944360 856.8 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogPositive-4 14120524 846.7 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogPositive
BenchmarkPhusLogPositive-4 27839614 431.2 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogPositive-4 27152686 434.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogrusPositive
BenchmarkLogrusPositive-4 2621076 4583 ns/op 608 B/op 17 allocs/op
BenchmarkLogrusPositive-4 2601892 4691 ns/op 608 B/op 17 allocs/op
BenchmarkGookitSlogPositive
>>> BenchmarkGookitSlogPositive-4 8908768 1359 ns/op 149 B/op 5 allocs/op
>>>> BenchmarkGookitSlogPositive-4 8997104 1340 ns/op 120 B/op 3 allocs/op
PASS
ok command-line-arguments 149.379s
ok command-line-arguments 167.095s
```

## Gookit packages
Expand Down
35 changes: 21 additions & 14 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -652,39 +652,46 @@ go test -v ./...

### 性能压测

Benchmark code at [_example/bench_loglibs_test.go](_example/bench_loglibs_test.go)

```bash
make test-bench
```

> record ad 2022.11.08
Benchmarks for `slog` and other log packages:

```text
% make test-bench
> **Note**: test and record ad 2023.04.13
```shell
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
BenchmarkZapNegative
BenchmarkZapNegative-4 123297997 110.4 ns/op 192 B/op 1 allocs/op
BenchmarkZapNegative-4 8381674 1429 ns/op 216 B/op 3 allocs/op
BenchmarkZapSugarNegative
BenchmarkZapSugarNegative-4 8655980 1383 ns/op 104 B/op 4 allocs/op
BenchmarkZeroLogNegative
BenchmarkZeroLogNegative-4 891508806 13.36 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogNegative-4 14173719 849.8 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogNegative
BenchmarkPhusLogNegative-4 811990076 14.74 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogNegative-4 27456256 451.2 ns/op 0 B/op 0 allocs/op
BenchmarkLogrusNegative
BenchmarkLogrusNegative-4 242633541 49.40 ns/op 16 B/op 1 allocs/op
BenchmarkLogrusNegative-4 2550771 4784 ns/op 608 B/op 17 allocs/op
BenchmarkGookitSlogNegative
>>> BenchmarkGookitSlogNegative-4 29102253 422.8 ns/op 125 B/op 4 allocs/op
>>>> BenchmarkGookitSlogNegative-4 8798220 1375 ns/op 120 B/op 3 allocs/op
BenchmarkZapPositive
BenchmarkZapPositive-4 9772791 1194 ns/op 192 B/op 1 allocs/op
BenchmarkZapPositive-4 10302483 1167 ns/op 192 B/op 1 allocs/op
BenchmarkZapSugarPositive
BenchmarkZapSugarPositive-4 3833311 3154 ns/op 344 B/op 7 allocs/op
BenchmarkZeroLogPositive
BenchmarkZeroLogPositive-4 13944360 856.8 ns/op 0 B/op 0 allocs/op
BenchmarkZeroLogPositive-4 14120524 846.7 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogPositive
BenchmarkPhusLogPositive-4 27839614 431.2 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogPositive-4 27152686 434.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogrusPositive
BenchmarkLogrusPositive-4 2621076 4583 ns/op 608 B/op 17 allocs/op
BenchmarkLogrusPositive-4 2601892 4691 ns/op 608 B/op 17 allocs/op
BenchmarkGookitSlogPositive
>>> BenchmarkGookitSlogPositive-4 8908768 1359 ns/op 149 B/op 5 allocs/op
>>>> BenchmarkGookitSlogPositive-4 8997104 1340 ns/op 120 B/op 3 allocs/op
PASS
ok command-line-arguments 149.379s
ok command-line-arguments 167.095s
```

## Gookit packages
Expand Down
46 changes: 41 additions & 5 deletions _example/bench_loglibs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,47 @@

Run benchmark: `make test-bench`

- on each test will update all package to latest.
> **Note**: on each test will update all package to latest.
## v0.5.1 - 2023.04.13

> **Note**: test and record ad 2023.04.13
```shell
goos: darwin
goarch: amd64
cpu: Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
BenchmarkZapNegative
BenchmarkZapNegative-4 8381674 1429 ns/op 216 B/op 3 allocs/op
BenchmarkZapSugarNegative
BenchmarkZapSugarNegative-4 8655980 1383 ns/op 104 B/op 4 allocs/op
BenchmarkZeroLogNegative
BenchmarkZeroLogNegative-4 14173719 849.8 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogNegative
BenchmarkPhusLogNegative-4 27456256 451.2 ns/op 0 B/op 0 allocs/op
BenchmarkLogrusNegative
BenchmarkLogrusNegative-4 2550771 4784 ns/op 608 B/op 17 allocs/op
BenchmarkGookitSlogNegative
BenchmarkGookitSlogNegative-4 8798220 1375 ns/op 120 B/op 3 allocs/op
BenchmarkZapPositive
BenchmarkZapPositive-4 10302483 1167 ns/op 192 B/op 1 allocs/op
BenchmarkZapSugarPositive
BenchmarkZapSugarPositive-4 3833311 3154 ns/op 344 B/op 7 allocs/op
BenchmarkZeroLogPositive
BenchmarkZeroLogPositive-4 14120524 846.7 ns/op 0 B/op 0 allocs/op
BenchmarkPhusLogPositive
BenchmarkPhusLogPositive-4 27152686 434.9 ns/op 0 B/op 0 allocs/op
BenchmarkLogrusPositive
BenchmarkLogrusPositive-4 2601892 4691 ns/op 608 B/op 17 allocs/op
BenchmarkGookitSlogPositive
BenchmarkGookitSlogPositive-4 8997104 1340 ns/op 120 B/op 3 allocs/op
PASS
ok command-line-arguments 167.095s
```

## v0.3.5 - 2022.11.08

> record ad 2022.11.08
> **Note**: test and record ad 2022.11.08
```shell
% make test-bench
Expand Down Expand Up @@ -39,7 +75,7 @@ ok command-line-arguments 149.379s

## v0.3.0

> record ad 2022.04.27
> **Note**: test and record ad 2022.04.27
```shell
% make test-bench
Expand Down Expand Up @@ -72,7 +108,7 @@ ok command-line-arguments 146.669s

### beta 2022.04.17

> record ad 2022.04.17
> **Note**: test and record ad 2022.04.17
```shell
$ go test -v -cpu=4 -run=none -bench=. -benchtime=10s -benchmem bench_loglibs_test.go
Expand Down Expand Up @@ -106,7 +142,7 @@ ok command-line-arguments 145.175s

## v0.2.1

> record ad 2022.04.17
> **Note**: test and record ad 2022.04.17
```shell
$ go test -v -cpu=4 -run=none -bench=. -benchtime=10s -benchmem bench_loglibs_test.go
Expand Down
70 changes: 52 additions & 18 deletions _example/bench_loglibs_test.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package main

import (
"io/ioutil"
"io"
"testing"

"github.com/gookit/slog"
"github.com/gookit/slog/handler"
"github.com/phuslu/log"
phuslu "github.com/phuslu/log"
"github.com/rs/zerolog"
"github.com/sirupsen/logrus"
"go.uber.org/zap"
Expand All @@ -24,9 +24,9 @@ var msg = "The quick brown fox jumps over the lazy dog"

func BenchmarkZapNegative(b *testing.B) {
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(ioutil.Discard),
zapcore.ErrorLevel,
zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(io.Discard),
zapcore.InfoLevel,
))

b.ReportAllocs()
Expand All @@ -36,9 +36,26 @@ func BenchmarkZapNegative(b *testing.B) {
}
}

func BenchmarkZapSugarNegative(b *testing.B) {
logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(io.Discard),
// zapcore.AddSync(os.Stdout),
zapcore.InfoLevel,
)).Sugar()

// logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
// return

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
}
}

func BenchmarkZeroLogNegative(b *testing.B) {
logger := zerolog.New(ioutil.Discard).With().Timestamp().Logger()
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
logger := zerolog.New(io.Discard).With().Timestamp().Logger().Level(zerolog.InfoLevel)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -48,7 +65,7 @@ func BenchmarkZeroLogNegative(b *testing.B) {
}

func BenchmarkPhusLogNegative(b *testing.B) {
logger := log.Logger{Level: log.ErrorLevel, Writer: log.IOWriter{Writer: ioutil.Discard}}
logger := phuslu.Logger{Level: phuslu.InfoLevel, Writer: phuslu.IOWriter{Writer: io.Discard}}

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -60,8 +77,8 @@ func BenchmarkPhusLogNegative(b *testing.B) {
// "github.com/sirupsen/logrus"
func BenchmarkLogrusNegative(b *testing.B) {
logger := logrus.New()
logger.Out = ioutil.Discard
logger.Level = logrus.ErrorLevel
logger.Out = io.Discard
logger.Level = logrus.InfoLevel

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -72,10 +89,14 @@ func BenchmarkLogrusNegative(b *testing.B) {

func BenchmarkGookitSlogNegative(b *testing.B) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(ioutil.Discard, []slog.Level{slog.ErrorLevel}),
handler.NewIOWriter(io.Discard, []slog.Level{slog.InfoLevel}),
// handler.NewIOWriter(os.Stdout, []slog.Level{slog.InfoLevel}),
)
logger.ReportCaller = false

// logger.Info("rate", "15", "low", 16, "high", 123.2, msg)
// return

b.ReportAllocs()
b.ResetTimer()

Expand All @@ -87,7 +108,7 @@ func BenchmarkGookitSlogNegative(b *testing.B) {
func BenchmarkZapPositive(b *testing.B) {
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(ioutil.Discard),
zapcore.AddSync(io.Discard),
zapcore.InfoLevel,
))

Expand All @@ -98,9 +119,22 @@ func BenchmarkZapPositive(b *testing.B) {
}
}

func BenchmarkZapSugarPositive(b *testing.B) {
logger := zap.New(zapcore.NewCore(
zapcore.NewConsoleEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(io.Discard),
zapcore.InfoLevel,
)).Sugar()

b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
logger.Info(msg, zap.String("rate", "15"), zap.Int("low", 16), zap.Float32("high", 123.2))
}
}

func BenchmarkZeroLogPositive(b *testing.B) {
logger := zerolog.New(ioutil.Discard).With().Timestamp().Logger()
zerolog.SetGlobalLevel(zerolog.InfoLevel)
logger := zerolog.New(io.Discard).With().Timestamp().Logger().Level(zerolog.InfoLevel)

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -110,7 +144,7 @@ func BenchmarkZeroLogPositive(b *testing.B) {
}

func BenchmarkPhusLogPositive(b *testing.B) {
logger := log.Logger{Writer: log.IOWriter{Writer: ioutil.Discard}}
logger := phuslu.Logger{Level: phuslu.InfoLevel, Writer: phuslu.IOWriter{Writer: io.Discard}}

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -121,8 +155,8 @@ func BenchmarkPhusLogPositive(b *testing.B) {

func BenchmarkLogrusPositive(b *testing.B) {
logger := logrus.New()
logger.Out = ioutil.Discard
logger.Level = logrus.TraceLevel
logger.Out = io.Discard
logger.Level = logrus.InfoLevel

b.ReportAllocs()
b.ResetTimer()
Expand All @@ -133,7 +167,7 @@ func BenchmarkLogrusPositive(b *testing.B) {

func BenchmarkGookitSlogPositive(b *testing.B) {
logger := slog.NewWithHandlers(
handler.NewIOWriter(ioutil.Discard, slog.NormalLevels),
handler.NewIOWriter(io.Discard, []slog.Level{slog.InfoLevel}),
)
logger.ReportCaller = false

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
20 changes: 10 additions & 10 deletions _example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,27 @@ module slog_example
go 1.18

require (
github.com/golang/glog v1.0.0
github.com/gookit/goutil v0.6.4
github.com/gookit/slog v0.4.0
github.com/golang/glog v1.1.1
github.com/gookit/goutil v0.6.8
github.com/gookit/slog v0.5.0
github.com/phuslu/log v1.0.67
github.com/rs/zerolog v1.28.0
github.com/rs/zerolog v1.29.0
github.com/sirupsen/logrus v1.9.0
github.com/syyongx/llog v0.0.0-20200222114215-e8f9f86ac0a3
go.uber.org/zap v1.23.0
go.uber.org/zap v1.24.0
)

require (
github.com/gookit/color v1.5.2 // indirect
github.com/gookit/color v1.5.3 // indirect
github.com/gookit/gsr v0.0.8 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.18 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/text v0.7.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
)

replace github.com/gookit/slog => ../

0 comments on commit 2eac8d4

Please sign in to comment.