1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
8 "log/slog/internal/buffer"
16 func TestRecordAttrs(t *testing.T) {
17 as := []Attr{Int("k1", 1), String("k2", "foo"), Int("k3", 3),
18 Int64("k4", -1), Float64("f", 3.1), Uint64("u", 999)}
19 r := newRecordWithAttrs(as)
20 if g, w := r.NumAttrs(), len(as); g != w {
21 t.Errorf("NumAttrs: got %d, want %d", g, w)
23 if got := attrsSlice(r); !attrsEqual(got, as) {
24 t.Errorf("got %v, want %v", got, as)
28 func TestRecordSourceLine(t *testing.T) {
29 // Zero call depth => empty file/line
30 for _, test := range []struct {
37 {1, "record_test.go", true}, // 1: caller of NewRecord
38 {2, "testing.go", true},
42 pc = callerPC(test.depth + 1)
44 r := NewRecord(time.Time{}, 0, "", pc)
45 gotFile, gotLine := sourceLine(r)
46 if i := strings.LastIndexByte(gotFile, '/'); i >= 0 {
47 gotFile = gotFile[i+1:]
49 if gotFile != test.wantFile || (gotLine > 0) != test.wantLinePositive {
50 t.Errorf("depth %d: got (%q, %d), want (%q, %t)",
51 test.depth, gotFile, gotLine, test.wantFile, test.wantLinePositive)
56 func TestAliasingAndClone(t *testing.T) {
57 intAttrs := func(from, to int) []Attr {
59 for i := from; i < to; i++ {
60 as = append(as, Int("k", i))
65 check := func(r Record, want []Attr) {
68 if !attrsEqual(got, want) {
69 t.Errorf("got %v, want %v", got, want)
73 // Create a record whose Attrs overflow the inline array,
74 // creating a slice in r.back.
75 r1 := NewRecord(time.Time{}, 0, "", 0)
76 r1.AddAttrs(intAttrs(0, nAttrsInline+1)...)
77 // Ensure that r1.back's capacity exceeds its length.
78 b := make([]Attr, len(r1.back), len(r1.back)+1)
81 // Make a copy that shares state.
83 // Adding to both should panic.
84 r1.AddAttrs(Int("p", 0))
85 if !panics(func() { r2.AddAttrs(Int("p", 1)) }) {
86 t.Error("expected panic")
88 r1Attrs := attrsSlice(r1)
89 // Adding to a clone is fine.
92 r2.AddAttrs(Int("p", 2))
93 check(r1, r1Attrs) // r1 is unchanged
94 check(r2, append(slices.Clip(r1Attrs), Int("p", 2)))
97 func newRecordWithAttrs(as []Attr) Record {
98 r := NewRecord(time.Now(), LevelInfo, "", 0)
103 func attrsSlice(r Record) []Attr {
104 s := make([]Attr, 0, r.NumAttrs())
105 r.Attrs(func(a Attr) { s = append(s, a) })
109 func attrsEqual(as1, as2 []Attr) bool {
110 return slices.EqualFunc(as1, as2, Attr.Equal)
113 // Currently, pc(2) takes over 400ns, which is too expensive
114 // to call it for every log message.
115 func BenchmarkPC(b *testing.B) {
116 for depth := 0; depth < 5; depth++ {
117 b.Run(strconv.Itoa(depth), func(b *testing.B) {
120 for i := 0; i < b.N; i++ {
128 func BenchmarkSourceLine(b *testing.B) {
129 r := NewRecord(time.Now(), LevelInfo, "", 5)
130 b.Run("alone", func(b *testing.B) {
131 for i := 0; i < b.N; i++ {
132 file, line := sourceLine(r)
137 b.Run("stringifying", func(b *testing.B) {
138 for i := 0; i < b.N; i++ {
139 file, line := sourceLine(r)
141 buf.WriteString(file)
143 buf.WritePosInt(line)
151 func BenchmarkRecord(b *testing.B) {
152 const nAttrs = nAttrsInline * 10
155 for i := 0; i < b.N; i++ {
156 r := NewRecord(time.Time{}, LevelInfo, "", 0)
157 for j := 0; j < nAttrs; j++ {
158 r.AddAttrs(Int("k", j))
160 r.Attrs(func(b Attr) { a = b })