Skip to content

Commit 0abda43

Browse files
committed
benchmark colored output
1 parent 22f02e1 commit 0abda43

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

internal/entryhuman/entry_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package entryhuman_test
22

33
import (
4+
"fmt"
5+
"io"
46
"io/ioutil"
57
"testing"
68
"time"
@@ -92,3 +94,38 @@ func TestEntry(t *testing.T) {
9294
assert.Equal(t, "entry", "\x1b[0m\x1b[0m0001-01-01 00:00:00.000 \x1b[91m[CRITICAL]\x1b[0m\t\x1b[36m<.:0> \x1b[0m\t\"\"\t{\x1b[34m\"hey\"\x1b[0m: \x1b[32m\"hi\"\x1b[0m}", act)
9395
})
9496
}
97+
98+
func BenchmarkFmt(b *testing.B) {
99+
bench := func(b *testing.B, color bool) {
100+
nfs := []int{1, 4, 16}
101+
for _, nf := range nfs {
102+
name := fmt.Sprintf("nf=%v", nf)
103+
if color {
104+
name = "Colored-" + name
105+
}
106+
b.Run(name, func(b *testing.B) {
107+
fs := make([]slog.Field, nf)
108+
for i := 0; i < nf; i++ {
109+
fs[i] = slog.F("key", "value")
110+
}
111+
se := slog.SinkEntry{
112+
Level: slog.LevelCritical,
113+
Fields: slog.M(
114+
fs...,
115+
),
116+
}
117+
w := io.Discard
118+
if color {
119+
w = entryhuman.ForceColorWriter
120+
}
121+
b.ResetTimer()
122+
b.ReportAllocs()
123+
for i := 0; i < b.N; i++ {
124+
_ = entryhuman.Fmt(w, se)
125+
}
126+
})
127+
}
128+
}
129+
bench(b, true)
130+
bench(b, false)
131+
}

0 commit comments

Comments
 (0)