Source file
src/log/slog/attr_test.go
1
2
3
4
5 package slog
6
7 import (
8 "internal/asan"
9 "internal/testenv"
10 "testing"
11 "time"
12 )
13
14 func TestAttrNoAlloc(t *testing.T) {
15 if asan.Enabled {
16 t.Skip("test allocates with -asan")
17 }
18 testenv.SkipIfOptimizationOff(t)
19
20 var (
21 i int64
22 u uint64
23 f float64
24 b bool
25 s string
26 x any
27 p = &i
28 d time.Duration
29 )
30 a := int(testing.AllocsPerRun(5, func() {
31 i = Int64("key", 1).Value.Int64()
32 u = Uint64("key", 1).Value.Uint64()
33 f = Float64("key", 1).Value.Float64()
34 b = Bool("key", true).Value.Bool()
35 s = String("key", "foo").Value.String()
36 d = Duration("key", d).Value.Duration()
37 x = Any("key", p).Value.Any()
38 }))
39 if a != 0 {
40 t.Errorf("got %d allocs, want zero", a)
41 }
42 _ = u
43 _ = f
44 _ = b
45 _ = s
46 _ = x
47 }
48
49 func BenchmarkAttrString(b *testing.B) {
50 var (
51 is string
52 u string
53 f string
54 bn string
55 s string
56 x string
57 ds string
58 p = &is
59 d time.Duration
60 )
61 b.ReportAllocs()
62 for i := 0; i < b.N; i++ {
63 is = Int64("key", 1).String()
64 u = Uint64("key", 1).String()
65 f = Float64("key", 1).String()
66 bn = Bool("key", true).String()
67 s = String("key", "foo").String()
68 ds = Duration("key", d).String()
69 x = Any("key", p).String()
70 }
71 _ = u
72 _ = f
73 _ = bn
74 _ = s
75 _ = x
76 _ = ds
77 _ = p
78 }
79
View as plain text