Skip to content

Commit 7cc660f

Browse files
authored
log: Add allocation tests (#4957)
1 parent 6ea99af commit 7cc660f

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

log/keyvalue_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,3 +300,60 @@ func testKV[T any](t *testing.T, key string, val T, kv log.KeyValue) {
300300
assert.False(t, kv.Value.Empty(), "value empty")
301301
assert.Equal(t, kv.Value.AsAny(), T(val), "AsAny wrong value")
302302
}
303+
304+
func TestAllocationLimits(t *testing.T) {
305+
const (
306+
runs = 5
307+
key = "key"
308+
)
309+
310+
// Assign testing results to external scope so the compiler doesn't
311+
// optimize away the testing statements.
312+
var (
313+
i int64
314+
f float64
315+
b bool
316+
by []byte
317+
s string
318+
slice []log.Value
319+
m []log.KeyValue
320+
)
321+
322+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
323+
b = log.Bool(key, true).Value.AsBool()
324+
}), "Bool.AsBool")
325+
326+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
327+
f = log.Float64(key, 3.0).Value.AsFloat64()
328+
}), "Float.AsFloat64")
329+
330+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
331+
i = log.Int(key, 9).Value.AsInt64()
332+
}), "Int.AsInt64")
333+
334+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
335+
i = log.Int64(key, 8).Value.AsInt64()
336+
}), "Int64.AsInt64")
337+
338+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
339+
s = log.String(key, "value").Value.AsString()
340+
}), "String.AsString")
341+
342+
byteVal := []byte{1, 3, 4}
343+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
344+
by = log.Bytes(key, byteVal).Value.AsBytes()
345+
}), "Byte.AsBytes")
346+
347+
sliceVal := []log.Value{log.BoolValue(true), log.IntValue(32)}
348+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
349+
slice = log.Slice(key, sliceVal...).Value.AsSlice()
350+
}), "Slice.AsSlice")
351+
352+
mapVal := []log.KeyValue{log.Bool("b", true), log.Int("i", 32)}
353+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
354+
m = log.Map(key, mapVal...).Value.AsMap()
355+
}), "Map.AsMap")
356+
357+
// Convince the linter these values are used.
358+
_, _, _, _, _, _, _ = i, f, b, by, s, slice, m
359+
}

log/record_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,3 +103,63 @@ func TestRecordAttributes(t *testing.T) {
103103
}
104104
})
105105
}
106+
107+
func TestRecordAllocationLimits(t *testing.T) {
108+
const runs = 5
109+
110+
// Assign testing results to external scope so the compiler doesn't
111+
// optimize away the testing statements.
112+
var (
113+
tStamp time.Time
114+
sev log.Severity
115+
text string
116+
body log.Value
117+
n int
118+
attr log.KeyValue
119+
)
120+
121+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
122+
var r log.Record
123+
r.SetTimestamp(y2k)
124+
tStamp = r.Timestamp()
125+
}), "Timestamp")
126+
127+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
128+
var r log.Record
129+
r.SetObservedTimestamp(y2k)
130+
tStamp = r.ObservedTimestamp()
131+
}), "ObservedTimestamp")
132+
133+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
134+
var r log.Record
135+
r.SetSeverity(log.SeverityDebug)
136+
sev = r.Severity()
137+
}), "Severity")
138+
139+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
140+
var r log.Record
141+
r.SetSeverityText("severity text")
142+
text = r.SeverityText()
143+
}), "SeverityText")
144+
145+
bodyVal := log.BoolValue(true)
146+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
147+
var r log.Record
148+
r.SetBody(bodyVal)
149+
body = r.Body()
150+
}), "Body")
151+
152+
attrVal := []log.KeyValue{log.Bool("k", true), log.Int("i", 1)}
153+
assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() {
154+
var r log.Record
155+
r.AddAttributes(attrVal...)
156+
n = r.AttributesLen()
157+
r.WalkAttributes(func(kv log.KeyValue) bool {
158+
attr = kv
159+
return true
160+
})
161+
}), "Attributes")
162+
163+
// Convince the linter these values are used.
164+
_, _, _, _, _, _ = tStamp, sev, text, body, n, attr
165+
}

0 commit comments

Comments
 (0)