-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_test.go
More file actions
72 lines (64 loc) · 1.61 KB
/
Copy pathbenchmark_test.go
File metadata and controls
72 lines (64 loc) · 1.61 KB
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
package slogx
import (
"io"
"testing"
)
// BenchmarkNew measures the cost of building a default logger with env loading
// disabled — representative of a cold start without environment interaction.
func BenchmarkNew(b *testing.B) {
for b.Loop() {
_ = New(
WithWriter(io.Discard),
WithLoadFromEnv(false),
WithSetDefault(false),
)
}
}
// BenchmarkNew_WithHandler measures the [WithHandler] short-circuit path where
// format, writer, level, and add-source resolution are skipped.
func BenchmarkNew_WithHandler(b *testing.B) {
h := stubHandler{}
b.ResetTimer()
for b.Loop() {
_ = New(
WithHandler(h),
WithLoadFromEnv(false),
WithSetDefault(false),
)
}
}
// BenchmarkLoadFromEnv measures the cost of parsing all four LOG_* overrides.
func BenchmarkLoadFromEnv(b *testing.B) {
b.Setenv("LOG_FORMAT", "text")
b.Setenv("LOG_LEVEL", "debug")
b.Setenv("LOG_ADD_SOURCE", "true")
b.Setenv("LOG_OUTPUT", "stderr")
b.ResetTimer()
for b.Loop() {
loadFromEnv(defaultConfig())
}
}
// BenchmarkLogger_Info measures end-to-end emit cost for a single info record
// through the default JSON handler to [io.Discard].
func BenchmarkLogger_Info(b *testing.B) {
lg := New(
WithWriter(io.Discard),
WithLoadFromEnv(false),
WithSetDefault(false),
)
b.ResetTimer()
for b.Loop() {
lg.Info("benchmark", "k", "v")
}
}
// BenchmarkNewWithLevelVar measures the cost of building a logger and
// allocating the returned [*slog.LevelVar].
func BenchmarkNewWithLevelVar(b *testing.B) {
for b.Loop() {
_, _ = NewWithLevelVar(
WithWriter(io.Discard),
WithLoadFromEnv(false),
WithSetDefault(false),
)
}
}