|
| 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 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "golang.org/x/exp/slices" |
| 13 | + "golang.org/x/exp/slog/internal/buffer" |
| 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 := r.Attrs(); !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.go", true}, |
| 38 | + } { |
| 39 | + r := MakeRecord(time.Time{}, 0, "", test.depth) |
| 40 | + gotFile, gotLine := r.SourceLine() |
| 41 | + if i := strings.LastIndexByte(gotFile, '/'); i >= 0 { |
| 42 | + gotFile = gotFile[i+1:] |
| 43 | + } |
| 44 | + if gotFile != test.wantFile || (gotLine > 0) != test.wantLinePositive { |
| 45 | + t.Errorf("depth %d: got (%q, %d), want (%q, %t)", |
| 46 | + test.depth, gotFile, gotLine, test.wantFile, test.wantLinePositive) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func TestAliasing(t *testing.T) { |
| 52 | + intAttrs := func(from, to int) []Attr { |
| 53 | + var as []Attr |
| 54 | + for i := from; i < to; i++ { |
| 55 | + as = append(as, Int("k", i)) |
| 56 | + } |
| 57 | + return as |
| 58 | + } |
| 59 | + |
| 60 | + check := func(r *Record, want []Attr) { |
| 61 | + t.Helper() |
| 62 | + got := r.Attrs() |
| 63 | + if !attrsEqual(got, want) { |
| 64 | + t.Errorf("got %v, want %v", got, want) |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + r1 := MakeRecord(time.Time{}, 0, "", 0) |
| 69 | + for i := 0; i < nAttrsInline+3; i++ { |
| 70 | + r1.AddAttr(Int("k", i)) |
| 71 | + } |
| 72 | + check(&r1, intAttrs(0, nAttrsInline+3)) |
| 73 | + r2 := r1 |
| 74 | + check(&r2, intAttrs(0, nAttrsInline+3)) |
| 75 | + // if cap(r1.attrs2) <= len(r1.attrs2) { |
| 76 | + // t.Fatal("cap not greater than len") |
| 77 | + // } |
| 78 | + r1.AddAttr(Int("k", nAttrsInline+3)) |
| 79 | + r2.AddAttr(Int("k", -1)) |
| 80 | + check(&r1, intAttrs(0, nAttrsInline+4)) |
| 81 | + check(&r2, append(intAttrs(0, nAttrsInline+3), Int("k", -1))) |
| 82 | +} |
| 83 | + |
| 84 | +func newRecordWithAttrs(as []Attr) Record { |
| 85 | + r := MakeRecord(time.Now(), InfoLevel, "", 0) |
| 86 | + for _, a := range as { |
| 87 | + r.AddAttr(a) |
| 88 | + } |
| 89 | + return r |
| 90 | +} |
| 91 | + |
| 92 | +func attrsEqual(as1, as2 []Attr) bool { |
| 93 | + return slices.EqualFunc(as1, as2, Attr.Equal) |
| 94 | +} |
| 95 | + |
| 96 | +// Currently, pc(2) takes over 400ns, which is too expensive |
| 97 | +// to call it for every log message. |
| 98 | +func BenchmarkPC(b *testing.B) { |
| 99 | + b.ReportAllocs() |
| 100 | + var x uintptr |
| 101 | + for i := 0; i < b.N; i++ { |
| 102 | + x = pc(3) |
| 103 | + } |
| 104 | + _ = x |
| 105 | +} |
| 106 | + |
| 107 | +func BenchmarkSourceLine(b *testing.B) { |
| 108 | + r := MakeRecord(time.Now(), InfoLevel, "", 5) |
| 109 | + b.Run("alone", func(b *testing.B) { |
| 110 | + for i := 0; i < b.N; i++ { |
| 111 | + file, line := r.SourceLine() |
| 112 | + _ = file |
| 113 | + _ = line |
| 114 | + } |
| 115 | + }) |
| 116 | + b.Run("stringifying", func(b *testing.B) { |
| 117 | + for i := 0; i < b.N; i++ { |
| 118 | + file, line := r.SourceLine() |
| 119 | + buf := buffer.New() |
| 120 | + buf.WriteString(file) |
| 121 | + buf.WriteByte(':') |
| 122 | + itoa((*[]byte)(buf), line, -1) |
| 123 | + s := string(*buf) |
| 124 | + buf.Free() |
| 125 | + _ = s |
| 126 | + } |
| 127 | + }) |
| 128 | +} |
| 129 | + |
| 130 | +func BenchmarkRecord(b *testing.B) { |
| 131 | + const nAttrs = nAttrsInline * 10 |
| 132 | + var a Attr |
| 133 | + |
| 134 | + for i := 0; i < b.N; i++ { |
| 135 | + r := MakeRecord(time.Time{}, InfoLevel, "", 0) |
| 136 | + for j := 0; j < nAttrs; j++ { |
| 137 | + r.AddAttr(Int("k", j)) |
| 138 | + } |
| 139 | + for j := 0; j < nAttrs; j++ { |
| 140 | + a = r.Attr(j) |
| 141 | + } |
| 142 | + } |
| 143 | + _ = a |
| 144 | +} |
0 commit comments