-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlong_alloc_test.go
More file actions
51 lines (44 loc) · 1.42 KB
/
long_alloc_test.go
File metadata and controls
51 lines (44 loc) · 1.42 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
//go:build !race
// This file enforces a strict 0-allocs/op contract on the structured
// hot path with very large records (70 KB messages + 70 KB string and
// bytes fields). The check is disabled under -race because the race
// detector triggers GC frequently enough to clear sync.Pool's
// localcache for the 1 MiB-class buffers these records require, which
// leaks ~1 alloc/op into the measurement window. That's a property of
// the test environment, not the production hot path: a non-race build
// keeps the pool warm and runs at 0 allocs/op as advertised.
package zlog
import (
"io"
"strings"
"testing"
)
func TestLongStructuredRecordsZeroAllocWarm(t *testing.T) {
longMsg := strings.Repeat("m", 70_000)
longValue := strings.Repeat("v", 70_000)
escapedValue := strings.Repeat("\"\\\n\r\t", 14_000)
longBytes := make([]byte, 70_000)
tests := []struct {
name string
writer Writer
}{
{name: "binary", writer: io.Discard},
{name: "logfmt", writer: NewLogfmtWriter(io.Discard)},
{name: "terminal", writer: NewTerminalWriter(io.Discard)},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logger := NewStructured()
logger.SetWriter(tt.writer)
allocs := testing.AllocsPerRun(10, func() {
logger.Info(longMsg,
String("long", longValue),
String("escaped", escapedValue),
Bytes("bytes", longBytes))
})
if allocs != 0 {
t.Fatalf("allocs = %v, want 0", allocs)
}
})
}
}