Skip to content

Commit

Permalink
log: Add allocation tests (#4957)
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Feb 22, 2024
1 parent 6ea99af commit 7cc660f
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 0 deletions.
57 changes: 57 additions & 0 deletions log/keyvalue_test.go
Expand Up @@ -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
}
60 changes: 60 additions & 0 deletions log/record_test.go
Expand Up @@ -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
}

0 comments on commit 7cc660f

Please sign in to comment.