From 7cc660fc0f81e54adc01d88bc5e4af8d6f362d50 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 22 Feb 2024 11:47:40 -0800 Subject: [PATCH] log: Add allocation tests (#4957) --- log/keyvalue_test.go | 57 +++++++++++++++++++++++++++++++++++++++++ log/record_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/log/keyvalue_test.go b/log/keyvalue_test.go index 4390bfae538..7a72ae06431 100644 --- a/log/keyvalue_test.go +++ b/log/keyvalue_test.go @@ -300,3 +300,60 @@ func testKV[T any](t *testing.T, key string, val T, kv log.KeyValue) { assert.False(t, kv.Value.Empty(), "value empty") assert.Equal(t, kv.Value.AsAny(), T(val), "AsAny wrong value") } + +func TestAllocationLimits(t *testing.T) { + const ( + runs = 5 + key = "key" + ) + + // Assign testing results to external scope so the compiler doesn't + // optimize away the testing statements. + var ( + i int64 + f float64 + b bool + by []byte + s string + slice []log.Value + m []log.KeyValue + ) + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + b = log.Bool(key, true).Value.AsBool() + }), "Bool.AsBool") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + f = log.Float64(key, 3.0).Value.AsFloat64() + }), "Float.AsFloat64") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + i = log.Int(key, 9).Value.AsInt64() + }), "Int.AsInt64") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + i = log.Int64(key, 8).Value.AsInt64() + }), "Int64.AsInt64") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + s = log.String(key, "value").Value.AsString() + }), "String.AsString") + + byteVal := []byte{1, 3, 4} + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + by = log.Bytes(key, byteVal).Value.AsBytes() + }), "Byte.AsBytes") + + sliceVal := []log.Value{log.BoolValue(true), log.IntValue(32)} + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + slice = log.Slice(key, sliceVal...).Value.AsSlice() + }), "Slice.AsSlice") + + mapVal := []log.KeyValue{log.Bool("b", true), log.Int("i", 32)} + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + m = log.Map(key, mapVal...).Value.AsMap() + }), "Map.AsMap") + + // Convince the linter these values are used. + _, _, _, _, _, _, _ = i, f, b, by, s, slice, m +} diff --git a/log/record_test.go b/log/record_test.go index c3eb9539c0a..f17f4f8d213 100644 --- a/log/record_test.go +++ b/log/record_test.go @@ -103,3 +103,63 @@ func TestRecordAttributes(t *testing.T) { } }) } + +func TestRecordAllocationLimits(t *testing.T) { + const runs = 5 + + // Assign testing results to external scope so the compiler doesn't + // optimize away the testing statements. + var ( + tStamp time.Time + sev log.Severity + text string + body log.Value + n int + attr log.KeyValue + ) + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + var r log.Record + r.SetTimestamp(y2k) + tStamp = r.Timestamp() + }), "Timestamp") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + var r log.Record + r.SetObservedTimestamp(y2k) + tStamp = r.ObservedTimestamp() + }), "ObservedTimestamp") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + var r log.Record + r.SetSeverity(log.SeverityDebug) + sev = r.Severity() + }), "Severity") + + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + var r log.Record + r.SetSeverityText("severity text") + text = r.SeverityText() + }), "SeverityText") + + bodyVal := log.BoolValue(true) + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + var r log.Record + r.SetBody(bodyVal) + body = r.Body() + }), "Body") + + attrVal := []log.KeyValue{log.Bool("k", true), log.Int("i", 1)} + assert.Equal(t, 0.0, testing.AllocsPerRun(runs, func() { + var r log.Record + r.AddAttributes(attrVal...) + n = r.AttributesLen() + r.WalkAttributes(func(kv log.KeyValue) bool { + attr = kv + return true + }) + }), "Attributes") + + // Convince the linter these values are used. + _, _, _, _, _, _ = tStamp, sev, text, body, n, attr +}