]> Cypherpunks.ru repositories - gostls13.git/blob - src/log/slog/record_test.go
log/slog: initial commit
[gostls13.git] / src / log / slog / record_test.go
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.
4
5 package slog
6
7 import (
8         "log/slog/internal/buffer"
9         "slices"
10         "strconv"
11         "strings"
12         "testing"
13         "time"
14 )
15
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)
22         }
23         if got := attrsSlice(r); !attrsEqual(got, as) {
24                 t.Errorf("got %v, want %v", got, as)
25         }
26 }
27
28 func TestRecordSourceLine(t *testing.T) {
29         // Zero call depth => empty file/line
30         for _, test := range []struct {
31                 depth            int
32                 wantFile         string
33                 wantLinePositive bool
34         }{
35                 {0, "", false},
36                 {-16, "", false},
37                 {1, "record_test.go", true}, // 1: caller of NewRecord
38                 {2, "testing.go", true},
39         } {
40                 var pc uintptr
41                 if test.depth > 0 {
42                         pc = callerPC(test.depth + 1)
43                 }
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:]
48                 }
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)
52                 }
53         }
54 }
55
56 func TestAliasingAndClone(t *testing.T) {
57         intAttrs := func(from, to int) []Attr {
58                 var as []Attr
59                 for i := from; i < to; i++ {
60                         as = append(as, Int("k", i))
61                 }
62                 return as
63         }
64
65         check := func(r Record, want []Attr) {
66                 t.Helper()
67                 got := attrsSlice(r)
68                 if !attrsEqual(got, want) {
69                         t.Errorf("got %v, want %v", got, want)
70                 }
71         }
72
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)
79         copy(b, r1.back)
80         r1.back = b
81         // Make a copy that shares state.
82         r2 := r1
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")
87         }
88         r1Attrs := attrsSlice(r1)
89         // Adding to a clone is fine.
90         r2 = r1.Clone()
91         check(r2, r1Attrs)
92         r2.AddAttrs(Int("p", 2))
93         check(r1, r1Attrs) // r1 is unchanged
94         check(r2, append(slices.Clip(r1Attrs), Int("p", 2)))
95 }
96
97 func newRecordWithAttrs(as []Attr) Record {
98         r := NewRecord(time.Now(), LevelInfo, "", 0)
99         r.AddAttrs(as...)
100         return r
101 }
102
103 func attrsSlice(r Record) []Attr {
104         s := make([]Attr, 0, r.NumAttrs())
105         r.Attrs(func(a Attr) { s = append(s, a) })
106         return s
107 }
108
109 func attrsEqual(as1, as2 []Attr) bool {
110         return slices.EqualFunc(as1, as2, Attr.Equal)
111 }
112
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) {
118                         b.ReportAllocs()
119                         var x uintptr
120                         for i := 0; i < b.N; i++ {
121                                 x = callerPC(depth)
122                         }
123                         _ = x
124                 })
125         }
126 }
127
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)
133                         _ = file
134                         _ = line
135                 }
136         })
137         b.Run("stringifying", func(b *testing.B) {
138                 for i := 0; i < b.N; i++ {
139                         file, line := sourceLine(r)
140                         buf := buffer.New()
141                         buf.WriteString(file)
142                         buf.WriteByte(':')
143                         buf.WritePosInt(line)
144                         s := buf.String()
145                         buf.Free()
146                         _ = s
147                 }
148         })
149 }
150
151 func BenchmarkRecord(b *testing.B) {
152         const nAttrs = nAttrsInline * 10
153         var a Attr
154
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))
159                 }
160                 r.Attrs(func(b Attr) { a = b })
161         }
162         _ = a
163 }