Skip to content

Commit 483c534

Browse files
committed
test(slog/pretty): add a couple simple comparison benchmarks
1 parent da54c22 commit 483c534

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
### Go
2+
coverage.out
3+
4+
### JetBrains
15
.idea/
26
*.iml

slog/pretty/buffer_test.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
func BenchmarkBufferPool(b *testing.B) {
1111
pool := newBufferPool()
12-
b.ReportAllocs()
1312
b.ResetTimer()
1413

1514
b.RunParallel(func(pb *testing.PB) {
@@ -24,6 +23,7 @@ func BenchmarkBuffer_Write(b *testing.B) {
2423
buf := newBuffer()
2524
in := []byte("Hello, world!")
2625
b.ResetTimer()
26+
2727
for i := 0; i < b.N; i++ {
2828
_, _ = buf.Write(in)
2929
}
@@ -33,6 +33,7 @@ func BenchmarkBuffer_WriteString(b *testing.B) {
3333
buf := newBuffer()
3434
in := "Hello, world!"
3535
b.ResetTimer()
36+
3637
for i := 0; i < b.N; i++ {
3738
_, _ = buf.WriteString(in)
3839
}
@@ -42,6 +43,7 @@ func BenchmarkBuffer_WriteTo(b *testing.B) {
4243
buf := newBuffer()
4344
_, _ = buf.WriteString(strings.Repeat("a", 1024))
4445
b.ResetTimer()
46+
4547
for i := 0; i < b.N; i++ {
4648
_, _ = buf.WriteTo(io.Discard)
4749
}
@@ -51,6 +53,7 @@ func BenchmarkBuffer_AppendByte(b *testing.B) {
5153
buf := newBuffer()
5254
in := byte('\n')
5355
b.ResetTimer()
56+
5457
for i := 0; i < b.N; i++ {
5558
buf.AppendByte(in)
5659
}
@@ -60,6 +63,7 @@ func BenchmarkBuffer_AppendBytes(b *testing.B) {
6063
buf := newBuffer()
6164
in := []byte("Hello, world!")
6265
b.ResetTimer()
66+
6367
for i := 0; i < b.N; i++ {
6468
buf.AppendBytes(in)
6569
}
@@ -69,6 +73,7 @@ func BenchmarkBuffer_AppendString(b *testing.B) {
6973
buf := newBuffer()
7074
in := "Hello, world!"
7175
b.ResetTimer()
76+
7277
for i := 0; i < b.N; i++ {
7378
buf.AppendString(in)
7479
}
@@ -78,6 +83,7 @@ func BenchmarkBuffer_AppendInt(b *testing.B) {
7883
buf := newBuffer()
7984
in := int64(42)
8085
b.ResetTimer()
86+
8187
for i := 0; i < b.N; i++ {
8288
buf.AppendInt(in)
8389
}
@@ -87,6 +93,7 @@ func BenchmarkBuffer_AppendUint(b *testing.B) {
8793
buf := newBuffer()
8894
in := uint64(73)
8995
b.ResetTimer()
96+
9097
for i := 0; i < b.N; i++ {
9198
buf.AppendUint(in)
9299
}
@@ -96,6 +103,7 @@ func BenchmarkBuffer_AppendFloat32(b *testing.B) {
96103
buf := newBuffer()
97104
in := float32(3.14)
98105
b.ResetTimer()
106+
99107
for i := 0; i < b.N; i++ {
100108
buf.AppendFloat32(in)
101109
}
@@ -105,6 +113,7 @@ func BenchmarkBuffer_AppendFloat64(b *testing.B) {
105113
buf := newBuffer()
106114
in := 3.14159265
107115
b.ResetTimer()
116+
108117
for i := 0; i < b.N; i++ {
109118
buf.AppendFloat64(in)
110119
}
@@ -113,6 +122,7 @@ func BenchmarkBuffer_AppendFloat64(b *testing.B) {
113122
func BenchmarkBuffer_AppendBool(b *testing.B) {
114123
buf := newBuffer()
115124
b.ResetTimer()
125+
116126
for i := 0; i < b.N; i++ {
117127
buf.AppendBool(true)
118128
}
@@ -123,6 +133,7 @@ func BenchmarkBuffer_AppendTimeFormat(b *testing.B) {
123133
t := time.Now()
124134
layout := time.RFC3339
125135
b.ResetTimer()
136+
126137
for i := 0; i < b.N; i++ {
127138
buf.AppendTimeFormat(t, layout)
128139
}

slog/pretty/handler_test.go

+49
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package pretty
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"log/slog"
78
"regexp"
89
"strconv"
@@ -40,6 +41,54 @@ func TestHandler(t *testing.T) {
4041
slogtest.Run(t, newHandler, result)
4142
}
4243

44+
func BenchmarkDefaultTextHandler(b *testing.B) {
45+
l := slog.New(slog.NewTextHandler(io.Discard, nil))
46+
b.ResetTimer()
47+
48+
b.RunParallel(func(pb *testing.PB) {
49+
for pb.Next() {
50+
l.Info("Hello, world!")
51+
}
52+
})
53+
}
54+
55+
func BenchmarkDefaultJSONHandler(b *testing.B) {
56+
l := slog.New(slog.NewJSONHandler(io.Discard, nil))
57+
b.ResetTimer()
58+
59+
b.RunParallel(func(pb *testing.PB) {
60+
for pb.Next() {
61+
l.Info("Hello, world!")
62+
}
63+
})
64+
}
65+
66+
func BenchmarkHandlerWithoutSource(b *testing.B) {
67+
l := slog.New(NewHandler(io.Discard, &Options{
68+
AddSource: false,
69+
}))
70+
b.ResetTimer()
71+
72+
b.RunParallel(func(pb *testing.PB) {
73+
for pb.Next() {
74+
l.Info("Hello, world!")
75+
}
76+
})
77+
}
78+
79+
func BenchmarkHandlerWithSource(b *testing.B) {
80+
l := slog.New(NewHandler(io.Discard, &Options{
81+
AddSource: true,
82+
}))
83+
b.ResetTimer()
84+
85+
b.RunParallel(func(pb *testing.PB) {
86+
for pb.Next() {
87+
l.Info("Hello, world!")
88+
}
89+
})
90+
}
91+
4392
func parse(b []byte) (map[string]any, error) {
4493
m := make(map[string]any)
4594
s := string(bytes.TrimSpace(b))

0 commit comments

Comments
 (0)