-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc_regression_test.go
More file actions
104 lines (93 loc) · 2.67 KB
/
alloc_regression_test.go
File metadata and controls
104 lines (93 loc) · 2.67 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package pslog
import (
"io"
"math"
"testing"
)
// Regression: hot path logging should allocate 0 bytes for all emitter variants
// when given pre-built keyvals (to avoid variadic slice creation) and no
// timestamps.
func TestLoggersAllocateZero(t *testing.T) {
keyvals := []any{"key", "value", "n", 123, "b", true}
cases := []struct {
name string
opts Options
}{
{"console_plain", Options{Mode: ModeConsole, DisableTimestamp: true, NoColor: true}},
{"console_color", Options{Mode: ModeConsole, DisableTimestamp: true, ForceColor: true}},
{"json_plain", Options{Mode: ModeStructured, DisableTimestamp: true, NoColor: true}},
{"json_color", Options{Mode: ModeStructured, DisableTimestamp: true, ForceColor: true}},
}
for _, tc := range cases {
logger := NewWithOptions(nil, io.Discard, tc.opts)
// Warm caches (duration/time/string/float) so the measured run is steady-state.
logger.Info("warm", keyvals...)
allocs := testing.AllocsPerRun(1000, func() {
logger.Info("msg", keyvals...)
})
if allocs != 0 {
t.Fatalf("%s: expected 0 allocs/log, got %.2f", tc.name, allocs)
}
}
}
// Regression: JSON non-finite float serialization should stay zero-allocation
// in steady-state for both string and null policies.
func TestJSONNonFiniteFloatAllocateZero(t *testing.T) {
keyvals := []any{
"nan", math.NaN(),
"pos_inf", math.Inf(1),
"neg_inf", math.Inf(-1),
}
cases := []struct {
name string
opts Options
}{
{
name: "json_plain_string_policy",
opts: Options{
Mode: ModeStructured,
DisableTimestamp: true,
NoColor: true,
NonFiniteFloatPolicy: NonFiniteFloatAsString,
},
},
{
name: "json_color_string_policy",
opts: Options{
Mode: ModeStructured,
DisableTimestamp: true,
ForceColor: true,
NonFiniteFloatPolicy: NonFiniteFloatAsString,
},
},
{
name: "json_plain_null_policy",
opts: Options{
Mode: ModeStructured,
DisableTimestamp: true,
NoColor: true,
NonFiniteFloatPolicy: NonFiniteFloatAsNull,
},
},
{
name: "json_color_null_policy",
opts: Options{
Mode: ModeStructured,
DisableTimestamp: true,
ForceColor: true,
NonFiniteFloatPolicy: NonFiniteFloatAsNull,
},
},
}
for _, tc := range cases {
logger := NewWithOptions(nil, io.Discard, tc.opts)
// Warm internal writer caches before measuring steady-state allocations.
logger.Info("warm", keyvals...)
allocs := testing.AllocsPerRun(1000, func() {
logger.Info("msg", keyvals...)
})
if allocs != 0 {
t.Fatalf("%s: expected 0 allocs/log, got %.2f", tc.name, allocs)
}
}
}