You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2026-06-03 18:35:08 +02:00
log: Add allocation tests (#4957)
This commit is contained in:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user