-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark_test.go
73 lines (58 loc) · 1.67 KB
/
benchmark_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package zapctxd_test
import (
"context"
"io"
"testing"
"github.com/bool64/ctxd"
"github.com/bool64/zapctxd"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
// BenchmarkSugaredZap benchmarks sugared zap logger.
// BenchmarkSugaredZap-4 975686 1174 ns/op 256 B/op 1 allocs/op.
func BenchmarkSugaredZap(b *testing.B) {
logger := zap.New(
zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionConfig().EncoderConfig),
&zaptest.Discarder{},
zap.DebugLevel,
))
s := logger.Sugar()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
s.Debugw("hello!", "bla2", 2, "bla", 1)
}
}
// BenchmarkCtxFull benchmarks zapctxd.Logger performance with rich context (realistic).
// BenchmarkCtxFull-4 320354 3378 ns/op 1248 B/op 8 allocs/op.
func BenchmarkCtxFull(b *testing.B) {
c := zapctxd.New(zapctxd.Config{})
ctx := context.Background()
ctx = ctxd.AddFields(ctx, "bla2", 2, "ops", 3.5)
ctx = ctxd.WithLogWriter(ctx, io.Discard)
ctx = ctxd.WithDebug(ctx)
// Put some pressure on context.
type ctxInt int
for i := 0; i < 20; i++ {
ctx = context.WithValue(ctx, ctxInt(i), i)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Debug(ctx, "hello!", "bla", 1)
}
}
// BenchmarkCtxLite benchmarks zapctxd.Logger performance with empty context (optimistic).
// BenchmarkCtxLite-4 830715 1381 ns/op 256 B/op 1 allocs/op.
func BenchmarkCtxLite(b *testing.B) {
c := zapctxd.New(zapctxd.Config{
Level: zap.DebugLevel,
Output: io.Discard,
})
ctx := context.Background()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
c.Debug(ctx, "hello!", "bla2", 2, "bla", 1)
}
}